Nasm浮点读取和打印错误

时间:2015-04-25 19:32:00

标签: assembly floating-point nasm

这是我为读取2个浮点数而编写的代码,并将其存储在内存中。然后我从内存加载它并打印它。但是,打印时我的第一个数字的值是错误的。但是我得到第二个数字的正确值。我不知道这段代码出了什么问题。请帮忙。

SECTION .data
message1: db "Enter the first number: ", 0
message2: db "Enter the second number: ", 0
formatin: db "%lf", 0
formatout: db "%lf", 10, 0 ; newline, nul terminator

section .bss
f1: resd 1
f2: resd 1

SECTION .text
global main 
extern scanf 
extern printf     

main:


push message1
call printf

add esp, 4 
push f1
push formatin 
call scanf
add esp, 8 

push message2
call printf
add esp, 4 

push f2
push formatin
call scanf
add esp, 8

fld qword[f1]
sub esp, 4
fst qword[esp]
push formatout
call printf
add esp, 8

fld qword[f2]
sub esp, 4
fst qword[esp]
push formatout
call printf
add esp, 8


mov eax,1
mov ebx,0
int 80h

2 个答案:

答案 0 :(得分:0)

尝试使用RESQ而不是RESD

f1: resq 1
f2: resq 1

答案 1 :(得分:0)

如果你想要双精度浮点数进行以下更改。

section .bss
f1: resq 1      ;!!!
f2: resq 1      ;!!!

以及

fld qword[f1]
sub esp, 8      ;!!!
fst qword[esp]
push formatout
call printf
add esp, 12     ;!!!

fld qword[f2]
sub esp, 8      ;!!!
fst qword[esp]
push formatout
call printf
add esp, 12     ;!!!