Chapter 2:instructions: Language of The Computer
Chapter 2:instructions: Language of The Computer
Chapter 2
Instructions: Language
of the Computer
Instruction Set
Simplified implementation
add a, b, c # a gets b + c
All arithmetic operations have this form
Design Principle 1: Simplicity favours
regularity
Arithmetic Operations
Arithmetic Example
C code:
f = (g + h) - (i + j);
# temp t0 = g + h
# temp t1 = i + j
# f = t0 - t1
Assembler names
Register Operands
C code:
f = (g + h) - (i + j);
f, , j in $s0, , $s4
Memory Operands
Big here means the largest address, the least significant byte at the
largest address. Byte address 0 1 2 3. The MSB at address 0 the LSB
at address 3
Little Endian:
C code:
g = h + A[8];
g in $s1, h in $s2, base address of A in $s3
lw $t0, 32($s3)
add $s1, $s2, $t0
offset
# load word
C code:
A[12] = h + A[8];
h in $s2, base address of A in $s3
Immediate Operands
Cannot be overwritten
x n2 2
n2
x1 2 x 0 2
1
Range: 0 to +2n 1
Example
n 1
Using 32 bits
0 to +4,294,967,295
Chapter 2 Instructions: Language of the Computer 15
n2
x n2 2
x1 2 x 0 2
1
Range: 2n 1 to +2n 1 1
Example
n 1
Using 32 bits
2,147,483,648 to +2,147,483,647
Chapter 2 Instructions: Language of the Computer 16
Signed Negation
Complement means 1 0, 0 1
x x 1111...111 2 1
x 1 x
Example: negate +2
Sign Extension
Representing Instructions
MIPS instructions
Register numbers
op
rs
rt
rd
shamt
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
Instruction fields
R-format Example
op
rs
rt
rd
shamt
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
$s1
$s2
$t0
add
17
18
32
000000
10001
10010
01000
00000
100000
000000100011001001000000001000002 = 0232402016
Chapter 2 Instructions: Language of the Computer 22
Hexadecimal
Base 16
0000
0100
1000
1100
0001
0101
1001
1101
0010
0110
1010
1110
0011
0111
1011
1111
rs
rt
constant or address
6 bits
5 bits
5 bits
16 bits
op
Instructions represented in
binary, just like data
Instructions and data stored
in memory
Programs can operate on
programs
Standardized ISAs
Java
MIPS
Shift left
<<
<<
sll
Shift right
>>
>>>
srl
Bitwise AND
&
&
and, andi
Bitwise OR
or, ori
Bitwise NOT
nor
Logical Operations
Shift Operations
rs
rt
rd
shamt
funct
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
op
AND Operations
$t1
$t0
OR Operations
$t1
$t0
NOT Operations
Change 0 to 1, and 1 to 0
a NOR b == NOT ( a OR b )
Register 0: always
read as zero
$t1
$t0
Conditional Operations
j L1
Compiling If Statements
C code:
if (i==j) f = g+h;
else f = g-h;
f, g, in $s0, $s1,
C code:
while (save[i] == k) i += 1;
$t1, $s3, 2
#t1=i*4 due
to byte addressing
add
of save[i]
lw
bne
addi
j
Exit:
$t0, 0($t1)
$t0, $s5, Exit
$s3, $s3, 1
Loop
Chapter 2 Instructions: Language of the Computer 33
Basic Blocks
Otherwise, set to 0
1 < +1 $t0 = 1
# unsigned
Steps required
1.
2.
3.
4.
5.
6.
Procedure Calling
Register Usage
C code:
int leaf_example (int g, h, i, j)
{ int f;
f = (g + h) - (i + j);
return f;
}
Arguments g, , j in $a0, , $a3
f in $s0 (hence, need to save $s0 on stack)
Result in $v0
MIPS code:
leaf_example:
addi $sp, $sp, -4
sw
$s0, 0($sp)
add $t0, $a0, $a1
add $t1, $a2, $a3
sub $s0, $t0, $t1
add $v0, $s0, $zero
lw
$s0, 0($sp)
addi $sp, $sp, 4
jr
$ra
Procedure body
Result
Restore $s0
Return
Non-Leaf Procedures
C code:
int fact (int n)
{
if (n < 1) return f; # f=1 (0!=1)
else return n * fact(n - 1);
}
Argument n in $a0
Result in $v0
MIPS code:
fact:
addi
sw
sw
slti
beq
addi
addi
jr
L1: addi
jal
lw
lw
addi
mul
jr
$sp,
$ra,
$a0,
$t0,
$t0,
$v0,
$sp,
$ra
$a0,
fact
$a0,
$ra,
$sp,
$v0,
$ra
$sp, -8
4($sp)
0($sp)
$a0, 1
$zero, L1
$zero, 1
$sp, 8
$a0, -1
0($sp)
4($sp)
$sp, 8
$a0, $v0
#
#
#
#
#
#
#
#
#
#
#
#
#
#
if so, result is 1
pop 2 items from stack
and return
else decrement n
recursive call
restore original n
and return address
pop 2 items from stack
multiply to get result
and return
Character Data
95 graphic, 33 control
ASCII, +96 more graphic characters
Byte/Halfword Operations
lb rt, offset(rs)
sb rt, offset(rs)
lh rt, offset(rs)
sh rt, offset(rs)
lui $s0, 61
ori $s0, $s0, 2304 0000 0000 0111 1101 0000 1001 0000 0000
32-bit Constants
Branch Addressing
rs
rt
constant or address
6 bits
5 bits
5 bits
16 bits
PC-relative addressing
Jump Addressing
address
6 bits
26 bits
Loop: sll
$t1, $s3, 2
80000
19
add
80004
22
32
lw
$t0, 0($t1)
80008
35
bne
80012
21
80016
19
19
80020
Exit:
Loop
20000
80024
bne $s0,$s1, L2
j L1
L2:
Static linking
C Sort Example
$s2, $a0
$s3, $a1
$s0, $zero
$t0, $s0, $s3
$t0, $zero, exit1
$s1, $s0, 1
$t0, $s1, 0
$t0, $zero, exit2
$t1, $s1, 2
$t2, $s2, $t1
$t3, 0($t2)
$t4, 4($t2)
$t0, $t4, $t3
$t0, $zero, exit2
$a0, $s2
$a1, $s1
swap
$s1, $s1, 1
for2tst
$s0, $s0, 1
for1tst
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
Move
params
Outer loop
Inner loop
Pass
params
& call
Inner loop
Outer loop
addi $sp,$sp, 20
sw $ra, 16($sp)
sw $s3,12($sp)
sw $s2, 8($sp)
sw $s1, 4($sp)
sw $s0, 0($sp)
#
#
#
#
#
#
#
#
#
#
#
#
#
#
Lessons Learnt
move $t0,$zero
loop1: sll $t1,$t0,2
add $t2,$a0,$t1
move $t0,$a0
# p = & array[0]
sll $t1,$a1,2
# $t1 = size * 4
add $t2,$a0,$t1 # $t2 =
#
&array[size]
loop2: sw $zero,0($t0) # Memory[p] = 0
addi $t0,$t0,4 # p = p + 4
slt $t3,$t0,$t2 # $t3 =
#(p<&array[size])
bne $t3,$zero,loop2 # if ()
# goto loop2
# i = 0
# $t1 = i * 4
# $t2 =
#
&array[i]
sw $zero, 0($t2) # array[i] = 0
addi $t0,$t0,1
# i = i + 1
slt $t3,$t0,$a1 # $t3 =
#
(i < size)
bne $t3,$zero,loop1 # if ()
# goto loop1
MIPS
1985
1985
Instruction size
32 bits
32 bits
Address space
32-bit flat
32-bit flat
Data alignment
Aligned
Aligned
15 32-bit
31 32-bit
Memory
mapped
Memory
mapped
Date announced
Instruction Encoding
Further evolution
Pentium 4 (2001)
New microarchitecture
Added SSE2 instructions
Chapter 2 Instructions: Language of the Computer 70
And further
Register
Register
Register
Immediate
Register
Memory
Memory
Register
Memory
Immediate
Address in register
Address = Rbase + displacement
Address = Rbase + 2scale Rindex (scale = 0, 1, 2, or 3)
Address = Rbase + 2scale Rindex + displacement
Chapter 2 Instructions: Language of the Computer 73
Variable length
encoding
Operand length,
repetition, locking,
Implementing IA-32
(Intel Architecture 32)
Simple instructions: 11
Complex instructions: 1many
ARM v8 Instructions
Fallacies
Fallacies
Pitfalls
Increment by 4, not by 1!
Concluding Remarks
Design principles
1.
2.
3.
4.
Layers of software/hardware
c.f. x86
Concluding Remarks
Instruction class
MIPS examples
SPEC2006 Int
SPEC2006 FP
Arithmetic
16%
48%
Data transfer
35%
36%
Logical
12%
4%
Cond. Branch
34%
8%
Jump
j, jr, jal
2%
0%