liveBook · Manning

.byte directive

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

An assembler directive that emits specified literal byte values into program data.

The .byte directive places one or more literal byte values into assembled program data. It is useful for fixed NES data such as palettes and screen text stored in the RODATA segment. section 5.8

Use .byte in several practical forms:

  • Write hexadecimal values separated by commas, such as .byte $0F,$15,$26,$37, to define palette entries. section 5.8
  • Write characters or quoted text, such as .byte 'W','E','L','C','O','M','E',0, to create a sequence of individual bytes. section 5.8 section 6.7
  • Append a zero byte when a routine needs an explicit end marker for text. section 6.7

Palette and welcome text

Place fixed data in RODATA, then label it so game code can refer to it: .byte $0F,$15,$26,$37 defines four palette bytes, while .byte 'W','E','L','C','O','M','E',0 defines text ending in zero. section 5.8

Listing 5.8 RODATA code segment (example.s)
;*****************************************************************
; Our default palette table has 16 entries for tiles
; and 16 entries for sprites
;*****************************************************************
 
.segment "RODATA"
default_palette:
.byte $0F,$15,$26,$37        #1
.byte $0F,$09,$19,$29        #2
.byte $0F,$01,$11,$21        #3
.byte $0F,$00,$10,$30        #4
.byte $0F,$18,$28,$38        #5
.byte $0F,$14,$24,$34        #6
.byte $0F,$1B,$2B,$3B        #7
.byte $0F,$12,$22,$32        #8
  
welcome_txt:
.byte 'W','E','L','C', 'O', 'M', 'E', 0

Inspect the complete RODATA listing showing eight palette rows and a zero-terminated welcome message. section 5.8

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

How deep it goes
Explains hexadecimal, character, quoted-string, and terminator forms, then demonstrates their use in palette and title-screen data.
Examples
Uses default palette rows, a welcome message, game-title text, and a Press Fire to Begin message.
What it assumes
Uses NES ROM segments and palette or screen concepts while demonstrating the byte declarations needed for those data structures.
What you can do after
Build it from scratch by declaring palette bytes and zero-terminated text for NES screen output.
How it connects
Connects literal bytes to ROM, RODATA segments, palette tables, text strings, and text-writing routines.

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.