RECENT EDIT
I am trying to run this floating point Quadratic Equation program on x86 MASM. This code is found in the Kip Irvine x86 textbook and I want to see how it works visually. The following code is below:
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
So I was able to have my program compile, execute and work completely. However, Whenever I run the program, I get this as the result:
+1694175115
Why is the result so large? Also I tried calling writefloat but it says this procedure is not in the Irvine32.inc or Macros.inc Library. Can someone show me why is it not working and what needs to be fixed? Thanks.
fstp negx
,st
(top of FPU stack) has one of the roots of the quadratic equation.fstp negx
takes the value inst
and places it in memory atnegx
and pops the stack. You can simply removefstp negx
and print the top of the FPU stack by simply using theWriteFloat
function withcall WriteFloat
.If you want the value both stored innegx
and printed then you can changefstp negx
tofst negx
and follow it withcall WriteFloat
– Tattancall writeint
altogether since it prints out the signed integer in EAX. It doesn't write floating point values. – TattanWriteFloat
must be there except you've got a real oldirvine32.inc
andirvine32.lib
. Get a newer link library from Irvine's homepage (Example programs and link library source code for the Seventh Edition) and install it. – Exertcall writeint
. – Exert