NASM浮点输出似乎是无稽之谈

时间:2013-10-17 18:26:30

标签: floating-point nasm

这里在NASM浮点运算的简单测试中,我开始收集用于求解两个线性方程组的数据。但是,应该采用a1 * a4的程序输出太长而不明智。我输入单位数整数作为我的数字,但仍然得到一个遍历几个控制台行的结果。我做错了什么?

我的代码:

extern printf
extern scanf
extern exit

global main

SECTION .data
message1: db "Enter the value of ",0
welcome: db "Welcome to the linear equation solver.",10,10,0

eq1:db "    a1 * x + a2 * y = b1",10,0
eq2:db "    a3 * x + a4 * y = b2",10,10,0

formats: db "%s",0
formatss: db "%s %s: ",0
formatf: db "%f",0
a1: db "a1",0
a2: db "a2",0
a3: db "a3",0
a4: db "a4",0
b1: db "b1",0
b2: db "b2",0

SECTION .bss

float1: resd 1
float2: resd 1
float3: resd 1
float4: resd 1
float5: resd 1
float6: resd 1
a1a4: resd 1
a2a3: resd 1

SECTION .text

main:

;Welcome to the linear equation solver
push welcome
push formats
call printf
add esp, 8

;show equation type
push eq1
push formats
call printf
add esp, 8
push eq2
push formats
call printf
add esp, 8

;get a1
push a1
push message1
push formatss
call printf
add esp, 12
push float1
push formatf
call scanf
add esp, 8

;get a2
push a2
push message1
push formatss
call printf
add esp, 12
push float2
push formatf
call scanf
add esp, 8

;get b1
push b1
push message1
push formatss
call printf
add esp, 12
push float5
push formatf
call scanf
add esp, 8

;get a3
push a3
push message1
push formatss
call printf
add esp, 12
push float3
push formatf
call scanf
add esp, 8

;get a4
push a4
push message1
push formatss
call printf
add esp, 12
push float4
push formatf
call scanf
add esp, 8

;get b2
push b2
push message1
push formatss
call printf
add esp, 12
push float6
push formatf
call scanf
add esp, 8

;Initiate floats
FINIT

;Bring a1 into st float register.
fld dword [float1] ;ST0
fmul dword [float4] ;ST0 *= float4
fstp dword [a1a4] ;Store result: a1 * a4, pop float stack

;print a1 * a4
fld dword [a1a4]
sub esp, 8
fstp qword [esp]
push formatf
call printf
add esp, 12

call exit

1 个答案:

答案 0 :(得分:0)

发现问题了!似乎浮点数存储在80位,但为了正确打印它,你必须强制它为64位双重表示。我已相应更改了代码。以下是打印存储在内存中的任何数字的方法:

fld dword [a1a4] ;load a1a4 float into FPU
sub esp, 8 ;reserve qword space for this in stack
fstp qword [esp] ;pop into stack
push formatf
call printf
add esp, 12