为什么 scanf 在提供 double 时返回 0.000000?

发布于 2024-09-18 10:30:36 字数 782 浏览 2 评论 0原文

我有以下汇编代码(为 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

我做错了什么?

I have the following assembly code (written for NASM on Linux):

; 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

When I assemble and run the program, I get a prompt as expected. However, no matter what number I enter, the output is always 0.000000.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2024-09-25 10:30:36

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

You are trying to scan a float with the '%f' token but providing a double. Pass in a float variable to scanf and then convert to a double or pass in '%lf' as the format string to scanf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文