News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Quadratic Square Root Code Problem

Started by etow, January 30, 2008, 03:13:23 PM

Previous topic - Next topic

etow

Hi I am having trouble with my code below:
--------------------------------------------------------------------------------------------------------------------------

.686 ; create 32 bit code

.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

Include \masm32\include\windows.inc
Include \masm32\macros\macros.asm       ; MASM support macros

  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
Include \masm32\include\masm32rt.inc

  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
IncludeLib \masm32\lib\msvcrt.lib

.Code

start:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Call Main
inkey        ; pause the screen while waiting for user to press any key
             ; to continue
    exit
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Main Proc
  Local WantContinue:DWord
  Local NumberA:DWord
  Local NumberB:DWord
  Local NumberC:DWord

    Mov WantContinue, 0

    .While ((WantContinue >= 0) && (WantContinue < -1))
       print chr$(13, 10)
       Mov NumberA, sval(input("Enter an integer value for Number A : "))
       Mov NumberB, sval(input("Enter an integer value for Number B : "))
       Mov NumberC, sval(input("Enter an integer value for Number C : "))
       print chr$(13, 10)
       Call calculateQuadraticRoots NumberA, NumberB, NumberC
       print chr$(13, 10)
       Mov WantContinue, sval(input("Enter -1 to quit program : "))
       print chr$(13, 10)
    .EndW
  Ret
Main EndP

calculateQuadraticRoots Proc A_Number:DWord, B_Number:DWord, C_Number:DWord
  Local squareRootNumber:DWord
  Local partSquareRootNumber:DWord
  Local BSquared:DWord
  Local NegB:DWord
  Local resultOfSquareRoot:DWord


  Neg B_Number

  Mov Eax, B_Number
  Add Eax, Eax
  Mov BSquared, Eax     ;  b^2

  Mov Eax, A_Number
  IMul C_Number
  Mov Ebx, Eax
  Mov Eax, -4          ;  -4 * a * c

  Mov Eax, partSquareRootNumber
  Mov Ebx, squareRootNumber

  Add Ebx, Eax
  Mov squareRootNumber, Ebx

  Call Intsqrt squareRootNumber
  Mov resultOfSquareRoot, Eax

  Ret
calculateQuadraticRoots EndP

End start

----------------------------------------------------------------------------------------------

I am receiving the following error messages:

============== proj4 - Debug ==============

Assembling: Module1
Module1.asm(3) : warning A4011: multiple .MODEL directives found : .MODEL ignored
------------------------------------------
WARNING Duplicate include file windows.inc
------------------------------------------
\masm32\include\masm32rt.inc(33) : warning A4011: multiple .MODEL directives found : .MODEL ignored
------------------------------------------
WARNING Duplicate include file windows.inc
------------------------------------------
-----------------------------------------
WARNING Duplicate include file user32.inc
-----------------------------------------
-----------------------------------------
WARNING Duplicate include file kernel32.inc
-----------------------------------------
Module1.asm(44) : error A2008: syntax error : ,
Module1.asm(77) : error A2206: missing operator in expression

Errors ocurred.


jdoe

etow,

You have a strange way to learn. Looks like try, try, try until it work, instead of learning how to do.

You already got your answer for duplicates and .MODEL
http://www.masm32.com/board/index.php?topic=8576.msg62394#msg62394
http://www.masm32.com/board/index.php?topic=8576.msg62395#msg62395

When you see "Module1.asm(44) : error A2008: syntax error : ," the "Module1.asm(44)" means that the error is at line 44
Look for "Using invoke" in the help file "masm32" in the help folder of MASM32 Project


MichaelW

etow,

Here is your source with the minimal corrections necessary to make it assemble and link without error.

;;INCLUDED IN masm32rt.inc: .686 ;create 32 bit code
;;INCLUDED IN masm32rt.inc: .Model flat, StdCall ;32 bit memory model
;;INCLUDED IN masm32rt.inc: Option  CaseMap :none ;case sensitive

;;INCLUDED IN masm32rt.inc: Include \masm32\include\windows.inc
;;INCLUDED IN masm32rt.inc: Include \masm32\macros\macros.asm

Include \masm32\include\masm32rt.inc

;;INCLUDED IN masm32rt.inc: IncludeLib \masm32\lib\msvcrt.lib

.Code

start:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    Call Main
    inkey ; pause the screen while waiting for user to press any key
    ; to continue
    exit
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; ----------------------------------------------------------------
; This is the prototype for the calculateQuadraticRoots procedure:
; ----------------------------------------------------------------

calculateQuadraticRoots PROTO :DWord,:DWord,:DWord

Main Proc
  Local WantContinue:DWord
  Local NumberA:DWord
  Local NumberB:DWord
  Local NumberC:DWord

    Mov WantContinue, 0
    .While ((WantContinue >= 0) && (WantContinue < -1))
      print chr$(13, 10)
      Mov NumberA, sval(input("Enter an integer value for Number A : "))
      Mov NumberB, sval(input("Enter an integer value for Number B : "))
      Mov NumberC, sval(input("Enter an integer value for Number C : "))
      print chr$(13, 10)

      ; --------------------------------------------------
      ; To pass arguments in this way you must use invoke.
      ; And to use invoke for a procedure that is defined
      ; after it is invoked, you must provide a prototype
      ; so MASM will know how to call it.
      ; --------------------------------------------------

      ;Call calculateQuadraticRoots NumberA, NumberB, NumberC
      invoke calculateQuadraticRoots, NumberA, NumberB, NumberC

      print chr$(13, 10)
      Mov WantContinue, sval(input("Enter -1 to quit program : "))
      print chr$(13, 10)
    .EndW
    Ret
Main EndP

; -----------------------------------------------------------------
; This is the definition for the calculateQuadraticRoots procedure:
; -----------------------------------------------------------------

calculateQuadraticRoots Proc A_Number:DWord, B_Number:DWord, C_Number:DWord
    Local squareRootNumber:DWord
    Local partSquareRootNumber:DWord
    Local BSquared:DWord
    Local NegB:DWord
    Local resultOfSquareRoot:DWord

    Neg B_Number

    Mov Eax, B_Number
    Add Eax, Eax
    Mov BSquared, Eax     ;  b^2

    Mov Eax, A_Number
    IMul C_Number
    Mov Ebx, Eax
    Mov Eax, -4          ;  -4 * a * c

    Mov Eax, partSquareRootNumber
    Mov Ebx, squareRootNumber

    Add Ebx, Eax
    Mov squareRootNumber, Ebx

    ; ---------------------------------------------------
    ; To pass arguments in this way you must use invoke.
    ; IntSqrt is part of the MASM32 library, so the
    ; prototype is taken care of, but you must use the
    ; correct case for all procedure names, as well as
    ; all of your label and variable names.
    ; ---------------------------------------------------

    ;Call Intsqrt squareRootNumber
    invoke IntSqrt, squareRootNumber

    Mov resultOfSquareRoot, Eax

    Ret
calculateQuadraticRoots EndP

End start
eschew obfuscation

RuiLoureiro

Quote from: etow on January 30, 2008, 03:13:23 PM
  Mov Eax, B_Number
  Add Eax, Eax                           
  Mov BSquared, Eax       ;  b^2   
 

etow,
           i think you have an error here:
                                                      ;  b^2    <=  This is   «b + b»  or  «b * b» ?????????????

           Add   Eax, Eax   <= This is eax PLUS eax    NOT   eax * eax  as  you  want
;................................................
You want something like this:


           Mov     Eax, B_Number
           mov     edx, eax
           mul     edx                           
           Mov     BSquared, Eax       ; BSquared = EAX * EAX 
                      
RuiLoureiro

etow

Here is my updated code below:
-------------------------------------------------------------------------------------

.686 ; create 32 bit code

.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\include\windows.inc
Include \masm32\macros\macros.asm       ; MASM support macros
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\include\masm32rt.inc
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
IncludeLib \masm32\lib\msvcrt.lib
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

.Code

start:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Call Main
inkey        ; pause the screen while waiting for user to press any key
             ; to continue
    exit
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Main Proc
  Local WantContinue:DWord
  Local NumberA:DWord
  Local NumberB:DWord
  Local NumberC:DWord

    Mov WantContinue, 0

    .While ((WantContinue >= 0) && (WantContinue < -1))
       print chr$(13, 10)
       print chr$("Quadratic Equation : Ax^2 + Bx + C ", 13, 10, 13, 10)
       Mov NumberA, sval(input("Enter an integer value for Number A : "))
       Mov NumberB, sval(input("Enter an integer value for Number B : "))
       Mov NumberC, sval(input("Enter an integer value for Number C : "))
       Invoke calculateQuadraticRoots, NumberA, NumberB, NumberC
       print chr$(13, 10)
       Mov WantContinue, sval(input("Enter -1 to quit program : "))
       print chr$(13, 10)
    .EndW
  Ret
Main EndP

calculateQuadraticRoots Proc A_Number:DWord, B_Number:DWord, C_Number:DWord
  Local squareRootNumber:DWord
  Local partSquareRootNumber:DWord
  Local BSquared:DWord
  Local originalB_Number:DWord
  Local negativeB:DWord
  Local resultOfSquareRoot:DWord
  Local numerator1:DWord
  Local numerator2:DWord
  Local denominator:DWord
  Local realRoot1:FLOAT
  Local realRoot2:FLOAT

; Quadratic Equation:  (-b +/- sqrt(b^2 - 4*a*c)) / (2*a)

  Mov Ebx, B_Number
  Mov originalB_Number, Ebx

  Mov Eax, B_Number
  Mov Edx, Eax
  Mul Edx
  Mov BSquared, Eax     ;  b^2

  Mov Eax, A_Number
  IMul C_Number
  Mov Ebx, Eax
  Mov Eax, -4
  IMul Ebx              ; -4 * a * c

  Mov partSquareRootNumber, Eax

  Mov Eax, BSquared
  Mov Ebx, partSquareRootNumber

  Add Eax, Ebx
  Mov squareRootNumber, Eax  ;  b^2 - 4 * a * c

  .If (squareRootNumber >= 0)
     Invoke IntSqrt, squareRootNumber
     Mov resultOfSquareRoot, Eax

     Neg B_Number
     Mov Eax, B_Number
     Mov negativeB, Eax  ; -b

     Mov Eax, 2
     IMul A_Number
     Mov denominator, Eax   ;  2 * a

     Mov Eax, negativeB
     Mov Ebx, resultOfSquareRoot
     Add Eax, Ebx
     Mov numerator1, Eax         ; (- b + sqrt(b^2 - 4 * a * c))

     Neg resultOfSquareRoot
     Mov Eax, resultOfSquareRoot
     Mov Ebx, negativeB
     Add Eax, Ebx
     Mov numerator2, Eax         ; (- b  - sqrt(b^2 - 4 * a * c))

     Mov Eax, numerator1
     Div denominator
     Mov realRoot1, Eax          ; (-b + sqrt(b^2 - 4 * a * c)) / (2 * a)

     Mov Eax, numerator2
     Div denominator
     Mov realRoot2, Eax          ; (-b - sqrt(b^2 - 4 * a * c)) / (2 * a)

     print chr$(13, 10)
     print chr$(" Solutions for quadratic equation : ")

     .If (A_Number == 1)
      print chr$("X^2 ")
     .ElseIf
        print str$(A_Number)
        print chr$("X^2 ")
     .EndIf

     .If (originalB_Number == 1)
      print chr$("+ X ")
     .ElseIf (originalB_Number < SDWord Ptr - 1)
      print str$(originalB_Number)
      print chr$("X ")
     .ElseIf (originalB_Number == -1)
      print chr$("- X ")
     .ElseIf (originalB_Number > 1)
      print chr$("+ ")
      print str$(originalB_Number)
        print chr$("X ")
     .EndIf

     .If (C_Number < SDWord Ptr - 1)
        print str$(C_Number)
     .ElseIf (C_Number == -1)
      print chr$("- ")
        print chr$("1")
     .ElseIf (C_Number > 0)
      print chr$("+ ")
      print str$(C_Number)
     .EndIf

     print chr$(13, 10)

     print chr$(" X1 = ")
     print str$(realRoot1)
     print chr$(13, 10)
     print chr$(" X2 = ")
     print str$(realRoot2)
     print chr$(13, 10)

  .ElseIf (squareRootNumber < SDWord Ptr 0)
  print chr$(13, 10)
     print chr$("There are no real solutions to this quadratic equation!")
     print chr$(13, 10)
  .EndIf

  Ret
calculateQuadraticRoots EndP

End start


------------------------------------------------------------------------------------------
I can not get the second X solution to the quadratic equation to calculate correctly

If I enter 1 for Number A,  enter 0 for Number B,  and enter -4 for Number C,

X1 = 2
X2 = 2147483646

Why is X2 this number above instead of -2?

Please help me.

Thanks

RuiLoureiro

etow,
        1. try to simplify. Dont use a lot of variables that you dont need.
           You can use registers to save some intermediate results (ex: ebx, esi, edi )
           Dont modify the input values like   ***  Neg    B_Number  ***. Use
           *** negativeB variable ***  if you want .
           
        2. I dont know where is the error, your code is not easy to follow. Here my help



  ; Quadratic Equation:  (-b +/- sqrt(b^2 - 4*a*c)) / (2*a)
 
  ;Mov   Ebx, B_Number                  ; dont need
  ;Mov   originalB_Number, Ebx          ; dont need
  ;
  ; b ^ 2
  ; -----
  Mov      eax, B_Number           
  Mov      edx, eax
  IMul      edx
  ;Mov      BSquared, eax               ; dont need   
  mov      ebx, eax                 ;  EBX = b ^ 2
  ;
  ; 4 * a * c
  ; ---------
  mov       ecx, 4
  Mov       eax, A_Number
  IMul      C_Number
  IMul      ecx                     ; EAX = 4 * a * c
  ;
  ; b ^ 2 - 4 * a * c
  ; -----------------
  sub       ebx, eax                ; EBX = b ^ 2 - 4 * a * c
  Mov       squareRootNumber, ebx   
  ;
  ; Test
  ;
  .If (squareRootNumber >= 0)
 
     Invoke   IntSqrt, squareRootNumber
     Mov      resultOfSquareRoot, Eax
            .............
           
       

RuiLoureiro

etow

Hi

My updated code below:

-------------------------------------------------------------------------

.686 ; create 32 bit code

.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\include\windows.inc
Include \masm32\macros\macros.asm       ; MASM support macros
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\include\masm32rt.inc
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
IncludeLib \masm32\lib\msvcrt.lib
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

.Code

start:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Call Main
inkey        ; pause the screen while waiting for user to press any key
             ; to continue
    exit
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

calculateQuadraticRoots Proto:DWord, :DWord, :DWord

Main Proc
  Local WantContinue:DWord
  Local NumberA:DWord
  Local NumberB:DWord
  Local NumberC:DWord

    Mov WantContinue, 0

    .While ((WantContinue >= 0) && (WantContinue < -1))
       print chr$(13, 10)
       print chr$("Quadratic Equation : Ax^2 + Bx + C ", 13, 10, 13, 10)
       Mov NumberA, sval(input("Enter an integer value for Number A : "))
       Mov NumberB, sval(input("Enter an integer value for Number B : "))
       Mov NumberC, sval(input("Enter an integer value for Number C : "))
       Invoke calculateQuadraticRoots, NumberA, NumberB, NumberC
       print chr$(13, 10)
       Mov WantContinue, sval(input("Enter -1 to quit program : "))
       print chr$(13, 10)
    .EndW
  Ret
Main EndP

calculateQuadraticRoots Proc A_Number:DWord, B_Number:DWord, C_Number:DWord
  Local squareRootNumber:DWord
  Local negativeB:DWord
  Local resultOfSquareRoot:DWord
  Local numerator1:DWord
  Local numerator2:DWord
  Local denominator:DWord
  Local realRoot1:Real4
  Local realRoot2:Real4

; Quadratic Equation:  (-b +/- sqrt(b^2 - 4*a*c)) / (2*a)

.If (A_Number != 0)

    Mov Eax, B_Number
    Mov Edx, Eax
    Mul Edx
    Mov Ebx, Eax          ; Ebx = b^2

    Mov Ecx, -4
    Mov Eax, A_Number
    Mul C_Number
    Mul Ecx               ; Eax = -4 * a * c

    Add Ebx, Eax
    Mov squareRootNumber, Ebx   ; ebx =  b^2 - 4 * a * c

   .If (squareRootNumber > SDWord Ptr - 1)
      Invoke IntSqrt, squareRootNumber
      Mov resultOfSquareRoot, Eax

      Mov Eax, B_Number
      Neg Eax
      Mov negativeB, Eax  ; -b

      Mov Eax, 2
      Mul A_Number
      Mov denominator, Eax   ;  eax = 2 * a

      Mov Eax, negativeB
      Mov Ebx, resultOfSquareRoot
      Add Eax, Ebx
      Mov numerator1, Eax       ;  eax = (- b + sqrt(b^2 - 4 * a * c))

      Mov Ebx, resultOfSquareRoot
      Neg Ebx
      Mov Eax, negativeB
      Add Eax, Ebx
      Mov numerator2, Eax       ; eax = (- b  - sqrt(b^2 - 4 * a * c))

      Mov Eax, numerator1
      Div denominator
      Mov realRoot1, Eax       ; eax = (-b + sqrt(b^2 - 4 * a * c)) / (2 * a)

      Mov Eax, numerator2
      Div denominator
      Mov realRoot2, Eax       ; eax = (-b - sqrt(b^2 - 4 * a * c)) / (2 * a)

      print chr$(13, 10)
      print chr$(" Solutions for quadratic equation : ")

      .If (A_Number == -1)
      print chr$("-X^2 ")
      .ElseIf (A_Number == 1)
        print chr$("X^2 ")
      .ElseIf ((A_Number > 1) || (A_Number < SDWord Ptr - 1))
        print str$(A_Number)
        print chr$("X^2 ")
      .EndIf

      .If (B_Number < SDWord Ptr - 1)
      print str$(B_Number)
      print chr$("X ")
      .ElseIf (B_Number == -1)
      print chr$("- X ")
      .ElseIf (B_Number == 1)
      print chr$("+ X ")
      .ElseIf (B_Number > 1)
      print chr$("+ ")
      print str$(B_Number)
        print chr$("X ")
      .EndIf

      .If (C_Number < SDWord Ptr - 1)
        print str$(C_Number)
      .ElseIf (C_Number == -1)
      print chr$("- ")
        print chr$("1")
      .ElseIf (C_Number > 0)
      print chr$("+ ")
      print str$(C_Number)
      .EndIf

      print chr$(13, 10)

      print chr$(" X1 = ")
      print str$(realRoot1)
      print chr$(13, 10)
      print chr$(" X2 = ")
      print str$(realRoot2)
      print chr$(13, 10)

   .ElseIf (squareRootNumber <= SDWord Ptr - 1)
      print chr$(13, 10)
      print chr$("There are no real solutions for the quadratic equation : ")

      .If (A_Number == -1)
      print chr$("-X^2 ")
      .ElseIf (A_Number == 1)
      print chr$("X^2 ")
      .ElseIf ((A_Number > 1) || (A_Number < SDWord Ptr - 1))
        print str$(A_Number)
        print chr$("X^2 ")
      .EndIf

      .If (B_Number < SDWord Ptr - 1)
      print str$(B_Number)
      print chr$("X ")
      .ElseIf (B_Number == -1)
      print chr$("- X ")
      .ElseIf (B_Number == 1)
      print chr$("+ X ")
      .ElseIf (B_Number > 1)
      print chr$("+ ")
      print str$(B_Number)
        print chr$("X ")
      .EndIf

      .If (C_Number < SDWord Ptr - 1)
        print str$(C_Number)
      .ElseIf (C_Number == -1)
      print chr$("- ")
        print chr$("1")
      .ElseIf (C_Number > 0)
      print chr$("+ ")
      print str$(C_Number)
      .EndIf

      print chr$(" !", 13, 10)
   .EndIf
.ElseIf
print chr$(13, 10)
print chr$("Error, Division by zero!", 13, 10)
.EndIf
  Ret
calculateQuadraticRoots EndP

End start


---------------------------------------------------------------------------------------------

I am not sure if realRoot1 and realRoot2 are correct in calculation.

RuiLoureiro

Quote from: etow on January 30, 2008, 11:59:26 PM

      Mov Eax, numerator1
      Div denominator
      Mov realRoot1, Eax       ; eax = (-b + sqrt(b^2 - 4 * a * c)) / (2 * a)

      Mov Eax, numerator2
      Div denominator
      Mov realRoot2, Eax       ; eax = (-b - sqrt(b^2 - 4 * a * c)) / (2 * a)

I am not sure if realRoot1 and realRoot2 are correct in calculation.
etow,
          ***  Div   denominator    ***  divides  EDX:EAX  by denominator. Whats the value
          of EDX ?
          Put    xor   edx, edx   or    mov edx, 0 before div.

          After   ***    Mov numerator1, Eax   ***      ;  eax = (- b + sqrt(b^2 - 4 * a * c))

          you dont need to repeat  ***  Mov Ebx, resultOfSquareRoot  ***
          because   ebx =resultOfSquareRoot.

RuiLoureiro

etow

hi

how do you do real or floating point division instead of integer division?

Thanks

RuiLoureiro

Quote from: etow on January 31, 2008, 02:29:50 PM
hi
how do you do real or floating point division instead of integer division?

Thanks

              As far as i know, using FPU.

RuiLoureiro

etow

Hi RuiLoureiro,

Can you give me an example how to use FPU?

Thanks

etow

I found out the problem.

the code below:


      Mov Eax, numerator1
      IDiv denominator
      Mov realRoot1, Eax       ; eax = (-b + sqrt(b^2 - 4 * a * c)) / (2 * a)

      Mov Eax, numerator2
      IDiv denominator
      Mov realRoot2, Eax       ; eax = (-b - sqrt(b^2 - 4 * a * c)) / (2 * a)


-------------------------------------------------------------------------------------------------------
if the result in Eax is positive then the result is okay
but if the result in Eax is negative then the result is not good or bad result.

How do I fix this?

Please help

Thanks

RuiLoureiro

Quote from: etow on January 31, 2008, 04:46:26 PM
I found out the problem.
-------------------------------------------------------------------------------------------------------
if the result in Eax is positive then the result is okay
but if the result in Eax is negative then the result is not good or bad result.

Hi etow,
         1. Its not correct. When we want to divide a dword value like numerator1 by a dword value like
           denominator we need to put edx to 0 or the signal of EAX into EDX.
           IDiv denominator    divide    EDX:EAX (64 bits) by denominator. Its clear.

         2. About FPU, see fpulib in MASM32 and tuturial\fputute written by Raymond Filiatreault.
             He should have many examples to help you. The work i am doing now doesnt use FPU, so i
              have not an example to give you. I used it some years ago i dont know where are my routines.
              Sorry.
RuiLoureiro

etow

hi

***  Div   denominator    ***  divides  EDX:EAX  by denominator. Whats the value
          of EDX ?
          Put    xor   edx, edx   or    mov edx, 0 before div.

          After   ***    Mov numerator1, Eax   ***      ;  eax = (- b + sqrt(b^2 - 4 * a * c))


Edx register is 0 after putting mov edx, 0 before div.   I tried the above but it does not work.
If the eax is negative for both realRoot1 and realRoot2 values, then answers are 2147483647.
If the eax is negative for either realRoot1 or realRoot2 values then the answer is 2147483647.

etow

Here is my updated code below:


.686 ; create 32 bit code

.Model flat, StdCall  ;32 bit memory model
Option CaseMap :none  ; case sensitive

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\include\windows.inc
Include \masm32\macros\macros.asm       ; MASM support macros
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; -----------------------------------------------------------------
  ; include files that have MASM format prototypes for function calls
  ; -----------------------------------------------------------------
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Include \masm32\include\masm32rt.inc
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  ; ------------------------------------------------
  ; Library files that have definitions for function
  ; exports and tested reliable prebuilt code.
  ; ------------------------------------------------
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
IncludeLib \masm32\lib\msvcrt.lib
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

.Code

start:
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Call Main
inkey        ; pause the screen while waiting for user to press any key
             ; to continue
    exit
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

calculateQuadraticRoots Proto:DWord, :DWord, :DWord

Main Proc
  Local WantContinue:DWord
  Local NumberA:DWord
  Local NumberB:DWord
  Local NumberC:DWord

    Mov WantContinue, 0

    .While ((WantContinue >= 0) && (WantContinue < -1))
       print chr$(13, 10)
       print chr$("Quadratic Equation : Ax^2 + Bx + C ", 13, 10, 13, 10)
       Mov NumberA, sval(input("Enter an integer value for Number A : "))
       Mov NumberB, sval(input("Enter an integer value for Number B : "))
       Mov NumberC, sval(input("Enter an integer value for Number C : "))
       Invoke calculateQuadraticRoots, NumberA, NumberB, NumberC
       print chr$(13, 10)
       Mov WantContinue, sval(input("Enter -1 to quit program : "))
       print chr$(13, 10)
    .EndW
  Ret
Main EndP

calculateQuadraticRoots Proc A_Number:DWord, B_Number:DWord, C_Number:DWord
  Local squareRootNumber:DWord
  Local negativeB:DWord
  Local resultOfSquareRoot:DWord
  Local numerator1:DWord
  Local numerator2:DWord
  Local denominator:DWord
  Local realRoot1:DWord
  Local realRoot2:DWord

; Quadratic Equation:  (-b +/- sqrt(b^2 - 4*a*c)) / (2*a)

.If (A_Number != 0)

    Mov Eax, B_Number
    Mov Edx, Eax
    IMul Edx
    Mov Ebx, Eax          ; Ebx = b^2

    Mov Ecx, -4
    Mov Eax, A_Number
    IMul C_Number
    IMul Ecx               ; Eax = -4 * a * c

    Add Ebx, Eax
    Mov squareRootNumber, Ebx   ; Ebx =  b^2 - 4 * a * c

   .If (squareRootNumber > SDWord Ptr - 1)
      Invoke IntSqrt, squareRootNumber
      Mov resultOfSquareRoot, Eax

      Mov Eax, B_Number
      Neg Eax
      Mov negativeB, Eax  ; Eax = -b

      Mov Eax, 2
      IMul A_Number
      Mov denominator, Eax   ;  Eax = 2 * a

      Mov Eax, negativeB
      Mov Ebx, resultOfSquareRoot
      Add Eax, Ebx
      Mov numerator1, Eax       ;  Eax = (- b + sqrt(b^2 - 4 * a * c))

      Neg Ebx
      Mov Eax, negativeB
      Add Eax, Ebx
      Mov numerator2, Eax       ; Eax = (- b  - sqrt(b^2 - 4 * a * c))

      Mov Eax, numerator1
      IDiv denominator
      Mov realRoot1, Eax       ; Eax = (-b + sqrt(b^2 - 4 * a * c)) / (2 * a)

      Mov Eax, numerator2
      IDiv denominator
      Mov realRoot2, Eax       ; Eax = (-b - sqrt(b^2 - 4 * a * c)) / (2 * a)

      print chr$(13, 10)
      print chr$(" Solutions for quadratic equation : ")

      .If (A_Number == -1)
      print chr$("-X^2 ")
      .ElseIf (A_Number == 1)
        print chr$("X^2 ")
      .ElseIf ((A_Number > 1) || (A_Number < SDWord Ptr - 1))
        print str$(A_Number)
        print chr$("X^2 ")
      .EndIf

      .If (B_Number < SDWord Ptr - 1)
      print str$(B_Number)
      print chr$("X ")
      .ElseIf (B_Number == -1)
      print chr$("- X ")
      .ElseIf (B_Number == 1)
      print chr$("+ X ")
      .ElseIf (B_Number > 1)
      print chr$("+ ")
      print str$(B_Number)
        print chr$("X ")
     .EndIf

      .If (C_Number < SDWord Ptr - 1)
        print str$(C_Number)
      .ElseIf (C_Number == -1)
      print chr$("- ")
        print chr$("1")
      .ElseIf (C_Number > 0)
      print chr$("+ ")
    print str$(C_Number)
      .EndIf

      print chr$(13, 10, 13, 10)

      print chr$(" X1 = ")
      print str$(realRoot1)
      print chr$(13, 10)
      print chr$(" X2 = ")
      print str$(realRoot2)
      print chr$(13, 10)

   .ElseIf (squareRootNumber <= SDWord Ptr - 1)
      print chr$(13, 10)
      print chr$("There are no real solutions for the quadratic equation : ")

      .If (A_Number == -1)
      print chr$("-X^2 ")
      .ElseIf (A_Number == 1)
      print chr$("X^2 ")
      .ElseIf ((A_Number > 1) || (A_Number < SDWord Ptr - 1))
        print str$(A_Number)
        print chr$("X^2 ")
      .EndIf

      .If (B_Number < SDWord Ptr - 1)
      print str$(B_Number)
      print chr$("X ")
      .ElseIf (B_Number == -1)
      print chr$("- X ")
      .ElseIf (B_Number == 1)
      print chr$("+ X ")
      .ElseIf (B_Number > 1)
      print chr$("+ ")
      print str$(B_Number)
        print chr$("X ")
      .EndIf

      .If (C_Number < SDWord Ptr - 1)
        print str$(C_Number)
      .ElseIf (C_Number == -1)
      print chr$("- ")
        print chr$("1")
      .ElseIf (C_Number > 0)
      print chr$("+ ")
      print str$(C_Number)
      .EndIf

      print chr$(" !", 13, 10)
   .EndIf
.ElseIf
print chr$(13, 10)
print chr$("Error, Division by zero!", 13, 10)
.EndIf
  Ret
calculateQuadraticRoots EndP

End start


----------------------------------------------------------------------------------

I am not sure why Eax register outputs 2147483647 for realRoot1 or realRoot2 or for both?

Can anyone help me?

Thanks