6.0 Data Access: 8051 Addressing Modes
An addressing mode is the method by which the CPU accesses an operand for an instruction. The 8051 provides several addressing modes, each offering a different way to specify the location of the source or destination data. Mastering these modes is essential for writing efficient and flexible assembly code.
6.1 Immediate Addressing
In this mode, the operand is the actual data value itself, not an address. The value is “immediately” available to the CPU. The hash symbol (#) is used to signify an immediate operand.
Example: MOV A, #6AH This instruction moves the hexadecimal value 6A directly into the Accumulator (Register A).
6.2 Direct Addressing
In this mode, the operand is the memory address of the data. The instruction uses this 8-bit address to access a location in the internal RAM (including SFRs). Note the absence of the # symbol.
Example: MOV A, 04H This instruction reads the data stored at RAM address 04H (which corresponds to register R4 of Bank 0) and moves it into the Accumulator.
6.3 Register Direct Addressing
This mode is similar to direct addressing, but the operand is the name of a register itself, which holds the data.
Example: MOV A, R4 This instruction moves the contents of register R4 into the Accumulator. The assembler knows the memory address of R4 based on the currently selected register bank.
6.4 Register Indirect Addressing
In this mode, a register is used as a pointer to the address of the data. The at symbol (@) indicates that the value inside the specified register is the address of the operand, not the data itself.
Example: MOV A, @R0 This instruction checks the value inside register R0. If R0 contains the value 20H, the CPU will go to RAM address 20H, read the data stored there, and move it into the Accumulator. Only registers R0 and R1 can be used for register indirect addressing.
6.5 Indexed Addressing
This mode is used for accessing data from program memory (ROM) and involves adding an offset to a base address to find the final target address. It is primarily used for reading look-up tables.
Example: MOVC A, @A+DPTR This instruction adds the 8-bit value in the Accumulator (A) to the 16-bit value in the Data Pointer (DPTR). The resulting 16-bit address is used to read a byte from program memory, which is then moved into the Accumulator. A similar instruction, MOVC A, @A+PC, uses the Program Counter as the base address.
After learning how to access data, the next logical step is to learn how to manipulate it and control the flow of a program using the 8051’s instruction set.