匯編指令的一般性要求: 1、兩個操作數的尺寸必須一致; 2、操作數不能同為內存.
MOV(Move): 最常用的數據傳送指令
;該指令不影響 EFlags
;指令格式: (其中的 r、m、i 分別表示: 寄存器、內存、立即數)
MOV r, r
MOV r, m
MOV r, i
MOV m, r
MOV m, i
; Test21_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
.data?
val dd ?
.code
main proc
mov val, 123
mov edx, val
mov eax, edx
PrintDec eax ;123
ret
main endp
end main
LEA(Load Effective Address): 有效地址傳送指令
;該指令不影響 EFlags
;指令格式:
LEA r, m
; Test21_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
.data
szText db 'ABCDEFG', 0
.code
main proc
lea eax, offset szText
lea ebx, szText
PrintHex eax ;00403000
PrintHex ebx ;00403000
ret
main endp
end main
XCHG(Exchange): 交換指令
;該指令不影響 EFlags
;指令格式:
XCHG r, r
XCHG r, m
XCHG m, r
; Test21_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
.data
val1 dd 111
val2 dd 222
.code
main proc
mov eax, val1
xchg eax, val2
mov val1, eax
PrintDec val1 ;222
PrintDec val2 ;111
ret
main endp
end main
XLATB(Translate Byte)、XLAT(Translate): 換碼指令
;XLATB 可簡化為 XLAT
;該指令不影響 EFlags
;XLAT 無參數, 操作和 EBX、AL 相關
;執行 XLAT 前, 先把源地址放入 EBX, 把字節序號放入 AL
;執行後, 指定字節被讀入 AL
; Test21_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
.data
szText db 'ABCDEFG', 0
.code
main proc
lea ebx, szText
mov al, 1
xlat
PrintHex al ;42 - 這是 'B' 的 ASSII 編碼
mov al, 2
xlatb
PrintHex al ;43 - 這是 'C' 的 ASSII 編碼
ret
main endp
end main
MOVZX(Move With Zero-Extend): 零擴展傳送
;該指令不影響 EFlags
;指令格式:
MOVZX r32, r16
MOVZX r32, m16
MOVZX r16, r8
MOVZX r16, m8
;示例同 MOVSX
MOVSX(Move With Sign-Extend): 符號擴展傳送
;該指令不影響 EFlags
;指令格式:
MOVSX r32, r16
MOVSX r32, m16
MOVSX r16, r8
MOVSX r16, m8
;MOVZX 和 MOVSX 的區別是:
;1、MOVZX 會將目標寄存器中高出的位補 0
;2、如果源操作數的最高位是 1, MOVSX 會將目標寄存器中高出的位補 1; 反之補 0
; Test21_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
bVal db 90h
dwVal1 dw 7FFFh
dwVal2 dw 8000h
.code
main proc
movzx eax, dwVal1
movsx edx, dwVal1
PrintHex eax ;00007FFF
PrintHex edx ;00007FFF
movzx eax, dwVal2
movsx edx, dwVal2
PrintHex eax ;00008000
PrintHex edx ;FFFF8000
mov cl, bVal
movzx ax, cl
movsx dx, cl
PrintHex ax ;0090
PrintHex dx ;FF90
ret
main endp
end main