Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 40 ยท Arrays, Strings and Structs

Strings and Byte Processing

Process NUL-terminated strings in assembly: write strlen, strcpy, upper-casing and reverse loops, then see what rep movsb, stosb and scasb really do.

At the machine level there is no such thing as a string. There are only bytes in memory, and a convention about where the text ends. C and most operating-system interfaces use the simplest convention: the text is a run of bytes followed by a single zero byte, the NUL terminator. Everything else, strlen, strcpy, upper-casing, reversing, is a loop that walks those bytes until it meets the zero.

In this lesson you will write those loops yourself in a toy assembly language, see how ASCII makes case conversion a one-bit operation, and meet the x86 string instructions (movsb, stosb, scasb with the rep prefix) that fold a whole loop into a single instruction. The addressing form [base + index] used here comes from Arrays and Addressing Modes, with a scale of 1 because every element is one byte.

Bytes, ASCII and the terminator

The text "Hi" is stored as three bytes: 0x48 (H), 0x69 (i) and 0x00. The first two are ASCII codes; the third is the terminator.

flowchart LR
  A["0x48 'H'"] --> B["0x69 'i'"] --> C["0x00 NUL, end of string"]

A C string is its bytes plus a trailing zero; the length is not stored anywhere.

That has three consequences you will see in every routine below:

  • Finding the length costs a loop over the whole string, so strlen is O(n).
  • Copying a string means copying the terminator too, or the copy never ends.
  • A buffer one byte too small silently breaks the convention, which is behind a famous class of bugs (Security: Buffer Overflows and Defenses comes back to this).

Loads and stores of single bytes use the byte size: in Intel syntax movzx eax, BYTE PTR [rsi] loads one byte and zero-extends it to the full register, so the upper bits are clean. mov BYTE PTR [rdi], al stores the low byte of rax. Compilers pick movzx for unsigned char and movsx for plain signed char; see MOV, LEA and Moving Data for the difference.

Real text may be UTF-8, where one character can take several bytes. Every routine in this lesson works on bytes, not characters, which is exactly right for strlen and strcpy, but means the upper-casing trick below only handles plain ASCII letters.

strlen and strcpy

Here is the C source for the two classics, with the loops spelled out:

C
size_t my_strlen(const char *s) {
    size_t n = 0;
    while (s[n] != 0) n++;
    return n;
}

void my_strcpy(char *dst, const char *src) {
    size_t i = 0;
    do { dst[i] = src[i]; } while (src[i++] != 0);
}

A simple assembly version of strlen (Intel syntax, rdi holds the string pointer under System V) looks like this:

NASM
my_strlen:
    xor  eax, eax                  ; length = 0
.Lloop:
    cmp  BYTE PTR [rdi+rax], 0     ; is s[length] the terminator?
    je   .Ldone
    inc  rax
    jmp  .Lloop
.Ldone:
    ret

You cannot run native code on this site, so the demo below uses the toy CPU again: a JavaScript interpreter with byte-addressable memory, a handful of Intel-style instructions and a puts pseudo-instruction that prints the NUL-terminated string at the address in a register. cmp remembers both operands and the conditional jumps compare them, a simplification of the real flags covered in Flags. The program measures the string at 0x40, then copies it, terminator included, to 0x80.

JS strlen and strcpy in toy assembly

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The length printed is 12, but the copy loop moves 13 bytes: the extra one is the terminator. Delete the cmp rax, 0 / jne pair in your mind and you get the classic bug: a copy that stops one byte early leaves the destination unterminated.

Case conversion and the ASCII trick

The ASCII table was designed so that upper-case and lower-case letters differ by exactly one bit. A is 0x41, a is 0x61: the difference is 0x20, bit 5. Therefore:

  • to make a letter lower-case, set bit 5 (or al, 0x20);
  • to make it upper-case, clear bit 5 (and al, 0xDF), or subtract 32;
  • to swap the case, flip it (xor al, 0x20).

This works only for the 52 letters. Applied to a digit or a space, the same operation produces garbage: '1' is 0x31, and setting bit 5 does nothing to it, but clearing bit 5 of 0x2D (-) gives 0x0D, a carriage return. So a correct routine first checks the range 'a' to 'z' (97 to 122). The next program does that, then reverses the string in place with two pointers that move toward each other.

JS Upper-case and reverse a string in place

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that digits, the comma, the space and the exclamation mark pass through the range checks untouched. The reverse loop stops when the two pointers meet or cross, so it works for both even and odd lengths, and an empty string never enters the loop body.

The string instructions

x86 has a family of one-byte-opcode instructions made for this kind of loop. Each works on fixed registers and advances them for you:

Instruction Does one step Registers used
movsb copy a byte from [rsi] to [rdi] rsi, rdi
stosb store al at [rdi] rdi, al
lodsb load [rsi] into al rsi, al
scasb compare al with [rdi] (sets flags) rdi, al
cmpsb compare [rsi] with [rdi] rsi, rdi

After each step the pointers move by one byte: forward if the direction flag DF is 0 (the normal state, set with cld) and backward if DF is 1 (std). The ABIs require DF to be clear on entry to and exit from every function. Word, doubleword and quadword variants exist (movsw, movsd, movsq, with a, ax, eax, rax for the data register).

The rep prefix repeats the instruction rcx times, decrementing rcx each step. repe/repne also stop early depending on the zero flag (ZF) and are meant for scas and cmps. So rep movsb is a memory copy, rep stosb is a memory fill, and repne scasb can search for a byte:

NASM
; memcpy(rdi, rsi, rdx)
mov  rcx, rdx
rep  movsb

; memset(rdi, 0, rcx)
xor  eax, eax
rep  stosb

; strlen(rdi) the classic way
xor  eax, eax          ; search for byte 0
mov  rcx, -1           ; "infinite" count
repne scasb
not  rcx
dec  rcx               ; rcx = length

The strlen trick is worth working through. rcx starts at all ones. Each step subtracts 1, including the step that finds the zero. For "hello" there are six steps (five letters plus the NUL), so rcx ends at -1 - 6 = -7. not rcx gives 6 (since not x = -x - 1), and dec rcx gives 5.

You will rarely see repne scasb in modern compiler output. C libraries implement strlen with vector instructions that examine 16 or 32 bytes at a time, which is much faster. rep movsb and rep stosb, on the other hand, are quick on recent processors for larger blocks and are used for memcpy/memset in some cases. For tiny sizes compilers simply emit a few plain mov instructions. The model below implements the three instructions and the direction flag in JavaScript with 64-bit BigInt registers, so rcx = -1 really wraps.

JS Model rep movsb, rep stosb and repne scasb

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last two lines show why DF exists: when source and destination overlap and the destination is higher, copying forward overwrites bytes before they are read. Starting at the end and going backward (std before the instruction) produces the right answer. That is what a correct memmove must do.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does a C string tell the program where it ends?

    C strings are the character bytes followed by a 0x00 terminator. Nothing stores the length, so strlen must scan until it finds the zero.
  2. 2. Which operation turns the ASCII letter 'a' (0x61) into 'A' (0x41)?

    Upper-case and lower-case ASCII letters differ only in bit 5 (0x20). Clearing it (or subtracting 32) gives upper case. Setting it would give lower case, and adding 32 would go the wrong way.
  3. 3. After mov rcx, -1 and repne scasb finds the NUL of "cat", what does rcx hold?

    There are 4 steps (c, a, t and the NUL), and rcx decrements once per step, including the step that finds the zero: -1 - 4 = -5. Then not rcx gives 4 and dec gives the length 3.
  4. 4. Why does the direction flag exist for string instructions?

    With DF set, the pointers step toward lower addresses. That lets a copy whose destination overlaps the end of the source run safely, as memmove requires.

Summary

  • A string is bytes plus a 0x00 terminator; its length is found by scanning, and copying must include the terminator.
  • Byte loops use scale 1 addressing, movzx to load and a byte store; the routines work on bytes, not Unicode characters.
  • ASCII upper and lower case differ only in bit 5 (0x20); check the letter range before changing it.
  • rep movsb, rep stosb and repne scasb fold copy, fill and search loops into one instruction, driven by rsi, rdi, rcx and the direction flag.
  • Libraries often use vector code instead of these instructions for small or search-heavy work, so do not expect to see repne scasb in optimized output.

Next: Structs, Padding and Alignment, where fields sit at fixed offsets and the compiler inserts gaps.