Skip to content
systemcoding.dev
Assembly

Why 6502 Assembly Still Teaches Modern CPU Concepts

A 1975 processor with 56 instructions still teaches what every modern CPU builds on: flags, addressing modes, the stack, memory-mapped I/O and interrupts.

7 min read
Summarize in AI
Flat illustration of an assembly listing with addresses and mnemonics, with the Arm, RISC-V and AMD logos.

The MOS 6502 shipped in 1975 and powered the Apple II, the Commodore 64, the Atari 2600 and the NES. It runs nothing you use today. So why would anyone learn its assembly language in the year a phone carries a deeply out-of-order core? Because the 6502 shows you the bones. Every idea you will meet in x86-64, Arm or RISC-V is present on the 6502 in a form small enough to understand completely: condition flags, addressing modes, a stack, interrupt vectors, memory-mapped hardware. The newer chips pile machinery on top of those ideas, and the machinery hides them. This post lists the concepts the 6502 teaches best, shows each in code, and says plainly what it does not teach.

If you want the whole path from registers to interrupts, the complete 6502 and Z80 guide is the cluster hub. This post answers one narrower question: what carries over.

Flags: the same idea in every ISA

Every CPU records facts about the last arithmetic result in condition bits. The 6502 has N, Z, C and V. x86-64 has SF, ZF, CF and OF in RFLAGS, and AArch64 has N, Z, C and V in NZCV. The names are nearly identical because the idea is. A conditional branch reads those bits, and a compare is a subtraction whose result is thrown away and whose flags are kept.

The 6502 makes the mechanism visible because the flags are few and every instruction's effect is documented in one line. Consider a countdown loop in the 6502 and the same loop in modern assembly:

countdown.sasm
        LDX #$0A
again:  DEX                 ; sets Z when X reaches zero
        BNE again           ; branch while Z is clear
countdown_x64.sasm
        mov  ecx, 10
again:  dec  ecx            ; sets ZF when ecx reaches zero
        jnz  again          ; branch while ZF is clear

The two loops are the same program. The decrement sets the zero flag and the branch tests it, with no separate comparison. If you understand why BNE works on the 6502, you understand why jnz works on x86-64, and why a compiler can drop the explicit cmp after a dec.

Text
6502:   X = $00, Z = 1 after the tenth DEX
x86-64: ECX = 0, ZF = 1 after the tenth DEC

Addressing modes: what a pointer really is

High-level languages hide the difference between a value and the address of a value. The 6502 puts it in the syntax. LDA #$05 loads the number 5. LDA $05 loads the byte stored at address 5. LDA ($05),Y reads a 16-bit pointer from addresses 5 and 6, adds Y and loads from the result. That last form is a pointer dereference with an index: ptr[i] in C.

strlen.cc
unsigned char length(const unsigned char *s)
{
    unsigned char n = 0;
    while (s[n] != 0)
        n++;
    return n;
}
strlen.sasm
ptr     = $20               ; zero-page pointer to the string

length: LDY #$00
loop:   LDA (ptr),Y         ; s[n]
        BEQ done
        INY                 ; n++
        BNE loop            ; stop after 255 so Y cannot wrap forever
done:   TYA                 ; return n in A
        RTS

The mapping is direct. ptr is the parameter, Y is the index n, LDA (ptr),Y is s[n], and BEQ is the loop test. On x86-64 the same thing is movzx eax, byte ptr [rdi + rcx] and on AArch64 it is ldrb w0, [x0, x1]. Base register plus index register, the shape of every array access you will ever read in a disassembly, is the 6502's indirect indexed mode. Seeing it done with three registers and two zero-page bytes makes the general form obvious.

The stack and the calling convention

The 6502 stack is a 256-byte page at $0100, with an 8-bit pointer that counts down. JSR pushes a return address and RTS pops it. That is the whole mechanism by which every language implements a function call. On a modern CPU it is hidden behind call, ret and a link register, and the return-address predictor makes it fast, but the idea is the same: a call saves where to come back to.

The 6502 also shows what a calling convention is, because the language has none. You choose. If a routine takes one byte and returns one byte, A is the natural channel. If it needs more, you agree on zero-page locations. A beginner who writes five subroutines and has to remember which one clobbers X has learned why ABIs specify caller-saved and callee-saved registers. The modern conventions are bigger versions of the same bookkeeping.

Overflow of the stack is just as concrete. With 256 bytes and two bytes per call, 128 nested calls fill the page and the next push wraps around and silently overwrites the oldest entries. Deep recursion on a 6502 is a bug you can trigger in a minute and reason about in your head, and it carries over exactly to a stack overflow on a PC, where the page is merely larger.

Memory-mapped I/O: hardware as addresses

The 6502 has no separate I/O instructions. A peripheral lives at an address, and a load or store talks to it. On the Commodore 64, writing to $D020 changes the border color:

border.sasm
        LDA #$02            ; color 2 is red
        STA $D020           ; the VIC-II border register

The CPU does not know it has touched a video chip. The address decoder routes the write there. This is the model that every microcontroller register uses, from the STM32 GPIOA->ODR to a PCIe device's BAR. Reading a datasheet register table and writing a byte to an address is the same act on a tiny microcontroller and on a server. Because volatile exists in C for exactly this reason (the compiler must not remove or reorder a store that has side effects), the 6502 explains why volatile is needed.

Interrupts and vectors

On reset the 6502 loads its program counter from the word at $FFFC. On a maskable interrupt it loads from $FFFE, and on a non-maskable one from $FFFA. A vector table, a list of handler addresses in a fixed place, is how every architecture dispatches interrupts, from the x86 interrupt descriptor table to the Arm Cortex-M table at the start of flash. The 6502 has three entries. Cortex-M has dozens. Same shape.

The handler also teaches the discipline that interrupt code requires. The CPU saves PC and the flags, but not A, X or Y, so a handler that touches them must save and restore them by hand or corrupt the interrupted program. Modern kernels do exactly this in an entry stub, which the RISC-V trap handler walkthrough in this cluster builds in full.

Two's complement and carry

Signed numbers, unsigned numbers and the carry between bytes all appear in the first week of 6502 programming, because the chip has only 8 bits and a number that does not fit forces you to deal with it. Adding two 16-bit values needs a carry between two 8-bit additions. Comparing signed values needs the overflow flag. Both are the foundation of multi-precision arithmetic, and arbitrary-precision libraries still use the same loop, with a 64-bit limb instead of an 8-bit one.

6502 conceptWhere it reappears
ADC with the carry flagadc on x86-64 and AArch64, adcs for big-integer limbs
CMP setting carryunsigned compare on every ISA
V flagsigned overflow detection, jo and b.vs
SEC before SBCborrow handling in multi-word subtraction
BRKsoftware interrupts, int3, brk #imm
JMP ($xxxx)indirect jumps, function pointers, jump tables

Cost you can count

An 8-bit 6502 has no cache, no pipeline reordering and no branch predictor. Every instruction has a published cycle count, and the sum is the run time. That makes it the one processor where performance is arithmetic, not measurement. The datasheet says LDA $10 takes 3 cycles and LDA $1234 takes 4, and the difference is the reason the zero page exists. A student who sees why a 1-cycle difference matters will find it easier to understand why a cache miss matters on a modern core, because the same question applies: where is the data, and how many steps away?

What the 6502 does not teach

Be honest about the gaps. The 6502 has no privilege levels, so it teaches nothing about system calls, kernels or protection. It has no virtual memory, no memory-ordering rules, no atomic instructions and no multi-core. It has no SIMD and no floating-point unit. Its calling convention is whatever you invent. And its code is hand-scheduled, so it teaches nothing about how a compiler's output looks, which is the form in which you will meet assembly most often.

If your goal is to read compiler output or to write an operating system, finish the 6502 material and then move to x86-64, Arm or RISC-V, where the missing pieces are. The 6502 is the first rung, and the point is to take the ideas with you.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.