【发布时间】:2017-10-25 14:28:15
【问题描述】:
近期编辑
我正在尝试在 x86 MASM 上运行这个浮点二次方程程序。这段代码可以在 Kip Irvine x86 教科书中找到,我想看看它是如何在视觉上工作的。以下代码如下:
include irvine32.inc
.DATA
a REAL4 3.0
b REAL4 7.0
cc REAL4 2.0
posx REAL4 0.0
negx REAL4 0.0
.CODE
main proc
; Solve quadratic equation - no error checking
; The formula is: -b +/- squareroot(b2 - 4ac) / (2a)
fld1 ; Get constants 2 and 4
fadd st,st ; 2 at bottom
fld st ; Copy it
fmul a ; = 2a
fmul st(1),st ; = 4a
fxch ; Exchange
fmul cc ; = 4ac
fld b ; Load b
fmul st,st ; = b2
fsubr ; = b2 - 4ac
; Negative value here produces error
fsqrt ; = square root(b2 - 4ac)
fld b ; Load b
fchs ; Make it negative
fxch ; Exchange
fld st ; Copy square root
fadd st,st(2) ; Plus version = -b + root(b2 - 4ac)
fxch ; Exchange
fsubp st(2),st ; Minus version = -b - root(b2 - 4ac)
fdiv st,st(2) ; Divide plus version
fstp posx ; Store it
fdivr ; Divide minus version
fstp negx ; Store it
call writeint
exit
main endp
end main
所以我能够让我的程序完全编译、执行和工作。但是,每当我运行程序时,我都会得到以下结果:
+1694175115
为什么结果如此之大?我也尝试调用 writefloat,但它说这个过程不在 Irvine32.inc 或 Macros.inc 库中。有人可以告诉我为什么它不起作用以及需要修复什么吗?谢谢。
【问题讨论】:
-
您不会以任何方式结束程序,因此 CPU 将继续读取垃圾数据并执行它。使用调试器随时查看发生了什么。
-
我没有看到这个问题的 C 部分。
-
当您到达
fstp negx时,st(FPU 堆栈的顶部)具有二次方程的根之一。fstp negx取st中的值并将其放入内存中的negx并弹出堆栈。您可以简单地删除fstp negx并通过简单地使用WriteFloat函数和call WriteFloat打印FPU 堆栈的顶部。如果您希望将值存储在negx中并打印然后您可以将fstp negx更改为fst negx并使用call WriteFloat跟随它 -
当然要完全删除
call writeint,因为它会在EAX中打印出带符号的整数。它不写入浮点值。 -
WriteFloat必须在那里,除非你有一个真正的旧irvine32.inc和irvine32.lib。获取newer link library from Irvine's homepage(第七版示例程序和链接库源代码)并安装。
标签: assembly floating-point x86 masm irvine32