1

我有以下汇编代码(为 Linux 上的 NASM 编写):

; This code has been generated by the 7Basic
; compiler <http://launchpad.net/7basic>

extern printf
extern scanf

      SECTION .data
printf_f: db "%f",10,0
scanf_f: db "%f",0

      SECTION .bss
v_0 resb 8

      SECTION .text

global main
  main:
push ebp
mov ebp,esp
push v_0         ; load the address of the variable
push scanf_f     ; push the format string
call scanf       ; call scanf()
add esp,8
push dword [v_0+4]  ; load the upper-half of the double
push dword [v_0]    ; load the bottom-half
push printf_f     ; push the format string
call printf       ; call printf
add esp,12
mov esp,ebp
pop ebp
mov eax,0
ret

当我组装并运行程序时,我得到了预期的提示。但是,无论我输入什么数字,输出始终是0.000000.

我究竟做错了什么?

4

1 回答 1

4

您正在尝试使用 '%f' 标记扫描浮点数,但提供双精度。将 float 变量传递给 scanf,然后转换为 double 或将 '%lf' 作为格式字符串传递给 scanf。

于 2010-09-12T21:16:29.693 回答