编程语言
首页 > 编程语言> > 学 Win32 汇编[26] - 加减指令: INC、DEC、NEG、ADD、ADC、SUB、SBB、CMP

学 Win32 汇编[26] - 加减指令: INC、DEC、NEG、ADD、ADC、SUB、SBB、CMP

作者:互联网

学 Win32 汇编[26] - 加减指令: INC、DEC、NEG、ADD、ADC、SUB、SBB、CMP


;INC(Increment): 加一
;DEC(Decrement): 减一
;NEG(Negate):    求补(求反)
;ADD(Add):       加
;ADC(Add Carry): 带进位加
;SUB(Subtract):  减
;SBB(Subtract With Borrow): 带进位减
;CMP(Compare):   比较

;INC、DEC、NEG 的指令格式:
;INC r/m

;ADD、ADC、SUB、SBB、CMP 的指令格式:
;ADD r/m, r/m/i

;它们的结果影响 OF、SF、ZF、AF、PF、CF

INC、DEC、NEG
; Test26_1.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.code
main proc
    mov eax, 6
    inc eax
    PrintDec eax ;7
    dec eax
    PrintDec eax ;6
    neg eax      ;NEG 就是加个负号
    PrintDec eax ;-6
    neg eax
    PrintDec eax ;6
    ret
main endp
end main

ADD、ADC
; Test26_2.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.code
main proc
    mov eax, 5
    add eax, 1
    PrintDec eax ;6
    
    stc          ;CF=1
    mov eax, 5
    adc eax, 1   ;ADC 比 ADD 还要多加上 CF
    PrintDec eax ;7
    
    clc          ;CF=0
    mov eax, 5
    adc eax, 1
    PrintDec eax ;6
    ret
main endp
end main

SUB、SBB
; Test26_3.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.code
main proc
    mov eax, 5
    sub eax, 1
    PrintDec eax ;4
    
    stc
    mov eax, 5
    sbb eax, 1   ;SBB 比 SUB 还要多减去 CF
    PrintDec eax ;3
    
    clc
    mov eax, 5
    sbb eax, 1
    PrintDec eax ;4
    ret
main endp
end main

CMP

;CMP 隐含执行 SUB, 但并不改写操作数, 只是影响标志位;
;零标志位标识着两个数是否相等, 符号标志位标识着两个数的大小. 
;它后面经常跟着跳转指令

; Test26_4.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.code
main proc
    mov eax, 3
    cmp eax, 3
    lahf
    PrintHex ah ;46 - 01000110b(ZF=1 说明两个数相等)
    
    mov eax, 3
    cmp eax, 2
    lahf
    PrintHex ah ;02 - 00000010b(SF=0、ZF=0 说明前者 > 后者)
    
    mov eax, 3
    cmp eax, 4
    lahf
    PrintHex ah ;76 - 10010111b(SF=1、ZF=0 说明前者 < 后者)    
    ret
main endp
end main

辨析 NEG 与 NOT
; Test26_5.asm
.386
.model flat, stdcall

include    windows.inc
include    kernel32.inc
include    masm32.inc
include    debug.inc
includelib kernel32.lib
includelib masm32.lib
includelib debug.lib

.data?
    val sword ?
.code
main proc
    ;求补运算: 取反+1
    mov val, 44
    PrintDec val  ;44
    PrintHex val  ;002C - 00000000 00101100b
    
    not val
    inc val
    PrintDec val  ;-44
    PrintHex val  ;FFD4 - 11111111 11010100b
    
    not val
    inc val
    PrintDec val  ;44
    PrintHex val  ;002C - 00000000 00101100b
    
    ;NEG 就相当于: 取反+1
    mov val, 44
    neg val
    PrintDec val  ;-44
    PrintHex val  ;FFD4 - 11111111 11010100b
    
    neg val
    PrintDec val  ;44
    PrintHex val  ;002C - 00000000 00101100b
    ret
main endp
end main

posted on 2010-04-15 17:15  万一  阅读(18685)  评论(0)  编辑  收藏

标签:26,SUB,val,NEG,includelib,eax,lib,include,inc
来源: https://blog.51cto.com/u_14617575/2745490