這裡的 "串" 並不單指字符串, 包括所有連續的數據(如數組); 串指令只用於內存操作.
移動串指令: MOVSB、MOVSW、MOVSD ;從 ESI -> EDI; 執行後, ESI 與 EDI 的地址移動相應的單位
比較串指令: CMPSB、CMPSW、CMPSD ;比較 ESI、EDI; 執行後, ESI 與 EDI 的地址移動相應的單位
掃描串指令: SCASB、SCASW、SCASD ;依據 AL/AX/EAX 中的數據掃描 EDI 指向的數據, 執行後 EDI 自動變化
儲存串指令: STOSB、STOSW、STOSD ;將 AL/AX/EAX 中的數據儲存到 EDI 給出的地址, 執行後 EDI 自動變化
載入串指令: LODSB、LODSW、LODSD ;將 ESI 指向的數據載入到 AL/AX/EAX, 執行後 ESI 自動變化
其中的 B、W、D 分別指 Byte、Word、DWord, 表示每次操作的數據的大小單位.
上述指令可以有重復前綴:
REP ECX > 0 時
REPE (或 REPZ) ECX > 0 且 ZF=1 時
REPNE(或 REPNZ) ECX > 0 且 ZF=0 時
;重復前綴可以自動按單位(1、2、4)遞減 ECX
MOVSB: 移動字符串
; Test29_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
szSource db 'Delphi 2010', 0
len equ $ - szSource - 1
szDest db len dup(?), 0
.code
main proc
lea esi, szSource
lea edi, szDest
mov ecx, len
cld ;復位標志寄存器的方向標志, 以讓串地址由低到高
rep movsb
PrintString szDest ;Delphi 2010
ret
main endp
end main
上面的例子, 假如不使用重復前綴...
; Test29_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
szSource db 'Delphi 2010', 0
len equ $ - szSource - 1
szDest db len dup(?), 0
.code
main proc
lea esi, szSource
lea edi, szDest
mov ecx, len
cld
@@: movsb
dec ecx
jnz @B
PrintString szDest
ret
main endp
end main
MOVSD 例:
; Test29_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
ddSource dd 11h,22h,33h
ddDest dd lengthof ddSource dup(?)
.code
main proc
lea esi, ddSource
lea edi, ddDest
mov ecx, lengthof ddSource
cld
rep movsd
DumpMem offset ddDest, sizeof ddDest ;11 00 00 00 - 22 00 00 00 - 33 00 00 00
ret
main endp
end main
MOVSW 例:
; Test29_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
ddSource dw 11h,22h,33h
ddDest dw lengthof ddSource dup(?)
.code
main proc
lea esi, ddSource
lea edi, ddDest
mov ecx, lengthof ddSource
cld
rep movsw
DumpMem offset ddDest, sizeof ddDest ;11 00 22 00 - 33 00 00 00
ret
main endp
end main
CMPSD 例:
; Test29_5.asm
.386p
.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
ddVal1 dd 1234h
ddVal2 dd 5678h
.code
main proc
lea esi, ddVal1
lea edi, ddVal2
cmpsd
je L1
PrintText '兩數不等'
jmp L2
L1: PrintText '兩數相等'
L2: ret
main endp
end main
CMPSW 例:
; Test29_6.asm
.386p
.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
dwArr1 dw 1,2,3,4,5
dwArr2 dw 1,3,5,7,9
.code
main proc
lea esi, dwArr1
lea edi, dwArr2
mov ecx, lengthof dwArr1
cld
repe cmpsw
je L1
PrintText '兩數組不等'
jmp L2
L1: PrintText '兩數組相等'
L2: ret
main endp
end main
對比數組時, 假如數組長度不一致...
; Test29_7.asm
.386p
.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
dwArr1 dw 1,2,3,4,5
dwArr2 dw 1,2,3,4,5,6
.code
main proc
lea esi, dwArr1
lea edi, dwArr2
mov ecx, lengthof dwArr1
cmp ecx, lengthof dwArr2
jne L1
cld
repe cmpsw
jne L1
PrintText '兩數組相等'
jmp L2
L1: PrintText '兩數組不等'
L2: ret
main endp
end main
如果對比的是 0 結束的字符串, 長度不一致也不用考慮
; Test29_8.asm
.386p
.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
szText1 db 'Delphi 2010', 0
szText2 db 'Delphi 2011', 0
.code
main proc
lea esi, szText1
lea edi, szText2
mov ecx, lengthof szText1
cld
repe cmpsb
je L1
PrintText '字符串不同'
jmp L2
L1: PrintText '字符串相同'
L2: ret
main endp
end main
SCASB 例:
; Test29_9.asm
.386p
.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 'ABCDEFGH', 0
.code
main proc
lea edi, szText
mov al, 'F'
mov ecx, lengthof szText - 1
cld
repne scasb
je L1
PrintText '沒找到'
jmp L2
L1: sub ecx, lengthof szText - 1
neg ecx
PrintDec ecx ;如果找得到, 這裡顯示是第幾個字符; 本例結果是 6
L2: ret
main endp
end main
STOSB 例:
; Test29_10.asm
.386p
.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
len = 31
szText db len dup(0), 0
.code
main proc
lea edi, szText
mov al, 'x'
mov ecx, len
cld
rep stosb
PrintString szText ;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ret
main endp
end main
LODSW 例: 數組求和
; Test29_11.asm
.386p
.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
dwArr dw 1,2,3,4,5,6,7,8,9,10
.code
main proc
lea esi, dwArr
mov ecx, lengthof dwArr
xor edx, edx
xor eax, eax
@@: lodsw
add edx, eax
loop @B
PrintDec edx ;55
ret
main endp
end main