100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Strings in Assembly

How byte-sequence strings are terminated, measured, copied, and compared in x86 assembly, including the REP-prefixed string instructions.

Memory & AddressingIntermediate9 min readJul 10, 2026
Analogies

Strings in Assembly

Assembly has no native string type; a string is simply a sequence of bytes in memory, and its length must be tracked by convention. The two dominant conventions are null-terminated strings, where a single 0x00 byte marks the end (as in C and most x86 system code), and length-prefixed strings, where a count precedes the data. Every string operation — copying, comparing, finding a character — must respect whichever convention the code was written for, since mixing them silently misreads memory.

🏏

Cricket analogy: A cricket over is conventionally over the moment six legal balls are bowled, with no separate counter object — the umpire just watches for the sixth ball, the way null-terminated code watches for the terminating zero byte.

Computing String Length and Copying

A classic strlen implementation scans forward from the string's start, incrementing a counter until it reads a zero byte, then returns the counter; this is an O(n) scan performed every single time the length is needed, since the length is never cached. Copying a string similarly walks both source and destination pointers in lockstep, copying one byte at a time and stopping only after the null terminator itself has been copied, so the destination buffer must be at least as large as the source including that terminator.

🏏

Cricket analogy: Working out a batting partnership's total by adding each ball-by-ball run one at a time, rather than reading a pre-summed total, mirrors strlen's byte-by-byte scan instead of a cached length field.

nasm
section .data
    msg:    db "Hello, Assembly!", 0   ; null-terminated string

section .text
global _start

strlen:                     ; rdi = pointer to string, returns length in rax
    xor     rax, rax
.next:
    cmp     byte [rdi + rax], 0
    je      .done
    inc     rax
    jmp     .next
.done:
    ret

_start:
    mov     rdi, msg
    call    strlen          ; rax now holds 16
    mov     eax, 60
    xor     edi, edi
    syscall

Comparing and Searching Strings

String comparison walks two buffers together, comparing corresponding bytes until either a mismatch is found or both strings hit their null terminator simultaneously, at which point they're equal. x86 provides the CMPSB instruction paired with the REPE prefix specifically for this: REPE CMPSB repeats byte comparison while ZF is set and RCX is nonzero, stopping the instant a mismatch occurs, which is exactly how a hand-rolled strcmp loop behaves but expressed as a single repeated instruction.

🏏

Cricket analogy: Comparing two teams' over-by-over scoring rates ball by ball until the first over where they diverge is exactly how REPE CMPSB compares bytes until the first mismatch halts the repeat.

REPE CMPSB (and SCASB, MOVSB used this way) reads or writes memory through RSI/RDI without any bounds checking. If two buffers being compared are not actually null-terminated as expected, the instruction will keep scanning past the intended buffer into unrelated memory until it happens to find a matching or mismatching byte — a classic source of buffer over-reads.

  • Assembly strings are raw byte sequences; the language enforces no type, so the ending convention (null-terminated or length-prefixed) must be tracked by the programmer.
  • Null-terminated strings end with a single 0x00 byte; length must be discovered by scanning, as with a hand-written or REPNE SCASB-based strlen.
  • strlen-style scans are O(n) and are not cached, so repeated length queries re-scan the whole string each time.
  • Copying a null-terminated string must include the terminator itself, and the destination buffer must be large enough to hold source length + 1.
  • REPE CMPSB compares bytes while ZF is set and RCX is nonzero, halting at the first mismatch, mirroring a hand-written comparison loop.
  • String instructions like CMPSB, SCASB, and MOVSB perform no bounds checking, so mismatched buffer sizes or missing terminators cause reads past the intended memory.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#AssemblyLanguageStudyNotes#StringsInAssembly#Strings#Assembly#Computing#String#StudyNotes#SkillVeris#ExamPrep