Understanding Strings in 6502 Assembly

Published on: Februrary 17, 2025

Introduction

In high-level languages, strings are built-in data types. However, in 6502 Assembly, strings are simply sequences of bytes stored in memory, typically terminated with a null marker ($00).

WELCOME_MESSAGE:
    dcb "H", "E", "L", "L", "O", $0D, $00  ; $0D for newline, $00 for termination
      

Why Strings Matter?

Strings are crucial for tasks like printing, user input, and data processing. Since the 6502 processor lacks built-in string handling, we must loop through each byte until reaching the null terminator.

Printing Characters and Strings

Unlike modern languages, printing requires manual memory management. To print 'A', we load its ASCII value and store it at the screen memory location.

LDA #$41      ; Load ASCII for 'A'
STA $F000     ; Store at screen address
      

Using ROM Routines for Output

Many 6502-based systems provide built-in ROM routines to simplify printing.

LDA #$41      ; Load ASCII for 'A'
JSR $FFD2     ; Call CHROUT to print
      

Printing a String with a Loop

Printing a full string requires looping until reaching the null terminator.

PRINT_STRING:
    LDY #$00              ; Start at beginning
LOOP_PRINT:
    LDA STRING, Y         ; Load next character
    BEQ PRINT_DONE        ; If 0, end of string
    JSR CHROUT            ; Print character
    INY                   ; Increment pointer
    BNE LOOP_PRINT        ; Continue looping
PRINT_DONE:
    RTS
      

Why This Matters?

Understanding how to print characters and strings in 6502 assembly is key for UI creation, debugging, and effective memory management.

Handling Input in 6502 Assembly

Handling user input is crucial, whether for a game, utility, or experiment. Learning how to read keypresses and process inputs is essential in 6502 Assembly.