Photo by Lorenzo Herrera on Unsplash
Software Portability and Optimization
Week 2 - Class 2 Continued ...
The 6502 processor, a true legend in the world of computing, boasts a dazzling array of 13 addressing modes. These modes determine how the arguments for instructions are accessed, and they are the key to unlocking the processor's full potential. In this blog post, we'll dive into each addressing mode and provide real-world examples to help you understand how they work.
1. Accumulator
The accumulator addressing mode is as straightforward as it gets. The operation works directly on the accumulator (A), and no additional data is required. Here are some examples:
ASL A ; Shift left the accumulator
LSR A ; Shift right the accumulator
ROL A ; Rotate left the accumulator
ROR A ; Rotate right the accumulator
Most assemblers don't require the explicit 'A' in these instructions, so you can simply write ASL
, LSR
, ROL
, and ROR
.
2. Absolute
In the absolute addressing mode, data is accessed using a 16-bit address specified as a constant. Here are some examples:
LDA $06d3 ; Load the accumulator from memory address $06d3
STX $0200 ; Store the X register into memory address $0200
JMP $5913 ; Jump to the subroutine at memory address $5913
3. Absolute, X
This addressing mode spices things up by adding the value of the X register (with carry) to a 16-bit address constant. Here's how it works:
LDA $8000,x ; Load the accumulator from memory address ($8000 + X)
STA $8000,x ; Store the accumulator into memory address ($8000 + X)
4. Absolute, Y
Similar to absolute, X, the absolute, Y addressing mode adds the value of the Y register (with carry) to a 16-bit address constant:
LDA $8000,y ; Load the accumulator from memory address ($8000 + Y)
STA $8000,y ; Store the accumulator into memory address ($8000 + Y)
5. Immediate
In immediate addressing mode, data is taken directly from the byte following the opcode. Here are a couple of examples:
LDA #$05 ; Load the accumulator with the value 5
ORA #$80 ; Perform a logical OR operation with the accumulator and the value 0x80
6. Implied
Implied addressing mode implies the data by the operation itself. Here are a couple of examples:
CLC ; Clear the Carry flag (implied that this operates on the Status register)
RTS ; Return from subroutne (implied that the return address is taken from the stack)
7. Indirect
In the indirect addressing mode, data is accessed using a pointer. The 16-bit address of the pointer is given in the two bytes following the opcode:
JMP ($9000) ; Jump to the location pointed to by addresses $9000 (low) and $9001 (high)
8. X, Indirect
This addressing mode is a bit unique. It starts with an 8-bit zero-page address, and then the X register is added to it (without carry). This resulting address is used as a pointer to access data:
LDA ($05,x) ; If x=4, the pointer at $09 (and $0a) will be used, loading the accumulator from the indicated address
9. Indirect, Y
Indirect, Y addressing mode involves an 8-bit address that identifies a pointer. The value of the Y register is added to the address contained in the pointer:
LDA ($10),y ; If y=4, and the pointer at $10 (and $11) holds $FF00, the accumulator is loaded from address ($FF00 + $04) = $FF04
10. Relative
In relative addressing mode, an 8-bit signed offset is provided. This offset value is added to the program counter (PC) to determine the effective address:
BNE $0600 ; The value "$0600" represents a signed offset, affecting the target address in the range of (-128:127) bytes from the current PC value.
11. Zero Page
The zero-page addressing mode uses an 8-bit address within the zero page. It's similar to absolute addressing, but the argument is only one byte, so the CPU doesn't need an additional cycle to fetch the high byte:
LDX $13 ; Load the X register from memory address $13 in the zero page
AND $07 ; Perform a bitwise AND operation with the accumulator and the value in memory address $07
12. Zero Page, X
In this mode, an 8-bit address is provided, and then the X register is added to it (without carry), effectively pointing to an address within the zero page:
STA $00,x ; Store the accumulator into memory address ($00 + X) in the zero page
LDA $00,x ; Load the accumulator from memory address ($00 + X) in the zero page
13. Zero Page, Y
Similar to zero page, X, this addressing mode uses an 8-bit address, and then the Y register is added to it (without carry):
STA $00,y ; Store the accumulator into memory address ($00 + Y) in the zero page
LDA $00,y ; Load the accumulator from memory address ($00 + Y) in the zero page
With these addressing modes at your disposal, you'll be able to harness the full power of the 6502 processor and write efficient and optimized code for your projects. Whether you're a seasoned assembly language programmer or just curious about the inner workings of CPUs, understanding these addressing modes is a valuable skill. Happy coding!