MIPS Functions
-
Upload
emma-dorsey -
Category
Documents
-
view
58 -
download
0
description
Transcript of MIPS Functions
![Page 1: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/1.jpg)
MIPS Functions
![Page 2: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/2.jpg)
Questions
A MIPS function is called by the jal instruction, which does two things: 1) going to the address of the first instruction in the function, 2) passing the arguments in $a0 to $a3.
![Page 3: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/3.jpg)
Questions
A MIPS function must be ended by the jr $ra instruction.
![Page 4: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/4.jpg)
Questions
MIPS functions are stored in a different part in the memory and not in the same part as the main function.
![Page 5: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/5.jpg)
Questions
A MIPS function has explicitly declare a name along with the list of arguments to be passed to it, including the names and the types.
![Page 6: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/6.jpg)
Questions
A function in MIPS cannot have loops.
![Page 7: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/7.jpg)
Questions
The jal L1 instruction jumps to L1, and saves the address of L1 into $ra.
![Page 8: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/8.jpg)
Questions
Suppose $s0, $v0, and $a0 are holding 60, 0, and 35, respectively. After the program executes till p9L2, what will be the value in $v0?
jal p9L1j p9L2
p9L1: add $v0, $v0, $a0 blt $v0, $s0, p9L1jr $ra
p9L2:
(a) 100(b) 70(c) The program will never run to p9L2.(d) None of the above.
![Page 9: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/9.jpg)
04/19/2023 week04-3.ppt 9
MIPS Calling Conventions
• MIPS assembly follows the following convention in using registers• $a0 - $a3: four argument registers in which to pass parameters• $v0 - $v1: two value registers in which to return values• $ra: one return address register to return to the point of origin
![Page 10: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/10.jpg)
MIPS Conventions
• Quite often, our function needs to use some registers to do dome calculation. So we will modify the values of them.• We can use $t0-$t9 freely inside a function, because the caller does
not expect the values inside $t0-$t9 to stay the same after the function call.• But, the caller do expect the values in $s0 to $s7 to be the same after
a function call.
![Page 11: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/11.jpg)
MIPS Conventions
• So, just try to avoid using $s0 and $s7 inside a function whenever possible.• But what if do need it? Such occasions will arise…
![Page 12: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/12.jpg)
Stack• So, if we do have to use $s0 -
$s7, we MUST save it somewhere before entering the main part of the function, and restore it before we return (before we execute “jr $ra”).
• In MIPS, we save them in the stack.
• Stack is a part in the memory allocated for functions. It starts at 0x7ffffffc and grows down as we add more stuffs to it.
• Stack is “first in last out.”
![Page 13: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/13.jpg)
$sp
• The top address of the stack, the address of the first word that is storing value, is (should be) always stored in $sp.• So, adding a word into the stack (pushing a word onto the stack) is a
two-step thing, because you have to maintain the correctness of $sp:• addi $sp, $sp, -4• sw $s0, 0($sp)
![Page 14: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/14.jpg)
Suppose we want to
![Page 15: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/15.jpg)
Stack and $sp
• Suppose we want to store a/2 in $s0. • How do we get a/2?
• At the beginning, we do• addi $sp, $sp, -4• sw $s0, 0($sp)
• At the end, we do• lw $s0, 0($sp)• addi $sp, $sp, 4
![Page 16: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/16.jpg)
.dataA: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19
.text
.globl mainmain:
la $s7, Ali $s0, 0 #ili $s1, 0 #resli $s6, 9
loop: sll $t0, $s0, 2add $t0, $t0, $s7lw $a0, 0($t0)lw $a1, 4($t0)jal weirdfunadd $s1, $s1, $v0addi $s0, $s0, 2blt $s0, $s6, loop
done: li $v0,10syscall
weirdfun: addi $sp, $sp, -4sw $s0, 0($sp)
srl $s0, $a0, 1add $t0, $a0, $a0add $t0, $t0, $a1sub $t0, $t0, $s0
ori $v0, $t0, 0
lw $s0, 0($sp)addi $sp, $sp, 4
jr $ra
![Page 17: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/17.jpg)
Function calls inside a function
• What if we need to call another function inside a function? Will this work?
twofun:addi $sp, $sp, -4sw $s0, 0($sp)
jal addfunsrl $s0, $a0, 1sub $v0, $v0, $s0
lw $s0, 0($sp)addi $sp, $sp, 4
jr $ra
![Page 18: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/18.jpg)
Function calls inside a function• The problem is that the
value of $ra is changed whenever you use jal somelabel.• How to deal with it?
twofun:addi $sp, $sp, -4sw $s0, 0($sp)
jal addfunsrl $s0, $a0, 1sub $v0, $v0, $s0
lw $s0, 0($sp)addi $sp, $sp, 4
jr $ra
![Page 19: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/19.jpg)
The working versionstwofun1:
addi $sp, $sp, -4
sw $s0, 0($sp)
addi $sp, $sp, -4
sw $ra, 0($sp)
jal addfun
srl $s0, $a0, 1
sub $v0, $v0, $s0,
lw $ra, 0($sp)
addi $sp, $sp, 4
lw $s0, 0($sp)
addi $sp, $sp, 4
jr $ra
twofun2:addi $sp, $sp, -8sw $s0, 4($sp)sw $ra, 0($sp)
jal addfunsrl $s0, $a0, 1sub $v0, $v0, $s0,
lw $ra, 0($sp)lw $s0, 4($sp)addi $sp, $sp, 8
jr $ra
![Page 20: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/20.jpg)
Saving registers
• In case of nested function calls, before calling a function, to be safe, the caller should• save $t0-$t9• save $a0-$a3If such registers are needed later. and • save $raBecause $ra is going to be needed later and it will be changed
• The callee should • save $s0-s7
![Page 21: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/21.jpg)
04/19/2023 week04-3.ppt 21
MIPS Calling Conventions
• MIPS assembly follows the following convention in using registers• $a0 - $a3: four argument registers in which to pass parameters• $v0 - $v1: two value registers in which to return values• $ra: one return address register to return to the point of origin
![Page 22: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/22.jpg)
MIPS Conventions
• Quite often, our function needs to use some registers to do dome calculation. So we will modify the values of them.• We can use $t0-$t9 freely inside a function, because the caller does
not expect the values inside $t0-$t9 to stay the same after the function call.• But, the caller do expect the values in $s0 to $s7 to be the same after
a function call.
![Page 23: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/23.jpg)
MIPS Conventions
• So, just try to avoid using $s0 and $s7 inside a function whenever possible.• But what if do need it? Such occasions will arise…
![Page 24: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/24.jpg)
Stack• So, if we do have to use $s0 -
$s7, we MUST save it somewhere before entering the main part of the function, and restore it before we return (before we execute “jr $ra”).
• In MIPS, we save them in the stack.
• Stack is a part in the memory allocated for functions. It starts at 0x7ffffffc and grows down as we add more stuffs to it.
• Stack is “first in last out.”
![Page 25: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/25.jpg)
$sp
• The top address of the stack, the address of the first word that is storing value, is (should be) always stored in $sp.• So, adding a word into the stack (pushing a word onto the stack) is a
two-step thing, because you have to maintain the correctness of $sp:• addi $sp, $sp, -4• sw $s0, 0($sp)
![Page 26: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/26.jpg)
Suppose we want to
![Page 27: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/27.jpg)
Stack and $sp
• Suppose we want to store a/2 in $s0. • How do we get a/2?
• At the beginning, we do• addi $sp, $sp, -4• sw $s0, 0($sp)
• At the end, we do• lw $s0, 0($sp)• addi $sp, $sp, 4
![Page 28: MIPS Functions](https://reader036.fdocuments.in/reader036/viewer/2022082213/56812ffb550346895d95728f/html5/thumbnails/28.jpg)
.dataA: .word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19
.text
.globl mainmain:
la $s7, Ali $s0, 0 #ili $s1, 0 #resli $s6, 9
loop: sll $t0, $s0, 2add $t0, $t0, $s7lw $a0, 0($t0)lw $a1, 4($t0)jal weirdfunadd $s1, $s1, $v0addi $s0, $s0, 2blt $s0, $s6, loop
done: li $v0,10syscall
weirdfun: addi $sp, $sp, -4sw $s0, 0($sp)
srl $s0, $a0, 1add $t0, $a0, $a0add $t0, $t0, $a1sub $t0, $t0, $s0
ori $v0, $t0, 0
lw $s0, 0($sp)addi $sp, $sp, 4
jr $ra