装配印刷浮动 [英] assembly printing float
问题描述
我在遇到一些麻烦,在装配以下操作。
我正在组装IA32。假设-4(%EBP)= x和-8(%EBP)= y和我得到他们的用户已。
这是code:
I'm having some troubles with the following actions in assembly. I'm working on assembly IA32. Assume -4(%ebp)=x and -8(%ebp)=y and I get them from user already. this is the code:
format1: .string "Multiply : %u * %u = %llu\n"
format2: .string "Divide : %u / %u = %u\n"
# 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马尔命令保存64字节结果的其他32个字节,所以我需要把它推到堆栈,以及对printf的。
例如我想这样的输出:
The reason the result of the multiply is in %llu is because I want to be able to multiply 2 long numbers and print the result even if it reaches 64 bytes. And also that in %edx the mull command saves the "other 32 bytes" of the 64 byte result, so I need to push it to the stack as well for the printf. e.g. I want this output:
Multiply : 4000000000 * 2 = 16000000000
另外,我要的3 4除法运算返回X.YZ结果。 (不超过2号中的尾数,和没有四舍五入)
例如。
Also, I want the divide operation of 3 with 4 to return X.YZ result. (no more than 2 numbers in the mantissa, and no rounding off) e.g.
Divide : 3 / 4 = 0.75
19和1000:
for 19 and 1000:
Divide : 19 / 1000 = 0.01
和为8和2:
Divide : 8 / 2 = 4.00
我真的努力了很多得到的结果,但没有成功。
非常感谢! :)
I really tried alot to get the result but no success. thanks ALOT! :)
推荐答案
是的,当然你也可以使用 scanf函数
,只是通过正确的参数。
如你已被告知,对于浮点结果,你需要使用一些浮点除法和打印的浮点格式。
Yes, of course you can use scanf
, just pass the correct arguments.
As you have been told, for floating point result you need to use some floating point divide and a floating point format for printing.
请注意,根据调用约定,你应该$ EBX
的对$ pserve寄存器的值。此外,你应该保持堆栈平衡和preferably对齐。
Note that according to calling convention, you should preserve value of ebx
register. Furthermore, you should keep stack balanced, and preferably aligned.
一个可能的解决方案:
.comm x,4,4
.comm y,4,4
.section .rodata
format1: .string "Div : %d / %d = %g\n"
format2: .string "Mod : %d %% %d = %d\n"
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
查看 code运行。
这篇关于装配印刷浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!