The instruction set of a Microcontroller (or any Microprocessor) determines how the CPU understands and executes the instructions. One of the important parts of an Instruction Set is the Addressing Modes. If you are familiar with Assembly Language Programming, you might have an idea on the structure of the instruction (Op-Code followed by Operands). The addressing modes help us optimally insert operands into our program. In this guide, we will look at different 8051 Microcontroller Addressing Modes.
This will be a short and simple guide on Addressing Modes. It will help you understand the Instruction Set a little bit better. But the main benefit of learning these addressing modes is you can write better Assembly Programs for 8051 Microcontroller.
What is an Addressing Mode?
An Addressing mode refers to a specific method that the microcontroller uses to access operands during instruction execution. It refers to how we specify the location of an operand that is required by an instruction. Using Addressing Modes, you can specify the source and destination of data in different ways.
In simple words, addressing modes are various ways of specifying the operand in an instruction.
That’s a lot of definitions. You will understand better when we look at the individual addressing modes in 8051 Microcontroller with a few examples.
8051 Microcontroller Addressing Modes
The 8051 Microcontroller (Intel MCS-51 family to be specific) allows five types of Addressing Mode. They are:
- Immediate Addressing Mode
- Direct Addressing Mode
- Register Addressing Mode
- Register Indirect Addressing Mode
- Indexed Addressing Mode
Depending on the programming situation, you as a developer can use any of these addressing modes to specify the source or destination data in the instruction.
Let us look at these 8051 Microcontroller Addressing Modes in detail in the next section. But before that, here’s a quick look at the structure of an instruction to the 8051 Microcontroller:
Mnemonic Destination Operand, Source Operand
The ‘Mnemonic’ part of the instruction is known as Op-Code (short for Operation Code). This specifies the type of operation to be performed.
Next, we have Operand(s). This is the data on which the CPU performs the operation as per the Op-code.
Depending on the Op-Code, we can have one or two operands. If there are two, then the first one (next to the Op-code) is the Destination Operand. The one on the right is the Source Operand.
The addressing modes come into play on how we implement these Operands in the instruction.
Immediate Addressing Mode
The Immediate Addressing Mode is the simplest and fastest way to assign data. Using Immediate Addressing Mode, we can include the data directly within the instruction itself. In Immediate Addressing Mode, the Source Operand is a Constant Data.
Almost all instructions that use Immediate Addressing use an 8-bit Data. The only exception is when you are working with the Data Pointer (DPTR). In this case, the data can be of 16-bits in length.
In this mode, the operand value appears as part of the instruction and does not require any additional memory access.
For example, in the instruction MOV A, #25H, the microcontroller directly loads the value 25H into the accumulator. The # symbol before the value indicates that the operand is an immediate constant and not a memory address. Hence the name Immediate Addressing Mode.
We can use Immediate Addressing when we need to work with constant values that do not change during the program’s execution.
Examples of Immediate Addressing
Below are some examples that illustrate Immediate Addressing Mode.
- MOV R2, #0F2H ; This instruction loads the hexadecimal value F2H directly into register R2.
- MOV DPTR, #0A3H ; This example sets the data pointer (DPTR) to the 16-bit value A3H.
- ADD A, #12H ; This instruction adds the constant value 12H to the contents of the accumulator.
Direct Addressing Mode
In Direct Addressing Mode, we can specify the memory address of the operand directly within the instruction. The instruction with Direct Address Mode includes the explicit address where the data resides.
For example, the instruction MOV A, 30H moves the contents of memory location 30H to the accumulator. Here, ‘30H’ is the address or memory location of the data.
The 8051 microcontroller supports internal RAM addresses in the range 00H – 7FH and special function registers (SFRs) in the range 80H – FFH for Direct Addressing.
When the program specifies an address within 00H – 7FH, it refers to internal RAM. On the other hand, addresses between 80H – FFH point to SFRs, which control hardware functions like timers, input/output ports, control registers etc.
Examples of Direct Addressing Mode
We frequently use Direct Addressing Mode to access lower 128-Bytes of RAM (including Register Banks) and all the SFRs. Here are a couple of examples illustrating its use:
- MOV A, 30H ; This instruction transfers the data from internal RAM address 30H to the accumulator.
- MOV 40H, R2 ; This operation stores the value of register R2 in the memory location 40H.
- SETB P1.3 ; This command sets bit 3 of Port 1 by directly referencing the corresponding SFR Bit address.
Register Addressing Mode
Using Register Addressing Mode, the microcontroller can access operands stored directly in its working registers. In Register Addressing Mode, we specify the Register name that contains the target data as an operand in the instruction. Since the data lies directly in the Register, this mode is also known as Register Direct Addressing.
In the 8051 microcontroller, these registers include R0 through R7, located within the currently selected register bank.
For example, in the instruction MOV A, R1, the value stored in register R1 moves to the accumulator.
If you remember the 8051 Microcontroller Memory Organization, the lower 128 Bytes of the RAM has four register banks (Bank 0 — Bank 3), each containing eight registers (R0 — R7). We can select the register bank using specific control bits in the Program Status Word (PSW).
Using these banks, we can switch between different working sets of registers when necessary. By directly referencing these registers through Register Addressing, we can simplify program instructions and reduce processing time. This mode is very useful in scenarios where the microcontroller frequently interacts with small datasets.
Examples of Register Addressing Mode
Register addressing mode is very useful in simple and repetitive operations where speed is a priority. The following instructions are examples of Register Addressing Mode:
- MOV A, R1 ; This instruction copies the data from register R1 to the accumulator.
- ADD A, R5 ; This instruction adds the value in register R5 to the accumulator.
- SUBB A, R2 ; This operation subtracts the value in register R2 from the accumulator, including the borrow bit.
Register Indirect Addressing Mode
The Register Indirect Addressing Mode is a dynamic way to access memory by using a register to hold the memory address of the operand. In the 8051 microcontroller, only two registers R0 and R1 act as pointers to memory locations. The name ‘Register Indirect Addressing Mode’ attests to the fact that the Register is used to hold the address of the data rather than the direct data.
For instance, in the instruction MOV A, @R0, the processor fetches the data from the memory address stored in register R0 and transfers it to the accumulator.
The ‘@’ symbol in the instruction indicates that the address is stored indirectly in the register, rather than being specified explicitly in the instruction.
By using Indirect Addressing, we can efficiently manage larger data sets, especially when working with arrays, buffers, or other sequential data structures.
Unlike direct addressing, which requires fixed addresses in the instruction, Register Indirect Addressing uses R0 or R1 as pointers. By changing the values stored in these registers, the program can access different memory locations without altering the instruction itself.
Examples of Register Indirect Addressing Mode
Register indirect addressing is very useful in operations that require accessing memory locations based on runtime data. Some examples are:
- MOV A, @R0 ; This instruction moves the data from the memory address stored in R0 to the accumulator.
- MOV @R1, 50H ; Here, the data 50H is stored at the memory location pointed to by register R1.
- INC @R0 ; This operation increments the value stored at the memory address held in R0.
Indexed Addressing Mode
Indexed Addressing Mode is particularly useful for accessing (reading) external program memory. If there are any look-up tables in program memory, then we can use Indexed Addressing to access that data.
In Indexed Addressing Mode, the microcontroller calculates the effective memory address by combining the contents of a base register with an immediate offset.
The 8051 microcontroller uses either the Data Pointer (DPTR) or the Program Counter (PC) as the base register. The Accumulator (A) will hold the offset address.
For instance, the instruction MOVC A, @A+DPTR retrieves data from the external program memory at the location determined by the sum of the accumulator (A) and the value in the DPTR register.
We can use Indexed Addressing Mode with the 8051 microcontroller to retrieve constants, lookup table values, or pre-stored data from the program memory.
When the instruction uses the DPTR, the program retrieves data from any external memory location. Alternatively, when the PC serves as the base, the program accesses data stored relative to the current instruction address.
Examples of Indexed Addressing Mode
Here are some examples of Indexed Addressing Mode:
- MOVC A, @A+DPTR ; This instruction retrieves a byte from the external program memory at an address calculated as DPTR + A.
- MOVC A, @A+PC ; This operation reads a byte from the program memory at an address determined by the sum of PC + A.
Other Addressing Modes
Apart from these, some authors and books (including the original Intel MCS-51 User Manual) included several other addressing modes. Here are some of them:
- Register-Specific Instructions: Some instructions, which are specific to certain registers (A or DPTR), do not need any address of the data.
- Implicit Addressing Mode: In this, we need not provide an operand explicitly. The instruction implicitly implies the operand (or the operation).
- Absolute Addressing Mode: Absolute Addressing is a specific addressing mode in the 8051 microcontroller used exclusively by the AJMP (Absolute Jump) and ACALL (Absolute Call) instructions. Absolute addressing uses the instruction itself to specify the lower 11 bits of the target memory address. The instruction takes the upper 5 bits of the target memory address from the upper 5 bits of the current program counter.
- Long Addressing Mode: The Long Addressing Mode is associated with the LJMP (Long Jump) and LCALL (Long Call) instructions. It takes a 16-bit address and can jump or call a subroutine anywhere within the 64K Byte code memory.
Summary of 8051 Microcontroller Addressing Modes
The following table summarizes each addressing mode, along with a brief explanation and examples for better understanding.
Addressing Mode | Description | Examples |
Immediate Addressing | The operand is directly provided in the instruction. It does not need a memory lookup. | 1. MOV A, #25H (Move immediate value 25H to accumulator) 2. MOV R2, #0F2H (Move 0F2H to R2) |
Register Addressing | The operand is located in one of the internal registers (R0 to R7). | 1. MOV A, R1 (Move value from R1 to A) 2. ADD A, R5 (Add value of R5 to A) |
Direct Addressing | The operand’s address is explicitly specified in the instruction. | 1. MOV A, 30H (Move value from address 30H to A) 2. MOV 40H, R2 (Move value of R2 to address 40H) |
Register Indirect Addressing | The operand’s address is stored in a register (either R0 or R1 only). The content of the register points to memory. | 1. MOV A, @R0 (Move value from address in R0 to A) 2. MOV @R1, 50H (Move value 50H to address in R1) |
Indexed Addressing | Combines a base register (DPTR or PC) with an immediate offset (A) to calculate the address. | 1. MOVC A, @A+DPTR (Access external memory with base DPTR) 2. MOVC A, @A+PC (Access program memory with base PC) |
Implicit Addressing | The operand is implied in the instruction itself, typically for flags or the accumulator. | 1. CPL A (Complement accumulator) 2. CLR C (Clear carry flag) |
Conclusion
In this guide, I explained all the Addressing Modes of the 8051 Microcontroller. If you are one of the few people who still wants to write Assembly Language Programs for 8051 Microcontroller, then you need to understand these Addressing Modes.
With the knowledge of 8051 Microcontroller Addressing Modes and the 8051 Microcontroller Instruction Set, you can write very efficient assembly programs.