Strings and arrays

Contents

Strings and arrays#

A string in assembly is just a sequence of bytes the operator treats as one. C strings end with a null byte; the operator emits db "hello", 0 (NASM) or .asciz "hello" (GAS).

A read-only string with a trailing newline and null.

section .rodata
msg:    db "hello, operator", 0xa, 0

A strlen-style scan in pure assembly.

section .text
xor     rcx, rcx
.loop:
    cmp     byte [rsi + rcx], 0
    je      .done
    inc     rcx
    jmp     .loop
.done:
    ; rcx = length

For repeated byte / word operations, x86-64’s rep prefix plus the string instructions (movsb, stosb, cmpsb, scasb).

; memcpy(rdi, rsi, rcx)
cld                            ; clear direction flag -> forward
rep movsb

References#

  • Addressing modes for the [rsi + rcx] form used in scans.

  • I/O for syscalls that consume these byte buffers.