跳到內容

movsb & scasb & cmpsb

將內容從rsi指向的地址搬到rdi指向的地址,b表示一次搬動一個byte,像是這樣:

mov [rdi], [rsi]
; 遞增rdi, rsi
mov [rdi], [rsi]
......
; 此mov偽指令不可執行,來源和目的不可以都是地址

將hello每次8 bytes移動到temp

section .data
hello: dq 'Hello, W'
dq 'orld!',10
section .bss
temp: resq 2
global _start
section .text
_start:
mov rsi, hello
mov rdi, temp
movsq ; temp="Hello, W" 使用x/s $rdi-8
movsq ; temp="orld!\n" 使用x/s $rdi-16 或 x/2gx $rsi-16
mov rax, 60
xor rdi, rdi
syscall

使用rep重複,先將rcx設成循環次數再執行rep movsq

section .data
hello: dq 'Hello, W'
dq 'orld!',10
section .bss
temp: resq 2
global _start
section .text
_start:
mov rsi, hello
mov rdi, temp
mov rcx, 2
rep movsq
mov rax, 60
xor rdi, rdi
syscall

從rdi指向的地址跟al做cmp比對,並將rdi遞增,可搭配rep/repe/repne

單次scasq判斷8個bytes是否與rax內容相等

section .data
hello: dq 'Hello, W'
dq 'orld!',10
global _start
section .text
_start:
mov rdi, hello
mov rax, 'Hello, W'
scasq ; 使用gdb查看 i r eflags, x/s $rdi
mov rax, 60
xor rdi, rdi
syscall

使用repne重複scasb直到遇到\0,rcx為搜索長度

section .data
hello: db "hello", 0
global _start
section .text
_start:
mov rdi, hello
xor rax, rax
mov rcx, 6
repne scasb ; 最後rsi位址在\0的地方
mov rax, 60
xor rdi, rdi
syscall

將rdi指向的地址內容與rsi依序遞增比對
cmp [rsi], [rdi] 然後將rdi遞增和rsi遞增
使用後rdi和rsi會被改成新的位址, 比如cmpsq會分別+8
可以搭配rep/repe/repne在相等或不相等的情況下最多重複rcx次

section .data
data1: db "abcxy"
data2: db "abcqw"
global _start
section .text
_start:
mov rdi, data1
mov rsi, data2
mov rcx, 5
repe cmpsb ; 最後做完cmp x, q並遞增,會停在y和w
mov rax, 60
xor rdi, rdi
syscall