Lesson 14 of 15
String Operations
Working with Strings
In assembly, strings are just sequences of bytes in memory. You manipulate them by loading and storing individual bytes.
Reading Bytes from Memory
Use the BYTE size specifier to access individual bytes:
movzx rax, BYTE [rsi] ; load one byte, zero-extend to 64 bits
mov BYTE [rdi], al ; store low byte of rax
movzx (move with zero-extension) loads a small value and fills the upper bits with zeros.
Iterating Over a String
To process each character of a null-terminated string:
lea rsi, [mystring]
loop:
movzx rax, BYTE [rsi]
cmp rax, 0 ; check for null terminator
je done
; ... process character in rax ...
inc rsi ; advance to next character
jmp loop
done:
Computing String Length
Count characters until you hit a null byte (0) or a known terminator.
Your Task
Write a program that computes the length of the string "Hello" (which is 5 characters) and prints it as a digit followed by a newline.
The string is stored in the data section with a null terminator. Loop through it, counting characters until you reach the null byte.
Print: "5\n"
x86_64 runtime loading...
Loading...
Click "Run" to execute your code.