Professional Documents
Culture Documents
❑ Quantitative design
➢ How many GPR & trade-off? 32 5. The role of compilers
➢ What is the GPR width & trade-off? 32 bit
MIPS storage model summary
Address
Program Counter 0 M[0]
32-bit memory address
4 M[1]
of the current instruction
8 M[2]
⁞ M[3]
**Note** r0=0 M[4]
r1
r2
General Purpose
Register File
32 32-bit words M[N-1]
named r0...r31
Memory
Alignment is required
Byte addressable
32-bit address (4 GB)
Support both endianness’s
1. Data Storage
❖ Design principles #2, #3: “smaller is faster”, “make the common case fast”
❑ Architectural representative
➢ Interface btw HLL and machine code
➢ Human-readable format of instructions
❑ Semantics:
➢ [rt] ← op{[rs], sign-extend(imm)}
➢ Pseudo-instruction: move = add instruction with zero immediate
➢ register zero ($0 or $zero) is hardwired to 0 (“make the common case fast”)
Notes:
The and instruction has an immediate version, andi
Logical Operations: Bitwise OR
Opcode: or ( bitwise OR )
Bitwise operation that that places a 1 in the result if
either operand bit is 1
Example: or $t0, $t1, $t2
❑ Strange Fact 2:
➢ There is no nori, but there is xori in MIPS
➢ Why?
Data transfer instructions
❑ Encoding:
op rs rt imm
6 bits 5 bits 5 bits 16-bit signed constant
❑ Semantics
➢ lw rt, imm(rs) # rt ← Mem[rs+imm], load a word from Memory
➢ sw rt, imm(rs) # rt → Mem[rs+imm], store a word in Memory
➢ In load-store architecture, lw and sw are the only mechanism for accessing
memory values.
➢ Memory address = base (stored in register) + offset (stored in instructions):
base addressing mode, a special case of displacement addressing.
❑ MIPS processor supports load & store instructions for bytes and
halfwords (“make the common case fast”).
➢ Load expands a memory data to fit into a 32-bit register: lb = load byte, lbu
= load byte unsigned, lh = load half, lhu = load half unsigned
➢ Store reduces a 32-bit register to fit in memory: sb = store byte, sh = store
halfword. Why don’t we need sbu and shu?
A load byte example
❑ Suppose $t0 initially contains 0x23456789. After the following
code runs, what is the value of $s0
sw $t0, 0($0)
lb $s0, 1($0)
a. in a big-endian system?
b. in a little-endian system?
❑ Solution:
a. 0x00000045
b. 0x00000067
Big-Endian Little-Endian
Word
Byte Address 0 1 2 3 Address 3 2 1 0 Byte Address
Data Value 23 45 67 89 0 23 45 67 89 Data Value
MSB LSB MSB LSB
32-bit constants
❑ Most constants are small
➢ 16-bit immediate is sufficient
❑ Example:
➢ What is the MIPS assembly code to load this 32-bit constant into $s0?
0000 0000 0111 1101 0000 1001 0000 0000
lui $s0, 61 0000 0000 0111 1101 0000 0000 0000 0000
ori $s0, $s0, 2304 0000 0000 0111 1101 0000 1001 0000 0000
(Conditional) Branch Instructions
❑ Assembly (e.g., branch if equal).
➢ BEQ rs rt target # assembly uses a symbolic target address
❑ Encoding
op rs rt imm
6 bits 5 bits 5 bits 16-bit signed instruction offset
❑ Semantics
➢ target = PC + 4 + sign-extend(imm) x 4;
if [rs] == [rt] then PC ← target else PC ← PC + 4;
Instruction PC+4
imm
16 bits
32 bits
Sign extend
+
➢ This addressing mode is called PC-relative. How far can you jump?
# of instructions to add
opcode 4
to (or subtract from) rs 9 (first operand)
the PC, starting at the
rt 0 (second operand)
instruction following
the branch immediate ??? (in base 10)
❑ Encoding
op imm
6 bits 26-bit signed instruction offset
❑ Semantics
➢ target = (PC+4)[31:28] x228 || zero-extend(imm)x4;
PC ← target;
Instruction PC+4
imm
26 bits 4 bits
➢ This addressing mode is called Pseudodirect. How far can you jump?
❑ Variations: jal, jr
IF statement (1/2)
C Statement to translate Variables Mapping
if (i == j) f ➔ $s0
f = g + h; g ➔ $s1
h ➔ $s2
i ➔ $s3
j ➔ $s4
true false
i==j? bne $s3, $s4, Else
add $s0, $s1, $s2
Else: j Exit
f = g+h f = g-h Else: sub $s0, $s1, $s2
Exit:
Key concept:
Any form of loop can be written in assembly with the
help of conditional branches and jumps.
Loop: if (j != k)
goto Exit;
while (j == k)
i = i+1;
i = i + 1;
goto Loop;
Exit:
Loops (2/2)
lw $t0, 4 ($s4)
sw $t2, -6 ($at)
Displacement addressing
Summary (3)
MIPS assembly language
Category Instruction Example Meaning Comments
add add $s1, $s2, $s3 $s1 = $s2 + $s3 Three operands; data in registers
Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers
add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constants
load w ord lw $s1, 100($s2) $s1 = Memory[$s2 + 100]Word from memory to register
store w ord sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory
Data transfer load byte lb $s1, 100($s2) $s1 = Memory[$s2 + 100]Byte from memory to register
store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory
load upper 16 Loads constant in upper 16 bits
lui $s1, 100 $s1 = 100 * 2
immediate
branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to Equal test; PC-relative branch
PC + 4 + 100
branch on not equal bne $s1, $s2, 25 if ($s1 != $s2) go to Not equal test; PC-relative
PC + 4 + 100
Conditional
branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; Compare less than; for beq, bne
else $s1 = 0
set less than slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; Compare less than constant
immediate else $s1 = 0
jump j 2500 go to 10000 Jump to target address
Uncondi- jump register jr $ra go to $ra For sw itch, procedure return
tional jump jump and link jal 2500 $ra = PC + 4; go to 10000 For procedure call