4.2.7 例子程序(AddSub3)

 

下面的程序使用ADD,SUB,INC,DEC和NEG指令实现了各种算术运算表达式,注释中展示了CPU的状态标志是如何受影响的:

TITLE Addition and Subtraction (AddSub3.asm)
INCLUDE Irvine32.inc
.data
    Rval     SDWORD    ?
    Xval      SDWORD    26
    Yval      SDWORD    30
    Zval      SDWORD    4 0
.code
main PROC

    ;INC and DEC
    mov ax,1000h
    inc ax            ;1001h
    dec ax            ;1000h

    ;表达式:Rval = -Xval + (Yval - Zval)
    mov eax,Xval
    neg eax                 ;-26
    mov ebx,Yval
    sub ebx,Zval       ;-10
    add eax,ebx
    mov Rval,eax       ;-36
    
    ;零标志的例子
    mov cx,1
    sub cx,1            ;ZF = 1
    mov ax,0FFFFh
    inc  ax              ;2F = 1

    ;符号标志的例子
    mov cx,0
    sub cx,1            ;SF = 1
    mov ax,7FFFh
    add ax,2            ;SF = 1

    ;进位标志的例子
    mov al,0FFh
    add al,1            ;CF = 1,AL = 00

    ;溢出标志的例子
    mov al,+127
    add al,1            ;OF = 1
    mov al,-128
    sub al,1            ;OF = 1
  
    exit
main ENDP
END main