String Instructions
x86 has a family of instructions designed for walking through memory: they use rsi as the source pointer, rdi as the destination pointer and rcx as a counter, and they advance the pointers automatically. Combined with a rep prefix, a single instruction can copy, fill or search a whole block of memory.
The instructions
| Instruction | Effect (byte version) | Then |
|---|---|---|
lodsb |
al = [rsi] |
rsi++ |
stosb |
[rdi] = al |
rdi++ |
movsb |
[rdi] = [rsi] |
rsi++, rdi++ |
scasb |
compare al with [rdi] (sets flags) |
rdi++ |
cmpsb |
compare [rsi] with [rdi] (sets flags) |
rsi++, rdi++ |
Each also exists for 2, 4 and 8 bytes: lodsw/lodsd/lodsq, stosq, movsq and so on, which use ax/eax/rax and step by the element size.
The direction flag (DF) decides whether the pointers go up (DF = 0) or down (DF = 1). The ABI guarantees DF = 0 at function boundaries, so normally they go up; cld clears it explicitly and std sets it (restore it with cld afterwards!).
The rep prefixes
| Prefix | Repeats while | Use with |
|---|---|---|
rep |
rcx != 0 (decrementing rcx each time) |
movs, stos |
repe / repz |
rcx != 0 and the last comparison was equal |
cmps, scas |
repne / repnz |
rcx != 0 and the last comparison was not equal |
scas, cmps |
rep stosb: fill memory
memset(buffer, value, count) in three set-up instructions and one rep stosb:
section .bss
line: resb 31
section .text
global _start
_start:
mov rdi, line ; destination
mov al, '=' ; value
mov ecx, 30 ; count
rep stosb ; 30 times: [rdi] = al, rdi++
mov byte [rdi], 10 ; rdi now points just past the filled part
mov eax, 1
mov edi, 1
mov rsi, line
mov edx, 31
syscall
mov eax, 60
xor edi, edi
syscall==============================
rep movsb: copy memory
memcpy(dst, src, count) is rep movsb. Modern CPUs implement it with a fast microcoded path ("ERMSB"), so for medium and large blocks it is one of the best ways to copy:
section .data
source: db "copy me, please!", 10
source_len equ $ - source
section .bss
dest: resb 64
section .text
global _start
_start:
mov rsi, source
mov rdi, dest
mov ecx, source_len
rep movsb ; copy source_len bytes
mov eax, 1
mov edi, 1
mov rsi, dest
mov edx, source_len
syscall
mov eax, 60
xor edi, edi
syscallcopy me, please!
repne scasb: search for a byte
scasb compares al with the byte at [rdi]. With repne it keeps going until it finds a match (or rcx runs out). Searching for 0 gives a compact strlen: start with rcx = -1 (practically unlimited). When the scan stops, rdi points one byte past the zero, so the length is rdi - start - 1:
section .data
planet: db "Neptune", 0
sentence: db "find the comma, please", 0
section .text
global _start
_start:
; strlen with repne scasb
mov rdi, planet
mov rbx, rdi ; remember the start
xor eax, eax ; al = 0: the byte to find
mov rcx, -1 ; no limit
repne scasb ; stops after the 0 byte
sub rdi, rbx
dec rdi ; don't count the 0 itself
call print_uint ; 7
; position of the first ',' (memchr / strchr)
mov rdi, sentence
mov al, ','
mov ecx, 22 ; search at most the whole sentence
repne scasb
jne .not_found ; ZF=0: rcx ran out without a match
sub rdi, sentence
dec rdi ; rdi went one past the match
call print_uint ; index of the comma
.not_found:
mov eax, 60
xor edi, edi
syscall
; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
mov rax, rdi ; rax = the number to convert
sub rsp, 32 ; room for up to 20 digits and a newline
lea rsi, [rsp + 31] ; fill the buffer from its end
mov byte [rsi], 10 ; the newline is the last character
mov ecx, 10
.next_digit:
xor edx, edx ; the dividend is rdx:rax, so clear rdx
div rcx ; rax = quotient, rdx = remainder (0..9)
add dl, '0' ; remainder -> ASCII digit
dec rsi
mov [rsi], dl ; store it in front of the previous one
test rax, rax
jnz .next_digit ; repeat until the quotient is 0
lea rdx, [rsp + 32]
sub rdx, rsi ; rdx = number of bytes to print
mov eax, 1 ; write(1, rsi, rdx)
mov edi, 1
syscall
add rsp, 32
ret7
14
repe cmpsb: compare blocks
repe cmpsb walks two buffers while their bytes are equal. Afterwards the flags tell you whether it stopped on a difference (ZF = 0) or ran through all rcx bytes (ZF = 1) — a memcmp:
section .data
password: db "s3cret!"
attempt1: db "s3cret?"
attempt2: db "s3cret!"
LEN equ 7
ok_msg: db "match", 10
bad_msg: db "no match", 10
section .text
global _start
_start:
mov rsi, password
mov rdi, attempt1
call check
mov rsi, password
mov rdi, attempt2
call check
mov eax, 60
xor edi, edi
syscall
; check(rsi, rdi): print whether LEN bytes are equal
check:
mov ecx, LEN
repe cmpsb ; stops at the first difference
jne .different
mov rsi, ok_msg
mov edx, 6
jmp .print
.different:
mov rsi, bad_msg
mov edx, 9
.print:
mov eax, 1
mov edi, 1
syscall
retno match
match
Note:
repe cmpsbstops at the first difference, so its running time reveals where two secrets differ. Real password checks use a constant-time comparison that always looks at every byte.
lodsb and stosb in a loop
Without a rep prefix, lodsb/stosb are handy for transform loops: load a byte, change it, store it. This converts a string to uppercase in place:
section .data
shout: db "quiet please", 10
shout_len equ $ - shout
section .text
global _start
_start:
mov rsi, shout
mov rdi, shout ; same buffer: read and write in place
mov ecx, shout_len
.next:
lodsb ; al = [rsi++]
cmp al, 'a'
jb .store
cmp al, 'z'
ja .store
sub al, 'a' - 'A' ; to uppercase
.store:
stosb ; [rdi++] = al
dec ecx
jnz .next
mov eax, 1
mov edi, 1
mov rsi, shout
mov edx, shout_len
syscall
mov eax, 60
xor edi, edi
syscallQUIET PLEASE
Test yourself
Which registers does rep movsb use?
- rax, rbx, rcx
- rsi (source), rdi (destination), rcx (count)
- rdi only
- rsp and rbp
What does repne scasb do?
- Copies bytes until a zero
- Scans [rdi] for the byte in al, stopping at the first match or when rcx reaches 0
- Compares two strings
- Fills memory with al
What does the direction flag control?
- Whether jumps go forward or backward
- Whether string instructions increment or decrement rsi/rdi
- The stack direction
- The byte order