liveBook · Manning

Label

As explained in Classic Game Programming on the NES: Make your own retro video game

A label is a symbolic identifier assigned to a code location so assembly instructions can refer to that location without specifying its numeric address.

A label gives a readable name to a place in assembly code. Use the name as the destination of a jump, branch, or subroutine call, and let the assembler calculate the machine-code address during compilation. This avoids hard-coding addresses and keeps code easier to change. section 4.4.1 section 4.4.4

Define a normal label with alphanumeric characters that do not begin with a number, followed by a colon. Reference it without the colon. Label case does not matter. section 4.4.4

Jumping to named code

`jmp NEWCODE` transfers execution to the instruction immediately after `NEWCODE:`. The label marks the destination without requiring its numeric memory address. section 4.4.4

Choose label scope to keep names manageable:

  • Use normal named labels for destinations that need a clear, reusable name. section 4.4.4
  • Use `.proc` and `.endproc` to place labels inside a separate procedure scope, preventing clashes with labels in other code. section 4.4.4
  • Use cheap local labels beginning with `@` for short loops; they remain valid only until the next normal label. section 4.4.4
  • Use unnamed labels with `:` and relative references such as `:-` or `:++` for nearby control flow. section 4.4.4

Branches can also target labels, but their destination must remain within the instruction’s limited forward or backward range. The assembler reports an Out of Range error when a branch cannot reach its label. section 4.4.2

Listing 4.25 Example cheap local label
ClearMemory:          #1
    lda #$00
    ldy #$20
@Loop:    
    dey
sta Mem,y
    bne @Loop
    rts
Sub:                  #2
    bne @Loop         #3

Inspect how `@Loop` works inside `ClearMemory:` and why it cannot be referenced after the later `Sub:` label starts a new scope. section 4.4.4

Meaningful names can also stand for fixed NES values. Define a register or controller value once, then use its name throughout the program while the assembler substitutes the actual value. section 5.3

How Classic Game Programming on the NES: Make your own retro video game covers it

How deep it goes
Defines label syntax and scopes, explains assembler address resolution, and notes branch-range errors without detailing machine-code encoding.
Examples
Demonstrates `NEWCODE`, a `ClearMemory` procedure with `@Loop`, unnamed loop labels, and named PPU, APU, and controller values.
What it assumes
Introduces label syntax directly while using 6502 instructions, registers, branches, and assembler compilation as surrounding context.
What you can do after
Apply it via an assembler to write readable control-flow targets and scoped loop labels without manually calculating addresses.
How it connects
Connects labels to jumps, branches, subroutine calls, procedures, assembler compilation, and NES PPU, APU, and controller values.

Also explained in

Compare how 3 books explain label →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Game Programming on the NES: Make your own retro video game. The section links point to the parts of the book it draws on.