3.Ch2.MIPS ISA
3.Ch2.MIPS ISA
3.Ch2.MIPS ISA
• Today’s topic:
MIPS instructions
Procedure call/return
C code: a=b+c;
C code a = b + c + d + e;
translates into the following assembly code:
add a, b, c add a, b, c
add a, a, d or add f, d, e
add a, a, e add a, a, f
C code f = (g + h) – (i + j);
C code f = (g + h) – (i + j);
translates into the following assembly code:
int a, b, c, d[10]
…
Memory
Base address
lw $t0, 8($t3)
any register
a constant that is added to the register in brackets
Convert to assembly:
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
Yosef A. Abdulmoghni Al-Razi University 18
Instruction Formats
• Unconditional branch:
j L1
jr $s0
Convert to assembly:
if (i == j)
f = g+h;
else
f = g-h;
Yosef A. Abdulmoghni Al-Razi University 21
Control Instructions
• Unconditional branch:
j L1
jr $s0
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
Exit:
Yosef A. Abdulmoghni Al-Razi University 22
Example
Convert to assembly:
while (save[i] == k)
i += 1;
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:
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
• 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
Yosef A. Abdulmoghni Al-Razi University 28
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
Yosef A. Abdulmoghni Al-Razi University 29
Example 1
More instructions
lb lbu lui
lh lhu sb/sh
slt/slti sltu/sltiu
while (save[i] == k)
i += 1;
Loop: starts at 80000
while (save[i] == k)
i += 1;
Loop: starts at 80000