Chapter 02
Chapter 02
Chapter 02
Edition
The Hardware/Software Interface
Chapter 2
Instructions: Language
of the Computer
COMPUTER ORGANIZATION AND DESIGN ARM
Edition
The Hardware/Software Interface
§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 0000 0000 0000 0000 10112
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
Using 32 bits
0 to +4,294,967,295
x x 1111...1112 1
x 1 x
Example: negate +2
+2 = 0000 0000 … 0010two
–2 = 1111 1111 … 1101two + 1
= 1111 1111 … 1110two
LEGv8 instructions
Encoded as 32-bit instruction words
Small number of formats encoding operation code
(opcode), register numbers, …
Regularity!
Instruction fields
opcode: operation code
Rm: the second register source operand
shamt: shift amount (00000 for now)
Rn: the first register source operand
Rd: the register destination
ADD X9,X20,X21
1112ten 21ten 0ten 20ten 9ten
8B15028916
Load/store instructions
Rn: base register
address: constant offset from contents of base register (+/- 32
doublewords)
Rt: destination (load) or source (store) register number
Immediate instructions
Rn: source register
Rd: destination register
CBZ register, L1
if (register == 0) branch to instruction labeled L1;
CBNZ register, L1
if (register != 0) branch to instruction labeled L1;
B L1
branch unconditionally to instruction labeled L1;
Chapter 2 — Instructions: Language of the Computer — 31
Compiling If Statements
C code:
if (i==j) f = g+h;
else f = g-h;
f, g, … in X22, X23, …
Compiled LEGv8 code:
SUB X9,X22,X23
CBNZ X9,Else
ADD X19,X20,X21
B Exit
Else: SUB X9,X22,x23
Exit: … Assembler calculates addresses
Chapter 2 — Instructions: Language of the Computer — 32
Compiling Loop Statements
C code:
while (save[i] == k) i += 1;
i in x22, k in x24, address of save in x25
Compiled LEGv8 code:
Loop: LSL X10,X22,#3
ADD X10,X10,X25
LDUR X9,[X10,#0]
SUB X11,X9,X24
CBNZ X11,Exit
ADDI X22,X22,#1
B Loop
Exit: …
Chapter 2 — Instructions: Language of the Computer — 33
Basic Blocks
A basic block is a sequence of instructions
with
No embedded branches (except at end)
No branch targets (except at beginning)
Argument n in X0
Result in X1
MOVK X9,255,LSL 0
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1111 1111 0000 0000 1111 1111
5 10000ten
6 bits 26 bits
CB-type
CBNZ X19, Exit // go to Exit if X19 != 0
181 Exit 19
8 bits 19 bits 5 bits
Example 2: lock
ADDI X11,XZR,#1 // copy locked value
again: LDXR X10,[X20,#0] // read lock
CBNZ X10, again // check if it is 0 yet
STXR X11, X9, [X20] // attempt to store
BNEZ X9,again // branch if fails
Unlock:
STUR XZR, [X20,#0] // free lock
Static linking
Indirection table
Linker/loader code
Dynamically
mapped code
Simple portable
instruction set for
the JVM
Compiles
Interprets
bytecodes of
bytecodes
“hot” methods
into native
code for host
machine
MOV X19,XZR // i = 0
for1tst:
CMP X19, X1 // compare X19 to X1 (i to n)
B.GE exit1 // go to exit1 if X19 ≥ X1 (i≥n)
ADDI X19,X19,#1 // i += 1
B for1tst // branch to test of outer loop
exit1:
1.5
0.5
0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT
1.5
0.5
0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT
2000
1500
1000
500
0
C/none C/O1 C/O2 C/O3 Java/int Java/JIT