装配印刷浮子
assembly printing float
我在汇编中的以下操作中遇到了一些麻烦。我正在处理组装 IA32。假设 -4(%ebp)=x 和 -8(%ebp)=y,我已经从用户那里得到了它们。这是代码:
format1: .string "Multiply : %u * %u = %llun"
format2: .string "Divide : %u / %u = %un"
# operation multiply
movl -4(%ebp), %eax
mull -8(%ebp)
pushl %edx
pushl %eax
pushl -8(%ebp)
pushl -4(%ebp)
pushl $format1
call printf
# operation divide
movl -4(%ebp), %eax
divl -8(%ebp)
pushl %eax
pushl -8(%ebp)
pushl -4(%ebp)
pushl $format2
call printf
乘法结果以 %llu 为单位的原因是我希望能够将 2 个长数字相乘并打印结果,即使它达到 64 字节。而且在 %edx 中,mull 命令保存 64 字节结果的"其他 32 个字节",所以我也需要将其推送到堆栈中以用于 printf。例如,我想要这个输出:
Multiply : 4000000000 * 2 = 16000000000
另外,我希望 3 与 4 的除法运算返回 X.YZ 结果。(尾数不超过2个数字,不四舍五入)例如
Divide : 3 / 4 = 0.75
对于 19 和 1000:
Divide : 19 / 1000 = 0.01
对于 8 和 2:
Divide : 8 / 2 = 4.00
我真的尝试了很多来得到结果,但没有成功。谢谢阿洛特!:)
是的,当然你可以使用 scanf
,只需传递正确的参数即可。正如您被告知的那样,对于浮点结果,您需要使用一些浮点除法和浮点格式进行打印。
请注意,根据调用约定,应保留寄存器ebx
值。此外,您应该保持堆栈平衡,最好保持一致。
一个可能的解决方案:
.comm x,4,4
.comm y,4,4
.section .rodata
format1: .string "Div : %d / %d = %gn"
format2: .string "Mod : %d %% %d = %dn"
format3: .string "%d %d"
.text
.globl main
.type main, @function
main:
subl $32, %esp # allocate space, preserve alignment
movl $format3, (%esp)
movl $x, 4(%esp)
movl $y, 8(%esp)
call scanf
# operation divide
fildl x
fidivl y
fstpl 12(%esp) # x / y
movl $format1, (%esp)
movl x, %eax
movl %eax, 4(%esp)
movl y, %eax
movl %eax, 8(%esp)
call printf
# operation modulo
movl x, %eax
cltd
idivl y
movl $format2, (%esp)
movl x, %eax
movl %eax, 4(%esp)
movl y, %eax
movl %eax, 8(%esp)
movl %edx, 12(%esp)
call printf
addl $32, %esp
xor %eax, %eax
ret
请参阅操作中的代码。
相关文章:
- 没有找到相关文章