What is an addressing mode?
In a recent post I referenced addressing modes. But what the heck are they!?
Setting the stage
The instruction register holds the program instruction that is currently being run.
A fixed number of bits within the instruction register represent the operation, e.g. “op. code” — examples of these instructions include things like add, subtract, load, and store. We can imagine the instruction register like this:
There’s a fixed number of bits allocated to the op. code (the 6 left-most boxes), and then a fixed number of bits that hold the operand/s being operated on (remaining 10 boxes). An operand could be a value, a CPU register, or a memory address. This set of fixed bits is referred to as the “address field.”
The number of bits allocated to the address field determines the amount of memory that can be addressed. The number of bits allocated to the op. codes determines how expressive the op. codes can be (or at least how many of them there can be).
Addressing modes provide different ways to use the addressable memory.
In my diagram, 2 bits of the operation code are used to determine the addressing mode. The addressing mode tells the processor how the bits in the address field should be interpreted.
LDA #80 LDA $80
These similar looking instructions are pretty different.
# tells us that the number following is a literal value.
$ tells us that the number following references a memory address.
LDA #80 loads the literal decimal value 80 into the A register and
LDA $80 loads the value located at memory address
$80 into the A register.
#80 is known as immediate mode because we are directly, or immediately, loading a value, while
$80 is known as absolute, or zero page, mode.
What about a literal hex value?
This loads the literal hex value
$80 (e.g. 128) into the A register.
- Check this out, from 6502.org for more info on addressing modes.
- One of my favorite ways that I’ve seen things at least adjacent to addressing modes implemented is through the use of runes. Day 1 of compdanzas’ uxn tutorial includes info on this.