0

在对 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
4

1 回答 1

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
于 2013-10-28T18:55:27.797 回答