Implementing Arithmetic Expressions


This program implements the following C++ expression in assembly language, using 32-bit signed integers:
     x = ( y / z ) × ( x + y )
 x = ( y / z ) × ( x + y ) 
 INCLUDE Irvine32.inc
 .data
           x  SDWORD  ?
           y  SDWORD  ?
           z  SDWORD  ?
           prompt1  BYTE  "x = ", 0
           prompt2  BYTE  "y = ", 0
           prompt3  BYTE  "z = ", 0
           prompt4  BYTE  "( y / z ) * ( x + y ) = ", 0
           prompt5  BYTE  "Divide by zero!", 0Dh, 0Ah, 0
           prompt6  BYTE  "Overflow!", 0Dh, 0Ah, 0

 .code
 main PROC
           ;; Read x.
           mov   edx, OFFSET prompt1
           call  WriteString
           call  ReadInt
           mov   x, eax

           ;; Read y.
           mov   edx, OFFSET prompt2
           call  WriteString
           call  ReadInt
           mov   y, eax

           ;; Read z.
           mov   edx, OFFSET prompt3
           call  WriteString
           call  ReadInt
           mov   z, eax

           ;; Calculate x = ( y / z ) × ( x + y ) .
           mov   eax, y
           cdq
           test  z, 0FFFFFFFFh
           jz    ZERO
           idiv  z
           mov   ebx, x
           add   ebx, y    
          
          
 
mov x, eax ;; Print result. mov edx, OFFSET prompt4 call WriteString mov eax, x call WriteInt call Crlf exit ;; Divide by zero. ZERO: mov edx, OFFSET prompt5 call WriteString exit ;; Result is too large. OVERFLOW: mov edx, OFFSET prompt6 call WriteString exit main ENDP END main