Strings
To the CPU a string is just a sequence of bytes in memory. Everything else — where it ends, how it is encoded, how it compares — is a convention that your code implements with ordinary loops. This lesson writes the classic string routines by hand; the next one shows the special instructions x86 provides for them.
Two ways to know where a string ends
| Convention | Example | Used by |
|---|---|---|
| explicit length | msg: db "Hi" + msg_len equ $ - msg |
system calls (write takes a length), Rust, Go |
| zero-terminated ("C strings") | msg: db "Hi", 0 |
the C library, most OS APIs |
With a terminating zero byte the length is not stored anywhere: you have to scan for the zero. That is what C's strlen does.
strlen: find the terminating zero
section .data
city: db "Reykjavik", 0
empty: db 0
section .text
global _start
_start:
mov rdi, city
call strlen
mov rdi, rax
call print_uint
mov rdi, empty
call strlen
mov rdi, rax
call print_uint
mov eax, 60
xor edi, edi
syscall
; strlen(str) -> number of bytes before the terminating 0
strlen:
xor eax, eax ; index = 0
.scan:
cmp byte [rdi + rax], 0
je .done
inc rax
jmp .scan
.done:
ret
; ---- 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
ret9
0
Printing a C string
Combine strlen with write and you get a puts-like helper that prints any zero-terminated string followed by a newline:
section .data
line1: db "Zero-terminated strings", 0
line2: db "know their own end", 0
newline: db 10
section .text
global _start
_start:
mov rdi, line1
call puts
mov rdi, line2
call puts
mov eax, 60
xor edi, edi
syscall
; puts(str): print a zero-terminated string and a newline
puts:
push rdi ; strlen does not change rdi, but be safe
call strlen
pop rsi ; buffer
mov rdx, rax ; length
mov eax, 1
mov edi, 1
syscall
mov eax, 1
mov edi, 1
mov rsi, newline
mov edx, 1
syscall
ret
; strlen(str) -> number of bytes before the terminating 0
strlen:
xor eax, eax
.scan:
cmp byte [rdi + rax], 0
je .done
inc rax
jmp .scan
.done:
retZero-terminated strings
know their own end
strcmp: comparing strings
Two strings are compared byte by byte until the bytes differ or both end. Like C's strcmp, the function returns a negative number, zero or a positive number — here the difference of the first differing bytes:
section .data
apple: db "apple", 0
apply: db "apply", 0
app: db "app", 0
section .text
global _start
_start:
mov rdi, apple
mov rsi, apply
call strcmp
mov rdi, rax
call print_int ; 'e' - 'y' < 0: apple comes first
mov rdi, apple
mov rsi, apple
call strcmp
mov rdi, rax
call print_int ; equal: 0
mov rdi, apple
mov rsi, app
call strcmp
mov rdi, rax
call print_int ; 'l' - 0 > 0: the longer string is greater
mov eax, 60
xor edi, edi
syscall
; strcmp(a, b) -> <0, 0 or >0
strcmp:
movzx eax, byte [rdi]
movzx edx, byte [rsi]
cmp eax, edx
jne .differ
test eax, eax ; both ended at the same time?
jz .differ ; then eax - edx = 0
inc rdi
inc rsi
jmp strcmp
.differ:
sub eax, edx
movsxd rax, eax ; return a signed 64-bit value
ret
; ---- print_int: print rdi as a signed decimal number and a newline ----
print_int:
test rdi, rdi
jns print_uint ; not negative: print it as it is
neg rdi ; make it positive
push rdi ; keep it (write and syscall clobber registers)
push '-' ; the minus sign, as a byte on the stack
mov eax, 1 ; write(1, rsp, 1)
mov edi, 1
mov rsi, rsp
mov edx, 1
syscall
pop rdi ; drop the '-'
pop rdi ; the positive value again
jmp print_uint ; print_uint's ret returns to our caller
; ---- 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
ret-20
0
108
Copying and concatenating
strcpy copies bytes including the terminator. Returning a pointer to the copied zero makes concatenation easy: the next copy simply starts there.
section .data
first: db "assembly", 0
middle: db " is ", 0
last: db "fast", 0
section .bss
buffer: resb 64
section .text
global _start
_start:
mov rdi, buffer ; destination
mov rsi, first
call strcpy ; rax = end of the copied text
mov rdi, rax
mov rsi, middle
call strcpy
mov rdi, rax
mov rsi, last
call strcpy
mov byte [rax], 10 ; replace the final 0 with a newline
lea rdx, [rax + 1]
sub rdx, buffer ; total length including the newline
mov eax, 1
mov edi, 1
mov rsi, buffer
syscall
mov eax, 60
xor edi, edi
syscall
; strcpy(dst, src) -> pointer to the 0 written at the end of dst
strcpy:
mov al, [rsi]
mov [rdi], al
test al, al
jz .done
inc rsi
inc rdi
jmp strcpy
.done:
mov rax, rdi
retassembly is fast
Warning:
strcpyhas no idea how big the destination is. Copying a long string into a small buffer overwrites whatever follows it in memory — the classic buffer overflow. Real code passes the buffer size and stops in time.
Text is bytes: ASCII and UTF-8
Each character in these examples is one byte of ASCII. Text in other languages is usually UTF-8, where a character can take 1 to 4 bytes ("é" is C3 A9). A byte-counting strlen then returns the number of bytes, not characters, and the case trick from the Bitwise lesson only works for A–Z. For anything beyond ASCII, treat strings as opaque byte sequences or use a library.
Test yourself
How does strlen find the length of a C string?
- It reads a length byte before the string
- It scans for the terminating zero byte
- It asks the kernel
- It uses $ - label
What does strcmp return when the first string is alphabetically smaller?
- 0
- A positive number
- A negative number
- 1
Why is the end-pointer returned by strcpy useful?
- It frees memory
- The next string can be copied right there, concatenating without re-scanning
- It is needed by write
- It isn't