Lecture 7
Lecture 7
Lecture 7
These slides are based on the book: David A. Patterson, John L. Hennessy, Computer Organization and Design
LESS THAN COMPARISONS
0x12340010 array[4]
0x1234800C array[3]
0x12348008 array[2]
0x12348004 array[1]
0x12348000 array[0]
ARRAYS
// high-level code
int array[5];
array[0] = array[0] * 2;
array[1] = array[1] * 2;
// high-level code
int array[1000]; int i;
6-<104>
PROCEDURE CALLS
Definitions
• Caller: calling procedure (in this case, main)
• Callee: called procedure (in this case, sum)
High-level code
void main()
{
int y;
y = sum(42, 7);
...
}
MIPS conventions:
• Call procedure: jump and link (jal)
• Return from procedure: jump register (jr)
• Argument values: $a0 - $a3
• Return value: $v0
PROCEDURE CALLS
void simple() {
0x00401020 simple: jr $ra
return;
}
void simple() {
0x00401020 simple: jr $ra
return;
}
jal: jumps to simple and saves PC+4 in the return address register ($ra).
In this case, $ra = 0x00400204 after jal executes.
jr $ra: jumps to address in $ra, in this case 0x00400204.
INPUT ARGUMENTS AND RETURN VALUES
MIPS conventions:
• Argument values: $a0 - $a3
• Return value: $v0
INPUT ARGUMENTS AND RETURN VALUES
High-level code
int main()
{
int y;
...
y = diffofsums(2, 3, 4, 5); // 4 arguments
...
}
main:
...
addi $a0, $0, 2 # argument 0 = 2
addi $a1, $0, 3 # argument 1 = 3
addi $a2, $0, 4 # argument 2 = 4
addi $a3, $0, 5 # argument 3 = 5
jal diffofsums # call procedure
add $s0, $v0, $0 # y = returned value
...
# $s0 = result
diffofsums:
add $t0, $a0, $a1 # $t0 = f + g
add $t1, $a2, $a3 # $t1 = h + i
sub $s0, $t0, $t1 # result = (f + g) - (h + i)
add $v0, $s0, $0 # put return value in $v0
jr $ra # return to caller
INPUT ARGUMENTS AND RETURN VALUES
MIPS assembly code
# $s0 = result
diffofsums:
add $t0, $a0, $a1 # $t0 = f + g
add $t1, $a2, $a3 # $t1 = h + i
sub $s0, $t0, $t1 # result = (f + g) - (h + i)
add $v0, $s0, $0 # put return value in $v0
jr $ra # return to caller
FC ? $sp FC ? FC ? $sp
F8 F8 $s0 F8
stack frame
F4 F4 $t0 F4
F0 F0 $t1 $sp F0
Preserved Nonpreserved
Callee-Saved Caller-Saved
$s0 - $s7 $t0 - $t9
29