提问人:Jonas 提问时间:6/17/2023 最后编辑:Jonas 更新时间:6/17/2023 访问量:112
如何使用scanf读取nasm x86 64中的浮点值?
How to use scanf to read a floating point value in nasm x86 64?
问:
我正在尝试简单地读取浮点值并使用程序集 x86 64 打印它。因此,当我尝试打印变量价格时,我将其用作 c 函数 scanf 缓冲区的值不会改变。
它将打印最初设置的值,因此在下面的代码中,它打印 0.0,因此 scanf 函数无法正确更改价格的值。
要生成可执行文件,我使用命令 .nasm -f elf64 test.asm && gcc -no-pie test.o -o test
我做错了什么?
section .data
price dq 0.0
fmt db "%.1f", 0
section .text
global main
extern printf, scanf
main:
push rbp
mov rbp, rsp
mov rdi, fmt ;scanf first arg, format string
mov rsi, price ;scanf second argument, this variable is supposed to change
mov rax, 0
call scanf
movq xmm0, [price] ;in float point mode xmm0 holds the value to be printed
mov rdi, fmt
mov rax, 1 ;rax has to be set 1 to float point mode
call printf
leave
ret
答:
3赞
fuz
6/17/2023
#1
格式说明符要求使用浮点数,而不是双精度值。用于双精度或调整为单精度浮点数。%f
%lf
price
评论
4赞
Peter Cordes
6/17/2023
如果你使用单精度,你需要,因为确实需要(如何使用printf打印单精度浮点数)。我想这就是你所说的调整的部分意思scanf
cvtss2sd xmm0, [price]
printf("%f")
double
price
评论