Lecture 3: MIPS Instruction Set
Lecture 3: MIPS Instruction Set
• Today’s topic:
1
Memory Operands
int a, b, c, d[10]
…
Memory
Base address
3
Immediate Operands
destination register
source address
lw $t0, 8($t3)
any register
a constant that is added to the register in brackets
5
Example
Convert to assembly:
• Binary 001000112 = 1 x 25 + 1 x 21 + 1 x 20
Dec Binary Hex Dec Binary Hex Dec Binary Hex Dec Binary Hex
0 0000 00 4 0100 04 8 1000 08 12 1100 0c
1 0001 01 5 0101 05 9 1001 09 13 1101 0d
2 0010 02 6 0110 06 10 1010 0a 14 1110 0e
3 0011 03 7 0111 07 11 1011 0b 15 1111 0f
7
Instruction Formats
9
Control Instructions
• Unconditional branch:
j L1
jr $s0 (useful for large case statements and big jumps)
Convert to assembly:
if (i == j)
f = g+h;
else
f = g-h;
10
Control Instructions
• Unconditional branch:
j L1
jr $s0 (useful for large case statements and big jumps)
Convert to assembly:
if (i == j) bne $s3, $s4, Else
f = g+h; add $s0, $s1, $s2
else j Exit
f = g-h; Else: sub $s0, $s1, $s2
11
Exit:
Example
Convert to assembly:
while (save[i] == k)
i += 1;
12
Example
Convert to assembly:
Loop: sll $t1, $s3, 2
while (save[i] == k) add $t1, $t1, $s6
i += 1; lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
i and k are in $s3 and $s5 and j Loop
base of array save[] is in $s6 Exit:
13
Procedures
parameters (arguments) are placed where the callee can see them
control is transferred to the callee
acquire storage resources for callee
execute the procedure
place result value where caller can access it
return control to caller
14
Registers
• A special register (storage not part of the register file) maintains the
address of the instruction currently being executed – this is the
program counter (PC)
call Proc B
Proc B’s values
…
call Proc C
Proc C’s values …
… return
Stack grows return
this way return
Low address 17
Storage Management on a Call/Return
• A new procedure must create space for all its variables on the stack
• Before executing the jal, the caller must save relevant values in
$s0-$s7, $a0-$a3, $ra, temps into its own stack space
• After the callee creates stack space, it updates the value of $sp
• Once the callee finishes, it copies the return value into $v0, frees
up stack space, and $sp is incremented
• On return, the caller may bring in its stack values, ra, temps into registers
• The responsibility for copies between stack and registers may fall
upon either the caller or the callee
18
Example 1
19
Example 1
21
Example 2
Stack
Text (instructions)
23
Title
• Bullet
24