chapter_02_reduced_slides
chapter_02_reduced_slides
2nd ed.
DESIGN The Hardware/Software
Interface
Chapter 2
Instructions: Language of
the Computer
§2.1 Introduction
Instruction Set
■The repertoire of instructions of a
computer
■Different computers have different
instruction sets
■But with many aspects in common
■Early computers had very simple
instruction sets
■Simplified implementation
■Many modern computers also have simple
instruction sets
■Range: 0 to +2n – 1
■Example
■0000 0000 … 0000 10112
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
■Using 64 bits: 0 to +18,446,774,073,709,551,615
■Example: negate +2
■+2 = 0000 0000 … 0010two
■–2 = 1111 1111 … 1101two + 1
= 1111 1111 … 1110two
Chapter 2 — Instructions: Language of the Computer — 16
Sign Extension
■Representing a number using more bits
■Preserve the numeric value
■Replicate the sign bit to the left
■c.f. unsigned values: extend with 0s
■Examples: 8-bit to 16-bit
■+2: 0000 0010 => 0000 0000 0000 0010
■–2: 1111 1110 => 1111 1111 1111 1110
■RISC-V instructions
■Encoded as 32-bit instruction words
■Small number of formats encoding operation code
(opcode), register numbers, …
■Regularity!
add x9,x20,x21
0 21 20 0 9 51
■Argument n in x10
■Result in x10
imm[12] imm[11]
■PC-relative addressing
■Target address = PC + immediate × 2
■ Example 2: lock
addi x12,x0,1 // copy locked value
again: lr.d x10,(x20) // read lock
bne x10,x0,again // check if it is 0 yet
sc.d x11,(x20),x12 // attempt to store
bne x11,x0,again // branch if fails
■ Unlock:
sd x0,0(x20) // free lock