Druck schwebt mit printf in x86 nasm 32-bit

Ich versuche, einige 32-Bit-Floats mit einer NASM-x86-Assembly auszudrucken. Dies ist ein minimales Arbeitsbeispiel für das, was ich versuche:

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

Wenn ich das starte, bekomme ich eine seltsame Ausgabe:

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

Wenn ich versuche, den Wert zu überprüfen, während das Programm ausgeführt wird, scheint er korrekt zu sein:

$ 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

Was mache ich hier falsch?

BEARBEITE

Wenn ich versuche, Doubles zu verwenden, wird dieses Programm nicht einmal zusammengesetzt:

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

Erzeugt diese Ausgabe:

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

Und wenn ich versuche, direkt vom Float-Stack in den Memory-Stack zu wechseln, erhalte ich einen Segfault, obwohl das Richtige im Speicher zu sein scheint.

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

call printf

Es sieht richtig aus in gdb:

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

Aber es wird ein Segfault in der Mitte des printf-Aufrufs generiert.

Ich muss etwas vermissen.

Antworten auf die Frage(2)

Ihre Antwort auf die Frage