assembly - 如何在 MIPS 中通过 $a0 传递 float ?

标签 assembly mips subroutine

我正在编写一个 MIPS 库,其中我计划通过 $aX 寄存器传递所有参数,并通过 $vX 寄存器返回所有值。

我编写了以下函数:

# PrintFloat - prints a float
# arguments:
#   $a0 = arress of the float
# return value:
#   n/a
PrintFloat: 
    move $f12, $a0
    li $v0, 2
    syscall
    # return 
    jr $ra 

此函数未在 MARS 中编译。

我有三个问题:

  1. 根本不可能通过 $a 寄存器传递 float 吗?
  2. 我应该设计库来通过 $sp 传递参数(使用 $sp 相对困惑)吗?
  3. 我是否需要在每个像这样的琐碎函数中创建堆栈帧才能使其成为非叶安全?

最佳答案

Is it not possible to pass floats through $a registers at all?

这是可能的。但在 MIPS 中,由于历史原因,FP 被视为协处理器,您必须使用特定指令在处理器和协处理器之间复制移动数据:MFC(从协处理器移动)和 MTC(移至协处理器)。 FP单元为协处理器1,对应的指令为mfc1mtc1

mfc1 rt, fs  # copy data from fp register fs to gp register rt

与复制到 fp 寄存器类似,请使用 mtc1

mtc1 rs, ft  # copy data from gp register rs to fp register ft

所以你只需要使用

mfc1 $a0, $f12

将 $f12 放入 $a0 中。

Should I design the library to pass the arguments through $sp instead (using $sp is comparatively messy)?

不,它没有用,除非你有大量的参数。

Do I need to create stack frames in every trivial functions like this in order to make it non-leaf safe?

没有。但是如果您需要在调用者或被调用者中保存寄存器,那么堆栈帧当然是必需的。如果您的函数调用另一个函数,则保存 $ra 。

关于assembly - 如何在 MIPS 中通过 $a0 传递 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55407732/

相关文章:

assembly - MIPS 系统调用和 $t 寄存器

mips - 在 MIPS 中打印换行符

perl - Perl 中左值子例程的用途是什么?

xml - 无法读取子内部的公共(public)标量

python - 在 python 中使用子例程而不是 if 语句

c - Getrusage 内联汇编

assembly - x86 汇编编码中的宏

assembly - : operator do in assembly? 是什么

assembly - 在从中断处理程序返回之前,我是否必须弹出某些异常推送到堆栈的错误代码?

c++ - MIPS 和 x86_64 之间对象对齐的差异