TExcelTAD
Home › x86-64 Assembly › Local Labels

Local Labels

Every loop and every if needs labels, and a program with many functions quickly runs out of good names: how many functions want a label called loop or done? NASM solves this with local labels: labels that start with a dot belong to the most recent non-local label.

How they work

nasm
count_spaces:           ; a normal ("global" to the file) label
.loop:                  ; really named count_spaces.loop
    ...
    jmp .loop           ; refers to count_spaces.loop
.done:                  ; count_spaces.done
    ret

count_digits:           ; a new normal label starts a new scope
.loop:                  ; count_digits.loop: no clash!
    ...

Inside a function you just write .loop; NASM silently prepends the name of the enclosing normal label. You have been using this all along: print_uint contains .next_digit, which is really print_uint.next_digit.

nasm · try it yourself
section .data
text:       db "a1 b22 c333 4"
TEXT_LEN    equ $ - text

section .text
global _start
_start:
    mov rdi, text
    mov esi, TEXT_LEN
    call count_spaces
    mov rdi, rax
    call print_uint

    mov rdi, text
    mov esi, TEXT_LEN
    call count_digits
    mov rdi, rax
    call print_uint

    mov eax, 60
    xor edi, edi
    syscall

; count_spaces(ptr, len) -> number of ' ' bytes
count_spaces:
    xor eax, eax
.loop:
    test rsi, rsi
    jz .done
    cmp byte [rdi], ' '
    jne .next
    inc rax
.next:
    inc rdi
    dec rsi
    jmp .loop
.done:
    ret

; count_digits(ptr, len) -> number of '0'..'9' bytes
count_digits:
    xor eax, eax
.loop:                          ; the same names again: no conflict
    test rsi, rsi
    jz .done
    movzx ecx, byte [rdi]
    sub ecx, '0'
    cmp ecx, 9
    ja .next
    inc rax
.next:
    inc rdi
    dec rsi
    jmp .loop
.done:
    ret

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
3
7

The full name

A local label's full name is parent.local, and you can use it from anywhere — for example to jump into the middle of another routine, or to make a debugger breakpoint:

nasm · try it yourself
section .text
global _start
_start:
    mov edi, 10
    call countdown.from_ten     ; skip countdown's own setup
    mov eax, 60
    xor edi, edi
    syscall

countdown:
    mov edi, 3                  ; normally starts at 3
.from_ten:
    push rdi
    call print_uint
    pop rdi
    sub rdi, 4
    ja .from_ten                ; while rdi > 0 (unsigned, no borrow)
    ret

; ---- print_uint: print rdi as an unsigned decimal number and a newline ----
print_uint:
    mov rax, rdi            ; rax = the number to convert
    sub rsp, 32             ; room for up to 20 digits and a newline
    lea rsi, [rsp + 31]     ; fill the buffer from its end
    mov byte [rsi], 10      ; the newline is the last character
    mov ecx, 10
.next_digit:
    xor edx, edx            ; the dividend is rdx:rax, so clear rdx
    div rcx                 ; rax = quotient, rdx = remainder (0..9)
    add dl, '0'             ; remainder -> ASCII digit
    dec rsi
    mov [rsi], dl           ; store it in front of the previous one
    test rax, rax
    jnz .next_digit         ; repeat until the quotient is 0
    lea rdx, [rsp + 32]
    sub rdx, rsi            ; rdx = number of bytes to print
    mov eax, 1              ; write(1, rsi, rdx)
    mov edi, 1
    syscall
    add rsp, 32
    ret
10
6
2

Using the full name from outside is legitimate, but it breaks the idea of local labels, so do it rarely.

The scope trap

The scope changes at every non-local label — including one you put in the middle of a function. Afterwards, .loop refers to a different label than before:

nasm · try it yourself
section .text
global _start
_start:
    mov ecx, 3
.loop:
    dec ecx
halfway:                        ; a normal label starts a new scope...
    jnz .loop                   ; ...so this means halfway.loop, which doesn't exist

    mov eax, 60
    xor edi, edi
    syscall
<source>:8: error: symbol `halfway.loop' not defined

If you need a named point inside a function, make it local too (.halfway:).

Labels starting with ..@

NASM has two more special prefixes:

Prefix Meaning
.name local to the previous normal label
..@name a normal label that does not start a new local scope (useful inside macros)
%%name inside a macro: unique for each expansion (see Macros)
..start and other .. names reserved for special purposes

Naming conventions

Good label names make assembly much easier to read:

  • Functions: snake_case verbs — print_uint, find_by_id.
  • Inside functions: .loop, .next, .done, .found, .not_found, .else, .end_if — names that describe the role of the place.
  • Data: nouns — buffer, inventory, msg_len.
  • Constants: UPPER_CASE — COUNT, SYS_WRITE.

Test yourself

Inside the function parse:, what is the full name of the label .digit?

  1. .digit
  2. parse.digit
  3. digit.parse
  4. _start.digit

Why can two different functions both contain a .loop label?

  1. NASM ignores duplicate labels
  2. Each becomes function.loop, so the full names differ
  3. Only the last one is kept
  4. They can't

What starts a new local-label scope?

  1. A ret instruction
  2. Any non-local label (one that doesn't start with a dot)
  3. A blank line
  4. A section directive