这个程序意味着添加两个浮点数并显示它们各自的结果,但是程序给出了不想要的结果,我不明白为什么需要为一个浮点数分配16个字节,因为一个双字节占用8个字节,那么为什么不为浮点数分配8个字节呢?
.text
.globl main
.type main, @function
main:
subl $12, %esp # allocate enough memory for a floating point value
flds (V0) # load loading single precision variable 1
flds (V1) # load single precision variable 2
fadd %st(1), %st(0) # add both of them [ NOTE: reg ST(0) contains correct result ]
fstpl (%esp) # store the float
pushl $.LC0
call printf
addl $16, %esp # return allocated mem to OS [ 12 + 4 = 16 ]
ret
.LC0: .string "%f\n"
V0: .float 9.3
V1: .float 9.4发布于 2019-11-19 10:47:30
在我看来,ESP在调用main之前是16字节对齐的(这会推送一个4字节的返回地址),所以它应该在调用printf之前使用sub $12, %esp重新创建i386系统V(假设您在Linux上)所要求的16字节对齐。
如果用C编译器编译C函数,就会看到这一点。
我还建议将flds用于单精度负载,fstpl用于双精度存储,以使大小变得显式。这也是错误的;默认情况下加载和存储是相同的,这个程序需要浮动加载和双存储。(默认为单精度float,dword)
(printf "%f"采用double,因为C中没有将float传递给变量函数的方法:默认升级适用。)
https://stackoverflow.com/questions/58931989
复制相似问题