ARM Computer Organization-Chapter02
ARM Computer Organization-Chapter02
Edition
The Hardware/Software Interface
Chapter 2
Instructions: Language
of the Computer
§2.1 Introduction
Instruction Set
n The repertoire of instructions of a
computer
n Different computers have different
instruction sets
n But with many aspects in common
n Early computers had very simple
instruction sets
n Simplified implementation
n Many modern computers also have simple
instruction sets
n Range: 0 to +2n – 1
n Example
n 0000 0000 0000 0000 0000 0000 0000 10112
= 0 + … + 1×23 + 0×22 +1×21 +1×20
= 0 + … + 8 + 0 + 2 + 1 = 1110
n Using 32 bits
n 0 to +4,294,967,295
x + x = 1111...1112 = -1
x + 1 = -x
n Example: negate +2
n +2 = 0000 0000 … 0010two
n –2 = 1111 1111 … 1101two + 1
= 1111 1111 … 1110two
n LEGv8 instructions
n Encoded as 32-bit instruction words
n Small number of formats encoding operation code
(opcode), register numbers, …
n Regularity!
n Instruction fields
n opcode: operation code
n Rm: the second register source operand
n shamt: shift amount (00000 for now)
n Rn: the first register source operand
n Rd: the register destination
ADD X9,X20,X21
1112ten 21ten 0ten 20ten 9ten
8B15028916
n Load/store instructions
n Rn: base register
n address: constant offset from contents of base register (+/- 32
doublewords)
n Rt: destination (load) or source (store) register number
n Immediate instructions
n Rn: source register
n Rd: destination register
n CBZ register, L1
n if (register == 0) branch to instruction labeled L1;
n CBNZ register, L1
n if (register != 0) branch to instruction labeled L1;
n B L1
n branch unconditionally to instruction labeled L1;
Chapter 2 — Instructions: Language of the Computer — 30
Compiling If Statements
n C code:
if (i==j) f = g+h;
else f = g-h;
n f, g, … in X19, X20, …
n Compiled LEGv8 code:
SUB X9,X22,X23
CBNZ X9,Else
ADD X19,X20,X21
B Exit
Else: SUB X19,X20,x21
Exit: … Assembler calculates addresses
Chapter 2 — Instructions: Language of the Computer — 31
Compiling Loop Statements
n C code:
while (save[i] == k) i += 1;
n i in X22, k in X24, address of save in x25
n 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 — 32
Basic Blocks
n A basic block is a sequence of instructions
with
n No embedded branches (except at end)
n No branch targets (except at beginning)
n Argument n in X0
n 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
n CB-type
n CBNZ X19, Exit // go to Exit if X19 != 0
181 Exit 19
8 bits 19 bits 5 bits
n 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
n 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