程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 學 Win32 匯編[26] - 加減指令: INC、DEC、NEG、ADD、ADC、SUB、SBB、CMP

學 Win32 匯編[26] - 加減指令: INC、DEC、NEG、ADD、ADC、SUB、SBB、CMP

編輯:Delphi

;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 



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved