# Length of a string

[Courses](https://asm-editor.specy.app/learn/courses.md) › [x86-64 assembly](https://asm-editor.specy.app/learn/courses/x86.md) › Examples

Web page: https://asm-editor.specy.app/learn/courses/x86/examples/string-length

> Find a string's length with an ordinary loop and a bounded repne scasb scan, then practice handling a missing terminator.

The same program in: [M68K](https://asm-editor.specy.app/learn/courses/m68k/examples/string-length.md), [MIPS](https://asm-editor.specy.app/learn/courses/mips/examples/string-length.md), [RISC-V](https://asm-editor.specy.app/learn/courses/risc-v/examples/string-length.md), [Z80](https://asm-editor.specy.app/learn/courses/z80/examples/string-length.md)

A zero-terminated string has no stored character count. To find its length, read bytes until the
first zero. This example finds the length with an ordinary loop and with `repne scasb`. The
program leaves the loop's result in `r8` and the scan's result in `r9`. Both searches are limited
to the bytes declared for `text`.

```x86
default rel
global _start

section .data
text:           db "assembly", 0
text_end:
TEXT_CAPACITY   equ text_end - text

section .text
_start:
    lea rsi, [text]             ; first byte
    xor rcx, rcx                ; offset, also the length so far
.next:
    cmp rcx, TEXT_CAPACITY
    jae .loop_missing           ; no byte remains to check
    cmp byte [rsi + rcx], 0
    je .loop_done               ; zero is not a character
    inc rcx
    jmp .next
.loop_done:
    mov r8, rcx
    jmp .scan_start
.loop_missing:
    mov r8, -1                  ; no zero within the capacity

.scan_start:
    lea rbx, [text]             ; save the starting address
    lea rdi, [text]             ; scasb reads from rdi
    mov rcx, TEXT_CAPACITY
    test rcx, rcx
    jz .scan_missing            ; no comparison for an empty range
    xor eax, eax                ; al = zero, the byte to find
    cld                         ; advance toward higher addresses
    repne scasb
    jnz .scan_missing           ; last comparison was not zero
    mov r9, rdi
    sub r9, rbx                 ; bytes examined, including the zero
    dec r9                      ; characters before the zero
    jmp .done
.scan_missing:
    mov r9, -1
.done:
    mov rax, 60
    xor rdi, rdi
    syscall
```

Run it and inspect `r8` and `r9` in the register panel: both are **8**. The ordinary loop tests
the capacity _before_ reading `[rsi + rcx]`. At offsets 0 through 7 it sees the letters; at
offset 8 it sees zero and copies `rcx` to `r8`. `TEXT_CAPACITY` is 9 because `db` writes eight
letters and the explicit terminator. The `text_end` label marks the address just after those
nine bytes; it stores no byte itself.

The `byte` in `cmp byte [rsi + rcx], 0` is required. `[rsi + rcx]` gives an address and the
immediate `0` has no width, so NASM cannot tell whether to compare one, two, four, or eight
bytes without an explicit size.

For the second search, `scasb` compares `al` with the byte at `rdi` and then advances `rdi` by
one because `cld` cleared the direction flag. `repne` repeats that comparison while `rcx` is
nonzero and the compared byte does not match `al`. It decrements `rcx` after every comparison. Here it
examines nine bytes, including the zero: on the match, `rdi = text + 9`, `rcx = 0`, and the
zero flag is set. `rdi - text` therefore gives the number of bytes examined, 9. Subtracting one excludes the
terminator and gives the length 8. The `jnz` checks the comparison result immediately after
the scan. If the capacity runs out on a nonzero byte, it takes the missing-terminator path.

Try replacing the declaration with `text: db "abcdefghi"`, leaving `text_end` in place. Those
nine declared bytes contain no zero. Each search reads only those bytes and leaves **-1** (the
64-bit pattern `0xFFFFFFFFFFFFFFFF`) in its result register. Restore the original declaration
afterward. The capacity is the number of accessible bytes supplied to the search; it does not
promise that a zero is present. A program using this pattern must know that capacity before it
starts reading.

## Your turn

Complete the two ordinary loops below. For each string, check its capacity before reading a
byte, count the nonzero bytes, and put **-1** in the result register if no zero occurs within
that capacity. `first` contains `"Hi!"` and a zero, so `r10` should be **3**. `second` has five
nonzero bytes and no terminator, so `r12` should contain **-1**, represented as
`0xFFFFFFFFFFFFFFFF`. Use **Test** to check both values, then inspect the registers.
Checking the five-byte capacity before each read keeps the second loop from reading at or
beyond `second_end`.

*Exercise: the code to start from, checked by the testcase after it:*

```x86
default rel
global _start

section .data
first:          db "Hi!", 0
first_end:
FIRST_CAPACITY  equ first_end - first
second:         db "abcde"
second_end:
SECOND_CAPACITY equ second_end - second

section .text
_start:
    lea rsi, [first]
    xor rcx, rcx
.first_next:
    ; Check FIRST_CAPACITY before reading a byte.
    ; On zero, put rcx in r10. On exhaustion, put -1 in r10.

.second_start:
    lea rsi, [second]
    xor rcx, rcx
.second_next:
    ; Check SECOND_CAPACITY before reading a byte.
    ; On zero, put rcx in r12. On exhaustion, put -1 in r12.

.done:
    mov rax, 60
    xor rdi, rdi
    syscall
```

*Testcase for the program above (JSON):*

```json
{
    "expectedRegisters": {
        "r10": 3,
        "r12": "0xFFFFFFFFFFFFFFFF"
    }
}
```

<details>
<summary>Show solution</summary>

*Solution:*

```x86
default rel
global _start

section .data
first:          db "Hi!", 0
first_end:
FIRST_CAPACITY  equ first_end - first
second:         db "abcde"
second_end:
SECOND_CAPACITY equ second_end - second

section .text
_start:
    lea rsi, [first]
    xor rcx, rcx
.first_next:
    cmp rcx, FIRST_CAPACITY
    jae .first_missing
    cmp byte [rsi + rcx], 0
    je .first_found
    inc rcx
    jmp .first_next
.first_found:
    mov r10, rcx
    jmp .second_start
.first_missing:
    mov r10, -1

.second_start:
    lea rsi, [second]
    xor rcx, rcx
.second_next:
    cmp rcx, SECOND_CAPACITY
    jae .second_missing
    cmp byte [rsi + rcx], 0
    je .second_found
    inc rcx
    jmp .second_next
.second_found:
    mov r12, rcx
    jmp .done
.second_missing:
    mov r12, -1

.done:
    mov rax, 60
    xor rdi, rdi
    syscall
```

</details>

## Instructions in this Lecture

- x86-64: [`lea`](https://asm-editor.specy.app/documentation/x86/instruction/lea.md), [`xor`](https://asm-editor.specy.app/documentation/x86/instruction/xor.md), [`cmp`](https://asm-editor.specy.app/documentation/x86/instruction/cmp.md), [`jae`](https://asm-editor.specy.app/documentation/x86/instruction/jae.md), [`je`](https://asm-editor.specy.app/documentation/x86/instruction/je.md), [`inc`](https://asm-editor.specy.app/documentation/x86/instruction/inc.md), [`jmp`](https://asm-editor.specy.app/documentation/x86/instruction/jmp.md), [`mov`](https://asm-editor.specy.app/documentation/x86/instruction/mov.md), [`test`](https://asm-editor.specy.app/documentation/x86/instruction/test.md), [`cld`](https://asm-editor.specy.app/documentation/x86/instruction/cld.md), [`scasb`](https://asm-editor.specy.app/documentation/x86/instruction/scasb.md), [`jne`](https://asm-editor.specy.app/documentation/x86/instruction/jne.md), [`sub`](https://asm-editor.specy.app/documentation/x86/instruction/sub.md), [`dec`](https://asm-editor.specy.app/documentation/x86/instruction/dec.md), [`syscall`](https://asm-editor.specy.app/documentation/x86/instruction/syscall.md)

Previous: [The largest element](https://asm-editor.specy.app/learn/courses/x86/examples/max-of-an-array.md)

Next: [Reverse a string in place](https://asm-editor.specy.app/learn/courses/x86/examples/reverse-a-string.md)
