Chapter 2 Instructions Language of The Computer
Chapter 2 Instructions Language of The Computer
Instructions: Language
of the Computer
Introduction
Computer language
Words: instructions
Vocabulary: instruction set
Similar for all, like regional dialect?
instruction sets
7 6 5 4 3 2 1 0
MSB LSB
Big Endian
The byte order put the byte whose address is “xx…x000” at the
0 1 2 3 4 5 6 7
MSB LSB
. .
. .
4n+3 78 4n+3 12
4n+2 56 4n+2 34
4n+1 34 4n+1 56
4n+0 12 4n+0 78
. .
. .
Big Endian Little Endian
Since we want to make the common case fast, fast having restricted alignment
is often a better choice, unless compatibility is an issue.
Increasing byte
address
7 6 5 4 3 2 1 0
4
3 (MSB) 2 1 0 (LSB) Little-endian byte order
4
0 (LSB) 1 2 3 (MSB) Big-endian byte order
R: Register, M: Memory
Register reg
Direct addr
Indirect reg
“base”
Displacement reg imm address
+
offset
all your base are belong to us
Memory reg
Indirect
index
How Many Addressing Modes?
Simple addressing modes
Register, Direct, Immediate
Register indirect addressing modes
Register Indirect, Displacement, Indexed, Memory Indirect
Advanced addressing modes
Auto-increment, Auto-decrement, Scaled
Numbered 0 to 31
32-bit data called a “word”
Assembler names
$t0, $t1, …, $t9 for temporary values
$s0, $s1, …, $s7 for saved variables
Design Principle 2: Smaller is faster
e.g. main memory: millions of locations
operand/result of an operation
But making different pieces of software work together is easier if
Special usage:
R28: pointer register
Range: 0 ~ +2n – 1
§2.4 Signed and Unsigned Numbers
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 ~ +4,294,967,295
3 well-known methods
Sign and Magnitude
1’s complement
2’s complement
x + x = 1111...1112 = −1
x + 1 = −x
Example: negate +2
+2 = 0000 0000 … 00102
–2 = 1111 1111 … 11012 + 1
= 1111 1111 … 11102
:
Signed (A2) Îlb $s3, 0($s0)
1999
Unsigned (A1) Îlbu $s3, 0($s0) 2000 1111 1111
1111 1111
2001
1111 1111
1111 1111
Assume
$s0 = 2000
MIPS instructions
Encoded as 32-bit instruction words
Small number of formats encoding operation code (opcode),
register numbers, …
Regularity!
Register numbers (5-bit representation)
$t0 – $t7 are reg’s 8 – 15
$t8 – $t9 are reg’s 24 – 25
$s0 – $s7 are reg’s 16 – 23
Instruction fields
op: operation code (opcode)
rs: first source register number
rt: second source register number
rd: destination register number
shamt: shift amount (00000 for now)
funct: function code (extends opcode)
0 17 18 8 0 32
000000100011001001000000001000002 = 0232402016
nor
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
lhi $s0, 61 0000 0000 0111 1101 0000 0000 0000 0000
ori $s0, $s0, 2304 0000 0000 0111 1101 0000 1001 0000 0000
op rs rt constant or address
6 bits 5 bits 5 bits 16 bits
PC-relative addressing
Target address = PC + offset × 4
PC already incremented by 4 by this time
Chapter 2 — Instructions: Language of the Computer — 78
Jump Addressing
Jump (j and jal) targets could be
anywhere in text segment
Encode full address in instruction
op address
6 bits 26 bits
Static linking
Indirection table
Linker/loader code
Dynamically
mapped code
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