0% found this document useful (0 votes)
507 views16 pages

Addressing Modes

The document summarizes the addressing modes and instruction set of the 8051 microcontroller. It discusses the 5 addressing modes - immediate, register, direct, register indirect, and indexed. It then explains the arithmetic, logic, compare, and rotate instructions. Some key instructions covered are ADD, SUB, AND, OR, XOR, CJNE (compare and jump), RR (rotate right), and RLC (rotate left through carry). Memory locations, registers, flags, and how instructions modify registers are described through examples for many of the core operations.

Uploaded by

GousAttar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
507 views16 pages

Addressing Modes

The document summarizes the addressing modes and instruction set of the 8051 microcontroller. It discusses the 5 addressing modes - immediate, register, direct, register indirect, and indexed. It then explains the arithmetic, logic, compare, and rotate instructions. Some key instructions covered are ADD, SUB, AND, OR, XOR, CJNE (compare and jump), RR (rotate right), and RLC (rotate left through carry). Memory locations, registers, flags, and how instructions modify registers are described through examples for many of the core operations.

Uploaded by

GousAttar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Unit 2: 8051 Instruction set

Addressing Modes:
The CPU can access data in various ways. The data could be in a register, or in
memory, or be provided as an immediate value. These various ways of accessing data are
called addressing modes.
The 8051 have 5 different addressing modes. They are as follows:
1. immediate
2. register
3. direct
4. register indirect
5. indexed

1) Immediate addressing mode:


In this addressing mode data is immediately available & the operand comes
immediately after the opcode.In between operand and opcode pound sign, “#” is used.
This addressing mode can be used to load information into any of the registers, including
the DPTR register.
e.g.:

2) Register addressing mode:


In this addressing mode data is in register & it can be moved to other register. “,”
sign is used between registers.
e.g.

Movement of data between registers (banks) is not allowed. For example, the instruction
“MOV R4, R7″ is invalid.
In these two addressing modes, the operands (data) are either inside the registers or
immediately available. But when data is available in memory (RAM/ROM) then other
modes are used these are:
3) Direct addressing mode:
There are 128 bytes of RAM in the 8051. It is divided as 00 to 1FH for register
banks and stack. 20-2F for bit addressable RAMS & 30-7F is used for scratch pad. The
register bank locations are accessed by the register names of R0 – R7, but data in location
30-7F is accessed by giving address.
In the direct addressing mode, the data is in a RAM memory location whose
address is known, and this address is given as a part of the instruction.

In this mode we can give address of register instead of name.

Stack and direct addressing mode


Another major use of direct addressing mode is the stack. In the 8051 family, only
direct addressing mode is allowed for pushing onto the stack.
Therefore, an instruction such as “PUSH A” is invalid. Pushing the accumulator
onto the stack must be coded as “PUSH 0E0H” where OEOH is the address of register A.
Similarly, pushing R3 of bank 0 is coded as “PUSH 03″.
Direct addressing mode must be used for the POP instruction as well. For
example, “POP 04″ will pop the top of the stack into R4 of bank 0.
Looping is not possible in direct addressing mode.
4) Register indirect addressing mode:
In the register indirect addressing mode, data is at memory location & address of
this memory location is stored in registers R0 and R1 only. They must be preceded by the
“@” sign, as shown below.

Advantage of register indirect addressing mode


One of the advantages of register indirect addressing mode is that it makes
accessing data dynamic rather than static as in the case of direct addressing mode.
Example:
Indexed addressing mode:
Indexed addressing mode is widely used in accessing data elements of look-up
table entries located in the program ROM space. The instruction used for this purpose is
“MOVC A, @A+DPTR”. The 16-bit register DPTR and register A are used to form the
address of the data element stored in on-chip ROM. The “C” means code.
Instruction Set:
Arithmetic Instruction:
Addition:

The instruction ADD is used to add two operands. The destination operand is
always in register A while the source operand can be a register, immediate data, or in
memory. The memory-to-memory arithmetic operations are never allowed in 8051
Assembly language.

Example:
Show how the flag register is affected by the
following instructions.
After the addition, register A (destination)
contains 00 and the flags are as follows:
CY = 1 since there is a carry out from D7.
P = 0 because the number of Is is zero (an
even number).
AC = 1 since there is a carry from D3 to D4.

DA instruction:
The DA (decimal adjust for addition) instruction will correct problem associated
with BCD addition. The DA instruction will add 6 to the lower nibble or higher nibble if
needed; otherwise, it will leave the result alone.

After the program is executed, register


A will contain 72H (47 + 25 = 72).
The “DA” instruction works
only on A.

Subtraction:

For subtraction only one instruction is used. To subtract without carry we have to make
CY = 0.
Example:

If the C Y = 0 after the execution


of SUBB, the result is positive; if
C Y = 1, the result is negative
and the destination has the 2′s
complement of the result. Normally, the result is left in 2′s complement, but the CPL
(complement) and INC instructions can be used to change it. The CPL instruction
performs the 1‘s complement of the operand; then the operand is incremented (INC) to
get the 2′s complement.

Multiplication:
The multiplication and division instructions work only with registers A and B. The
8051 supports byte-by-byte multiplication only.

Division:
The 8051 supports byte over byte division only.
When dividing a byte by a byte, the numerator
must be in register A and the denominator must
be in B. After the DIV instruction is performed, the quotient is in A and the remainder is
in B.
Example:

LOGIC AND COMPARE INSTRUCTIONS


1) AND:
Syntax:

This instruction will


perform a logical AND on
the two operands and place
the result in the
destination. The
destination is normally the
accumulator. The source operand can be a register, in memory, or immediate.

2) OR:
Syntax: ORL destination, source; dest = dest OR source
The destination and source operands are ORed, and the result is placed in the
destination. The ORL instruction can be used to set certain bits of an operand to 1. The
destination is normally the accumulator. The source operand can be a register, in memory or
immediate.

3) XOR:
Syntax: XRL destination, source; dest = dest XOR source
This instruction will perform the XOR operation on the two operands, and place the
result in the destination. The destination is normally the accumulator. The source operand
can be a register, in memory, or immediate.
Example:

CPL A (complement accumulator):


This instruction complements the contents of register A. The complement action
changes the 0s to 1s and the 1s to 0s. This is also called 1’s complement. Although the CPL
instruction cannot be used to complement R0-R7, it does work on PO-P3 ports
Example: MOV A, #55H
CPL A ; A=AAH -01010101 becomes 10101010(AAH)
To get the 2′s complement, all we have to do is to add 1 to the 1’s complement.
Example:
Compare instruction:
Syntax: CJNE destination,source,relative address
In the 8051, the actions of comparing and jumping are combined into a single
instruction called CJNE (compare and jump if not equal). The source operand can be in a
register, in memory, or immediate. The CJNE instruction compares two operands, and
jumps if they are not equal. This instruction affects the carry flag only.

Solution:
1. Yes, it jumps because 55H
and 99H are not equal.
2. A = 55H, its original value
before the comparison.

Rotate Instruction:
1) Rotating to right
Syntax: RR A; rotate right A
In rotate right, the 8 bits of the accumulator are rotated right one
bit, and bit DO exits from the
least significant bit and enters into D7 (most significant bit).

2) Rotating to left
In rotate left, the 8 bits of the accumulator are
rotated left one bit, and bit D7 exits from the MSB
(most significant bit) and enters into DO (least
significant bit).

3) Rotating right through the carry

In RRC A, as bits are rotated from left to right,


they exit the LSB to the carry flag, and the carry flag enters the
MSB. In other words, in RRC A the LSB is moved to CY and
CY is moved to the MSB. In reality, the carry flag acts as if it is part of register A,
making it a 9-bit register.

4) Rotating left through the carry

In RLC A, as bits are shifted from right to


left they exit the MSB and enter the carry flag,
and the carry flag enters the LSB. In other
words, in RCL the MSB is moved to CY (carry flag) and CY is moved to the LSB.

SWAP A
It works only on the accumulator (A). It swaps the lower nibble and the higher
nibble. i.e. the lower 4 bits are put into the higher 4 bits, and the higher 4 bits are put into
the lower 4 bits.
e.g.:

Loop and Jump Instructions:

Looping in the 8051


Repeating a sequence of instructions a certain number of times are called a loop.
In the 8051, the loop action is performed by the instruction “DJNZ reg, label”. In this
instruction, the register is decremented; if it is not zero, it jumps to the target address
referred to by the label.
e.g. MOV R0, #0FFH
Here: DJNZ R0, Here
End
Example:
In the example, the R2 register
is used as a counter. The counter is
first set to 10. In each iteration the
instruction DJNZ decrements R2 and
checks its value. If R2 is not zero, it
jumps to the target address associated
with the label “AGAIN”.
This looping action continues
until R2 becomes zero. After R2
becomes zero, it falls through the loop and executes the instruction
immediately below it, in this case the “MOV R5, A” instruction.
In the DJNZ instruction that the registers can be any of RO – R7.

Loop inside a loop (Nested loop) :


In 8051 the registers are 8-bit that can be count maximum of 256. For repeat an
action more times than 256, we use a loop inside a loop, i.e. nested loop. In a nested loop,
we use two registers to hold the count.
Example:
Write a program to (a) load the accumulator with the value 55H, and (b)
complement the ACC 700 times.
Solution:
Since 700 is larger than 255 (the maximum capacity of any register), we use two
registers to hold the count. The following code shows how to use R2 and R3 for the
count.

In this program, R2 is used to keep the inner loop count. In the instruction “DJNZ
R2, AGAIN”, whenever R2 becomes 0 it falls through and “DJNZ R3, NEXT” is
executed. This instruction forces the CPU to load R2 with the count 70 and the inner loop
starts again. This process will continue until R3 becomes zero and the outer loop is
finished.

Other conditional jumps:


1) JZ (jump if A = 0)
In this instruction the content of register A is checked. If it is zero, it jumps to the target
address.
e.g.:

OVER: ----------
In this program, if either RO or Rl is zero, it jumps to the label OVER. Notice that
the JZ instruction can be used only for register A. It can only check to see whether the
accumulator is zero, and it does not apply to any other register.

Table: Conditional Jump


Instructions

All conditional jumps are short jumps,


meaning that the address of the target
must be within -128 to +127 bytes of the
contents of the program counter (PC).
Example:

Unconditional jump instructions:


In the 8051 there are two unconditional jumps: LJMP (long jump) and SJMP
(short jump).

1) LJMP (long jump):


LJMP is an unconditional long jump. It is a 3-byte instruction in which the first
byte is the opcode, and the second and third bytes represent the 16-bit address in range
0000 H to FFFFH.
e.g.: org 0000h
MOV A, #0FFH
LJMP $
-----------
$ : -----------
The program counter in the 8051 is 16-bit, hence it can access 64K bytes memory
but not all 8051 family members have 64K bytes of on-chip program ROM. Hence it is
necessary to save memory space. For this reason instead of LJMP instruction, SJMP
instruction is used which is a 2-byte instruction. This can save some bytes of memory in
many applications where memory space is in short supply. All conditional jumps such as
JNC, JZ, and DJNZ are also short jumps.

2) SJMP (short jump):


It is 2-byte instruction, the first byte is the opcode and the second byte is the
relative address of the target location. The relative address is in the range of 00 – FFH .It
is divided into forward and backward jumps; that is, within -128 to +127 bytes of
memory relative to the address of the current PC (program counter).
If the jump is forward, the target address can be within a space of 127 bytes from
the current PC. If the target address is backward, the target address can be within -128
bytes from the current PC.

Calculating the short jump address:


All conditional jumps such as JNC, JZ, and DJNZ are also short jumps because
that they are all two-byte instructions. In these instructions the first byte is the opcode and
the second byte is the relative address.
The target address is relative to the value of the program counter. To calculate the
target address, the second byte is added to the PC of the instruction immediately below
the jump.

Example:
Using the following list file, verify the jump forward address calculation.

Solution:
A) Calculation of forward jumps:
1) First notice that the JZ and JNC instructions both jump forward.
2) The target address for a forward jump is calculated by adding the PC of the
following instruction to the second byte of the short jump instruction, which is called the
relative address.
3) In line 4 the instruction “JZ NEXT” has opcode of 60 and operand of 03 at the
addresses of 0004 and 0005.
4) The 03 is the relative address, relative to the address of the next instruction INC
RO, which is 0006. By adding 0006 to 3, the target address of the label NEXT, which is
0009, is generated.
5) In the same way for line 9, the “JNC OVER” instruction has opcode and
operand of 50 and 05 where 50 is the opcode and 05 the relative address.
6) Therefore, 05 is added, the address of instruction “CLR A”, giving 12H, the
address of label OVER.

B) Calculation of backward jumps:


1) In that program list, “JNC AGAIN” has opcode 50 and relative address F2H.
2) When the relative address of F2H is added to 15H, the address of the instruction
below the jump, we have 15H + F2H = 07 (the carry is dropped).
3) Notice that 07 is the address of label AGAIN. Look also at ‘.’SJMP HERE”,
which has 80 and FE for the opcode and relative address, respectively.
4) The PC of the following instruction, 0017H, is added to FEH, the relative
address, to get 0015H, address of the HERE label (17H + FEH = 15H). Notice that FEH
is -2 and 17H + (-2) = 15

Call instruction:
The CALL instruction is used to call a subroutine. In the 8051 there are two call
instructions: LCALL (long call) and ACALL (absolute call).

LCALL (long call):


In this 3-byte instruction, the first byte is the opcode and the second and third
bytes are used for the address of the target subroutine. Therefore, LCALL can be used to
call subroutines located anywhere within the 64K-byte address space of the 8051.
When a subroutine is called, control is transferred to that subroutine, and the
processor saves the PC (program counter) on the stack and begins to fetch instructions
from the new location. After finishing execution of the subroutine, the instruction RET
(return) transfers control back to the caller. Every subroutine needs RET as the last
instruction.

Steps:
1. Upon executing the first “LCALL DELAY”, the address of the instruction right
below it, “MOV A, #OAAH”, is pushed onto the stack, and the 8051 starts to execute
instructions at address 300H.
2. In the DELAY subroutine, first the counter R5 is set to 255 (R5 = FFH);
therefore, the loop is repeated 256 times. When R5 becomes 0, control falls to the RET
instruction, which pops the address from the stack into the program counter and resumes
executing the instructions after the CALL.

Call instruction and stack:


Upon calling a subroutine, the stack keeps track of where the CPU should return
after completing the subroutine. For this reason, we must be very careful in any
manipulation of stack contents. The rule is that the number of PUSH and POP
instructions must always match in any called subroutine. i.e. for every PUSH there must
be a POP.

Example:

ACALL (absolute call):


ACALL is a 2-byte instruction. The target address of the subroutine must be within 2K
bytes because only 11 bits of the 2 bytes are used for the address. ACALL is same as
LCALL it only saves ROM space.
Example:

Common questions

Powered by AI

The stack in the 8051 microcontroller holds the return address whenever a subroutine call is made (via LCALL or ACALL), allowing the processor to resume execution after the subroutine finishes . Careful manipulation of the stack is essential because any mismatch between the number of PUSH and POP operations in a subroutine can disrupt the program execution, leading to an incorrect return address being retrieved and thus logical errors or crashes . Hence, ensuring that the stack maintains integrity through matched operations is crucial for program stability and correct execution flow.

The CJNE instruction combines comparison and conditional branching in a single step, comparing two operands and jumping to a specified address if they are not equal . This instruction efficiently controls program flow by integrating comparison and branch, eliminating the need for separate comparison and branch steps. It is particularly useful in loops and conditional executions where decisions are made based on dynamic data, as it simplifies the control code and improves execution speed by reducing instruction count and execution cycles . The carry flag is the only one affected, allowing flexible decision-making strategies.

The AND (ANL), OR (ORL), and XOR (XRL) instructions perform distinct logical operations. The AND instruction performs a bitwise logical AND between the accumulator and another operand, making all destination bits zero unless both operands' bits are one . OR performs a logical OR, setting bits in the result where either operand has a bit set to one . XOR, on the other hand, sets each bit in the result to one if the corresponding bits in the operands differ . Each serves specific purposes in manipulating bit patterns, enabling manipulation like bit masking (AND), setting particular bits (OR), or toggling bits (XOR).

SJMP (short jump) and LJMP (long jump) are both unconditional jump instructions but differ in range and memory usage. SJMP is a 2-byte instruction that allows jumps within -128 to +127 bytes from the current program counter, economizing on program memory space . LJMP is a 3-byte instruction that can cover the entire 64K byte memory range, making it suitable for long distance. However, it uses more memory space per jump instruction . The choice between SJMP and LJMP affects memory management efficiency; SJMP saves ROM space, which is crucial when program memory is limited, while LJMP offers greater flexibility for larger programs within the 64K memory constraint.

The DA instruction is used to adjust the accumulator after a binary-coded decimal (BCD) addition to ensure the result remains in valid BCD format . This adjustment is necessary because binary additions might yield results that are not valid BCD digits, requiring correction by adding 6 to the affected digit range when the lower nibble exceeds 9 or a carry is generated from adding two BCD numbers. Thus, it is particularly important in applications involving decimal arithmetic where BCD format consistency is required, such as calculating sums in financial applications .

Immediate addressing mode refers to the situation where the operand is a part of the instruction itself and follows the opcode directly, denoted by a '#' symbol (e.g., MOV A, #5H). In contrast, direct addressing mode accesses data stored at a specific memory location in RAM, and the instruction contains the address of the memory location (e.g., MOV A, 30H). Immediate addressing is quicker since the operand is immediately available, while direct addressing requires access to specific memory addresses.

The carry (CY) flag is used to indicate an overflow in arithmetic operations, such as when there is a carry out of the most significant bit during addition, typically showing that the addition result exceeds the bit capacity of the register . The auxiliary carry (AC) flag is set if there is a carry from the lower nibble (D3 to D4), useful for operations like BCD addition where correcting the result is necessary (e.g., using the DA instruction). Both flags are crucial in ensuring correct arithmetic operations, such as distinguishing between positive and negative results in the SUBB instruction and adjusting BCD sums.

Indexed addressing mode significantly enhances access efficiency for look-up tables stored in ROM by allowing dynamic data retrieval using a combination of a base register (DPTR) and an accumulator . The instruction 'MOVC A, @A+DPTR' employs the base address from DPTR adding the offset from A, enabling the program to access successive table entries efficiently. This avoids recalculating physical addresses for each access, reducing code complexity and execution time, especially beneficial in applications with extensive data manipulations like tables for conversion or function estimation .

Memory-to-memory arithmetic operations are not allowed in the 8051 due to its architecture, which emphasizes efficiency and simplicity by limiting operations to involve at least one operand in a register . This design choice reduces the complexity and number of cycles required for instruction execution, as accessing two memory locations concurrently would be resource-intensive and slow. Consequently, operands must be loaded into registers before arithmetic can be performed, which can influence programming strategies, requiring more register management but ultimately streamlining the instruction execution flows .

Register indirect addressing mode allows dynamic access to data by using registers like R0 and R1 to store the address of data in memory, which provides flexibility in manipulating data addresses during program execution (e.g., MOV A, @R0). This contrasts with direct addressing mode, where the address is static and typed directly into the instruction, limiting flexibility in accessing data as memory changes over time . The '@' symbol in register indirect mode enables the dynamic reference, making it suitable for operations like iterative processing of array elements.

You might also like