我试图使用NASM风格的x86程序集打印出一些32位浮点数。这是我尝试做的最小工作示例:
global main
extern printf, scanf
section .data
scan_format: db "%f",0
print_format: db "%f",0xA,0
section .bss
result_num: resb 4
section .text
main:
push result_num
push scan_format
call scanf
add esp, 8
push dword [result_num]
push print_format
call printf
add esp, 8
ret
当我运行它时,我得到一些奇怪的输出:
$ nasm -felf32 -g printf_test.asm
$ gcc printf_test.o -o printf_test.out
$ ./printf_test.out <<< 1234
-0.000000
如果我在程序运行时尝试检查该值,它似乎是正确的:
$ gdb ./printf_test.out
(gdb) disassemble *main
Dump of assembler code for function main:
0x08048420 <+0>: push 0x804a028
0x08048425 <+5>: push 0x804a018
0x0804842a <+10>: call 0x8048330 <scanf@plt>
0x0804842f <+15>: add esp,0x8
0x08048432 <+18>: push DWORD PTR ds:0x804a028
0x08048438 <+24>: push 0x804a01b
0x0804843d <+29>: call 0x8048320 <printf@plt>
0x08048442 <+34>: add esp,0x8
0x08048445 <+37>: ret
0x08048446 <+38>: nop
0x08048447 <+39>: nop
0x08048448 <+40>: nop
0x08048449 <+41>: nop
0x0804844a <+42>: nop
0x0804844b <+43>: nop
0x0804844c <+44>: nop
0x0804844d <+45>: nop
0x0804844e <+46>: nop
0x0804844f <+47>: nop
End of assembler dump.
(gdb) break *main+34
Breakpoint 1 at 0x8048442
(gdb) r
Starting program: /vagrant/project_03/printf_test.out
1234
-0.000000
Breakpoint 1, 0x08048442 in main ()
(gdb) p /f result_num
$1 = 1234
我在这里做错了什么?
如果我尝试使用双打,它甚至不会组装,这个程序:
global main
extern printf, scanf
section .data
scan_format: db "%f",0
print_format: db "%f",0xA,0
section .bss
result_num: resb 4
result_num_dub: resb 8
section .text
main:
push result_num
push scan_format
call scanf
add esp, 8
fld dword [result_num]
fstp qword [result_num_dub]
push qword [result_num_dub] ;ASSEMBLER ERROR HERE
push print_format
call printf
add esp, 8
ret
生成此输出:
$ nasm -felf32 -g printf_test.asm
printf_test.asm:22: error: instruction not supported in 32-bit mode
如果我尝试直接从浮动堆栈转到内存堆栈,我会得到一个段错误,即使正确的东西似乎存储在内存中。
fld dword [result_num]
fstp qword [esp]
push format
call printf
在gdb中看起来是正确的:
(gdb) p *((double*)($esp))
$9 = 2.5
但是它会在printf调用的中间生成一个段错误。
我必须遗漏一些东西。
答案 0 :(得分:2)
正如迈克尔指出的那样,%f
中的printf
期望double
,所以在将数字推到{{1}之前,您的数字必须转换为double
}}:
printf
推qword [result_num_dub]; ASSEMBLER ERROR HERE
在32位模式下,您不能执行64位操作,例如一次按64位。这就是我使用global main
extern printf, scanf
section .data
scan_format: db "%f",0
print_format: db "Result: %f",0xA,0
section .bss
result_num: resb 4
section .text
main:
push result_num
push scan_format
call scanf
add esp, 8
sub esp,8 ;reserve stack for a double in stack
mov ebx,result_num
fld dword [ebx] ;load float
fstp qword [esp] ;store double (8087 does the conversion internally)
push print_format
call printf
add esp, 12
ret
方法保留堆栈空间的原因。你的第二个项目只需要这个:
sub esp