mips函数return一个数

Mips function return a number

我一直在尝试学习如何在火星上使用 Assembly MIPS32 进行编程。 不过我有一个问题,我想创建一个 return 是整数的函数(我不知道它是不是这么叫的)。 基本上我想制作一种方法,要求用户插入一个数字并将该数字与之前请求的另一个数字相加 这是我想出的,但我不知道如何 return $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

如果有人有任何建议或修复,请回复。 提前致谢

在 MIPS 处理器上使用 $v0 函数值 return 是很常见的。当然,您可以自由地为自己的函数想出任何您想要的调用约定,但除非您有充分的理由不这样做,否则我会坚持使用 $v0 来获取 return 值。

此外,要调用您通常使用 jal 的函数,因为这就是设置 $ra 寄存器的原因,以便您稍后可以 return 和 jr $ra .
如果您还没有,我建议您下载 MIPS32™ Architecture For Programmers Volume II: The MIPS32™ Instruction Set 并阅读 jaljr 工作(或任何其他您认为自己不完全理解的指令)。

因此您在问题中描述的代码可能类似于:

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