Addressing Modes
Addressing Modes
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
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.
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.
Subtraction:
For subtraction only one instruction is used. To subtract without carry we have to make
CY = 0.
Example:
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:
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:
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).
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.:
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.
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.
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.
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).
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.
Example: