Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

assembly - Printing floats with printf in x86 nasm 32-bit

I'm trying to print out some 32-bit floats using NASM flavored x86 assembly. This is a minimum working example of what I'm trying to do:

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

When I run this, I get some strange output:

$ nasm -felf32 -g printf_test.asm
$ gcc printf_test.o -o printf_test.out
$ ./printf_test.out <<< 1234
-0.000000

If I try to examine the value while the program is running, it seems to be correct:

$ 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

What am I doing wrong here?

EDIT

If I try to use doubles, it won't even assemble, this program:

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

Generates this output:

$ nasm -felf32 -g printf_test.asm
printf_test.asm:22: error: instruction not supported in 32-bit mode

And if I try to go directly from the float stack to the memory stack, I get a segfault, even though the right thing appears to be stored in memory.

fld dword [result_num]
fstp qword [esp]
push format

call printf

It looks right in gdb:

(gdb) p  *((double*)($esp))
$9 = 2.5

But it generates a segfault in the middle of the printf call.

I have to be missing something.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Michael pointed, %f in printf expects a double, so your number must be converted into a double just before pushing it on the stack for printf:

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

push qword [result_num_dub] ;ASSEMBLER ERROR HERE

You cannot do 64 bit operations, like pushing 64 bits at a time, in 32 bit mode. This is why I used the sub esp method to reserve stack space. Your second program just needs this:

section .text
main:
    push result_num
    push scan_format
    call scanf
    add esp, 8

    fld dword [result_num]
    fstp qword [result_num_dub]
    push dword [result_num_dub+4]  ;pushes 32 bits (MSB)
    push dword [result_num_dub]    ;pushes 32 bits (LSB)
    push print_format
    call printf
    add esp, 12   ;<-- 12 bytes, not 8.
    ret

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...