Instructions
Lars Ailo Bongo <larsab@cs.uit.no>
inf-2200, fall 2022
29.08.22
A golden age for computer architecture
Instruction Set
Chapter 2 — Instructions: Language of the Computer — 3
§2.1 Introduction
The MIPS Instruction Set
Chapter 2 — Instructions: Language of the Computer — 4
Levels of Program Code
Chapter 1 — Computer Abstractions and Technology — 15
Mandatory assignment 1
Arithmetic Operations
add a, b, c # a gets b + c
Chapter 2 — Instructions: Language of the Computer — 16
§2.2 Operations of the Computer Hardware
Arithmetic Example
f = (g + h) - (i + j);
add t0, g, h # temp t0 = g + h�add t1, i, j # temp t1 = i + j�sub f, t0, t1 # f = t0 - t1
Chapter 2 — Instructions: Language of the Computer — 17
Register Operands
Chapter 2 — Instructions: Language of the Computer — 18
§2.3 Operands of the Computer Hardware
Register Operand Example
f = (g + h) - (i + j);
add $t0, $s1, $s2�add $t1, $s3, $s4�sub $s0, $t0, $t1
Chapter 2 — Instructions: Language of the Computer — 19
Memory Operands
Chapter 2 — Instructions: Language of the Computer — 20
Memory Operand Example 1
g = h + A[8];
lw $t0, 32($s3) # load word�add $s1, $s2, $t0
Chapter 2 — Instructions: Language of the Computer — 21
offset
base register
Memory Operand Example 2
A[12] = h + A[8];
lw $t0, 32($s3) # load word�add $t0, $s2, $t0�sw $t0, 48($s3) # store word
Chapter 2 — Instructions: Language of the Computer — 22
Registers vs. Memory
Chapter 2 — Instructions: Language of the Computer — 23
Immediate Operands
addi $s3, $s3, 4
addi $s2, $s1, -1
Chapter 2 — Instructions: Language of the Computer — 25
The Constant Zero
add $t2, $s1, $zero
Chapter 2 — Instructions: Language of the Computer — 26
Unsigned Binary Integers
Chapter 2 — Instructions: Language of the Computer — 27
§2.4 Signed and Unsigned Numbers
2s-Complement Signed Integers
Chapter 2 — Instructions: Language of the Computer — 28
2s-Complement Signed Integers
Chapter 2 — Instructions: Language of the Computer — 29
Signed Negation
Chapter 2 — Instructions: Language of the Computer — 30
Sign Extension
Chapter 2 — Instructions: Language of the Computer — 31
Representing Instructions
Chapter 2 — Instructions: Language of the Computer — 32
§2.5 Representing Instructions in the Computer
Hexadecimal
Chapter 2 — Instructions: Language of the Computer — 33
0 | 0000 | 4 | 0100 | 8 | 1000 | c | 1100 |
1 | 0001 | 5 | 0101 | 9 | 1001 | d | 1101 |
2 | 0010 | 6 | 0110 | a | 1010 | e | 1110 |
3 | 0011 | 7 | 0111 | b | 1011 | f | 1111 |
MIPS R-format Instructions
Chapter 2 — Instructions: Language of the Computer — 34
op
rs
rt
rd
shamt
funct
6 bits
6 bits
5 bits
5 bits
5 bits
5 bits
R-format Example
add $t0, $s1, $s2
Chapter 2 — Instructions: Language of the Computer — 35
special
$s1
$s2
$t0
0
add
0
17
18
8
0
32
000000
10001
10010
01000
00000
100000
000000100011001001000000001000002 = 0232402016
op
rs
rt
rd
shamt
funct
6 bits
6 bits
5 bits
5 bits
5 bits
5 bits
MIPS I-format Instructions
Chapter 2 — Instructions: Language of the Computer — 36
op
rs
rt
constant or address
6 bits
5 bits
5 bits
16 bits
Stored Program Computers
Chapter 2 — Instructions: Language of the Computer — 37
The BIG Picture
Logical Operations
Chapter 2 — Instructions: Language of the Computer — 38
Operation | C | Java | MIPS |
Shift left | << | << | sll |
Shift right | >> | >>> | srl |
Bitwise AND | & | & | and, andi |
Bitwise OR | | | | | or, ori |
Bitwise NOT | ~ | ~ | nor |
§2.6 Logical Operations
Shift Operations
Chapter 2 — Instructions: Language of the Computer — 39
op
rs
rt
rd
shamt
funct
6 bits
6 bits
5 bits
5 bits
5 bits
5 bits
AND Operations
and $t0, $t1, $t2
Chapter 2 — Instructions: Language of the Computer — 40
OR Operations
or $t0, $t1, $t2
Chapter 2 — Instructions: Language of the Computer — 41
NOT Operations
nor $t0, $t1, $zero
Chapter 2 — Instructions: Language of the Computer — 42
0000 0000 0000 0000 0011 1100 0000 0000
$t1
1111 1111 1111 1111 1100 0011 1111 1111
$t0
Register 0: always read as zero
Conditional Operations
Chapter 2 — Instructions: Language of the Computer — 43
§2.7 Instructions for Making Decisions
Compiling If Statements
if (i==j) f = g+h;�else f = g-h;
bne $s3, $s4, Else� add $s0, $s1, $s2� j Exit�Else: sub $s0, $s1, $s2�Exit: …
Chapter 2 — Instructions: Language of the Computer — 44
Assembler calculates addresses
Compiling Loop Statements
while (save[i] == k) i += 1;
Loop: sll $t1, $s3, 2� add $t1, $t1, $s6� lw $t0, 0($t1)� bne $t0, $s5, Exit� addi $s3, $s3, 1� j Loop�Exit: …
Chapter 2 — Instructions: Language of the Computer — 45
Basic Blocks
Chapter 2 — Instructions: Language of the Computer — 46
More Conditional Operations
slt $t0, $s1, $s2 # if ($s1 < $s2)�bne $t0, $zero, L # branch to L
Chapter 2 — Instructions: Language of the Computer — 47
Branch Instruction Design
Chapter 2 — Instructions: Language of the Computer — 48
Signed vs. Unsigned
Chapter 2 — Instructions: Language of the Computer — 49
Procedure Calling
Chapter 2 — Instructions: Language of the Computer — 50
x = lead_example(1, 2, 3, 4);
…
int leaf_example (int g, h, i, j)�{ int f;� f = (g + h) - (i + j);� return f;�}
Register Usage
Chapter 2 — Instructions: Language of the Computer — 51
Procedure Call Instructions
jal ProcedureLabel
jr $ra
Chapter 2 — Instructions: Language of the Computer — 52
Leaf Procedure Example
int leaf_example (int g, h, i, j)�{ int f;� f = (g + h) - (i + j);� return f;�}
Chapter 2 — Instructions: Language of the Computer — 53
Leaf Procedure Example
Chapter 2 — Instructions: Language of the Computer — 54
Non-Leaf Procedures
Chapter 2 — Instructions: Language of the Computer — 55
Non-Leaf Procedure Example
int fact (int n)�{ � if (n < 1) return f;� else return n * fact(n - 1);�}
Chapter 2 — Instructions: Language of the Computer — 56
Non-Leaf Procedure Example
Chapter 2 — Instructions: Language of the Computer — 57
Local Data on the Stack
Chapter 2 — Instructions: Language of the Computer — 58
Memory Layout
Chapter 2 — Instructions: Language of the Computer — 59
Character Data
Chapter 2 — Instructions: Language of the Computer — 60
§2.9 Communicating with People
Byte/Halfword Operations
lb rt, offset(rs) lh rt, offset(rs)
lbu rt, offset(rs) lhu rt, offset(rs)
sb rt, offset(rs) sh rt, offset(rs)
Chapter 2 — Instructions: Language of the Computer — 61
String Copy Example
void strcpy (char x[], char y[])�{ int i;� i = 0;� while ((x[i]=y[i])!='\0')� i += 1;�}
Chapter 2 — Instructions: Language of the Computer — 62
String Copy Example
Chapter 2 — Instructions: Language of the Computer — 63
32-bit Constants
lui rt, constant
Chapter 2 — Instructions: Language of the Computer — 64
§2.10 MIPS Addressing for 32-Bit Immediates and Addresses
Branch Addressing
Chapter 2 — Instructions: Language of the Computer — 65
op
rs
rt
constant or address
6 bits
5 bits
5 bits
16 bits
Jump Addressing
Chapter 2 — Instructions: Language of the Computer — 66
op
address
6 bits
26 bits
Target Addressing Example
Chapter 2 — Instructions: Language of the Computer — 67
Loop: sll $t1, $s3, 2 | 80000 | 0 | 0 | 19 | 9 | 4 | 0 |
add $t1, $t1, $s6 | 80004 | 0 | 9 | 22 | 9 | 0 | 32 |
lw $t0, 0($t1) | 80008 | 35 | 9 | 8 | 0 | ||
bne $t0, $s5, Exit | 80012 | 5 | 8 | 21 | 2 | ||
addi $s3, $s3, 1 | 80016 | 8 | 19 | 19 | 1 | ||
j Loop | 80020 | 2 | 20000 | ||||
Exit: … | 80024 | | |||||
Branching Far Away
beq $s0,$s1, L1
↓
bne $s0,$s1, L2� j L1�L2: …
Chapter 2 — Instructions: Language of the Computer — 68
Addressing Mode Summary
Chapter 2 — Instructions: Language of the Computer — 69
Concluding Remarks
1. Simplicity favors regularity
2. Smaller is faster
3. Make the common case fast
4. Good design demands good compromises
Chapter 2 — Instructions: Language of the Computer — 70
§2.22 Concluding Remarks
Digital Technology Innovation Lab
Concept
Team
Validation
Prototype
Finance
Tech Startup
=
Sep
Jun
Mar
Dec