跳转到内容

movsb & scasb & cmpsb

movsb

将内容从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

scasb

从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