Slide 7.3: NEG instructions
Slide 7.5: Flags affected by addition and subtraction
Home

Implementing Arithmetic Expressions

R = - (X + (-Y)) + Z

 INCLUDE Irvine32.inc
 .data
    prompt1  BYTE  "Calculating the expression: - (X + (-Y)) + Z", 0Dh, 0Ah, 0
    prompt2  BYTE  "Enter the value of X:  ", 0
    prompt3  BYTE  "Enter the value of Y:  ", 0
    prompt4  BYTE  "Enter the value of Z:  ", 0
    prompt5  BYTE  "The result R is  ", 0
    R  SWORD  ?
    X  SWORD  ?
    Y  SWORD  ?
    Z  SWORD  ?
 .code
 main proc
    call   Clrscr
    mov    edx, OFFSET prompt1     ; Calculating the expression: - (X + (-Y)) + Z
    call   WriteString

    mov    edx, OFFSET prompt2     ; Enter the value of X:
    call   WriteString
    call   ReadInt
    mov    X, ax

    mov    edx, OFFSET prompt3     ; Enter the value of Y:
    call   WriteString
    call   ReadInt
    mov    Y, ax

    mov    edx, OFFSET prompt4     ; Enter the value of Z:
    call   WriteString
    call   ReadInt
    mov    Z, ax
    mov    ax, X                   ; R = - (X + (-Y)) + Z
   

   
   
neg ax add ax, Z mov R, ax
    mov    edx, OFFSET prompt5     ; The result R is 
    call   WriteString
    movsx  eax, R
    call   WriteInt
    call   Crlf
    call   Crlf
    exit
 main ENDP
 END main
An Execution Example
 Calculating the expresssion: - (X + (-Y)) + Z
 Enter the value of X:  20
 Enter the value of Y:  10
 Enter the value of Z:  -10
 The result R is  -20

The italic text with yellow background is entered by users.