我正在编写一个 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 中编译。
我有三个问题:
- 根本不可能通过
$a
寄存器传递 float 吗? - 我应该设计库来通过
$sp
传递参数(使用$sp
相对困惑)吗? - 我是否需要在每个像这样的琐碎函数中创建堆栈帧才能使其成为非叶安全?
最佳答案
Is it not possible to pass floats through $a registers at all?
这是可能的。但在 MIPS 中,由于历史原因,FP 被视为协处理器,您必须使用特定指令在处理器和协处理器之间复制移动数据:MFC
(从协处理器移动)和 MTC
(移至协处理器)。 FP单元为协处理器1,对应的指令为mfc1
和mtc1
。
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/