Reputation: 49
I've been trying to learn how to program with Assembly MIPS32 on Mars. I got a question though, i want to make a function (i don't know if that is how its called) that returns an integer. Basically i would like to make a method that asks the user to insert a number and add that number with another one that was previously requested This is what i came up with but i don't know how to return the value to $s2
New:
la $a0, prompt2 # Print string "Input: "
li $v0,4
syscall
li $v0,5 #Read int x
syscall
jr $ra
Add:
j New
add $s1, $s1, $s2 #add Int $s1 and $s2 and save them to $s1
j loop
if anyone has any suggestions or fixes please respond. Thanks in advance
Upvotes: 0
Views: 22089
Reputation: 58437
It is common on MIPS processors to use $v0
for function return values. Of course, you're free to come up with any calling convention you want for your own functions, but unless you have a very good reason not to I'd say stick with $v0
for return values.
Also, to call a function you'd typically use jal
, because that's what sets up the $ra
register so that you later can return with jr $ra
.
If you don't already have it, I suggest that you download MIPS32™ Architecture For Programmers Volume II: The MIPS32™ Instruction Set and read up on how jal
and jr
work (or any other instruction that you feel that you don't fully understand).
So the code you described in your question could look something like:
New:
la $a0, prompt2 # Print string "Input: "
li $v0,4
syscall
li $v0,5 #Read int x
syscall
# The result of syscall 5 is in $v0
jr $ra
Add:
jal New
add $s1, $s1, $v0 # add the value we just read to $s1
j loop
Upvotes: 3