C/C++教程

学 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)  编辑  收藏
这篇关于学 Win32 汇编[26] - 加减指令: INC、DEC、NEG、ADD、ADC、SUB、SBB、CMP的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!