0% found this document useful (0 votes)
6 views

Module-3 - Addressing Mode and Instruction Sets

The document discusses different addressing modes of the 8051 microcontroller including immediate, register, direct, register indirect, and indexed addressing modes. It provides examples of instructions using each addressing mode and explains how they work. The document also summarizes the instruction set of the 8051 including arithmetic, logical, data transfer, branching/looping, and bit manipulation instructions. Examples are given to demonstrate different instructions like addition, jumps, calls, and decimal adjustment.

Uploaded by

atharv atre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Module-3 - Addressing Mode and Instruction Sets

The document discusses different addressing modes of the 8051 microcontroller including immediate, register, direct, register indirect, and indexed addressing modes. It provides examples of instructions using each addressing mode and explains how they work. The document also summarizes the instruction set of the 8051 including arithmetic, logical, data transfer, branching/looping, and bit manipulation instructions. Examples are given to demonstrate different instructions like addition, jumps, calls, and decimal adjustment.

Uploaded by

atharv atre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Addressing Modes

8051 Addressing Modes


1. Immediate Addressing mode

2. Register Addressing mode

3. Direct Addressing mode

4. Register indirect Addressing mode

5. Indexed Addressing mode


8051 Addressing Modes
Immediate Addressing mode
• The operand comes immediately after the op-code.
• The immediate data must be preceded by ‘#’ sign.
• This addressing mode can be used to load information into any of the register, including the
DPTR.
Example :-

MOV A, #25H // load 25H in to A

MOV R4, #62 // load the decimal value 62 into R4.

MOV DPTR, #4532H // DPTR=4532H.


8051 Addressing Modes
Register addressing mode
• Register addressing mode involves the use of registers to hold the data to be
manipulated.
• Permits access to eight registers(R0-R7) of register bank
Example :-
• MOV A,R0 // copy the contents of R0 in to A.
• MOV R2,A // copy the contents of A in to R2.
• ADD A,R5 // add the content of R5 to content of A.
8051 Addressing Modes
Direct addressing mode
• In direct addressing mode, the data is in a RAM memory location whose
address is known, and this address (8-bit) is given as a part of the instruction.
• Only internal data RAM and SFR’S can be directly addressed.
• Location 00H to 7FH to address internal RAM and location 80H to FFH to
address SFR
• Example :-
• MOV R0, 40H // save content of RAM location 40h into R0.
• MOV 56H, A // save content of A in RAM location 56H.
• MOV 0E0H, #55H // load A with 55 (SFR)
8051 Addressing Modes
Register indirect addressing mode
• In the register indirect addressing mode, a register is used as a pointer to the
data.
• If the data is inside the CPU, only register R0 and R1 are used for this purpose.
• In other words, R2-R7 cannot be used to hold the address of an operand located
in RAM when using this addressing mode.
• When R0 and R1 are used as pointers , that is, when they hold the address of
RAM locations , they must be preceded by the “@” sign.
Example :-
• MOV A, @R0 // move contents of RAM location whose address is held by R0 into A.
• MOV @R1, B // move contents of B RAM location whose address is held by R1
8051 Addressing Modes
Indexed addressing mode
• Only programmed memory is accessed .
• Either DPTR or PC may act as base register
• Register A acts as Index register
• Summation of both base and index register determines the operand
address.
• The content of A are added to the 16-bit register DPTR to form the 16-bit
address of the needed data.
• As the data elements are stored in the program space ROM of the 8051,it
uses the instruction MOVC instead of MOV.
• Example: MOVC A, @A+DPTR
8051 Instruction Set
8051 has simple instruction set in different groups. There are:
• Arithmetic instructions
• Logical instructions
• Data transfer instructions
• Branching and looping instructions
• Bit control instructions
8051 Instruction Set
Arithmetic instructions
• These instructions are used to perform various mathematical operations like addition,
subtraction, multiplication, and division etc.
• ADD A, R1 // Add the content of register1 to Accumulator
• ADDC A,#2 // Add 2 to accumulator with carry
• SUBB A,R2 // Subtract content of register2 from Accumulator
• MUL AB // multiply A with B (lower in A, Upper in B)
• DIV AB // division A by B (quotient in A, remainder in B)
• INC A // Increment accumulator
• DEC A // Decrement accumulator
8051 Instruction Set
Logical instructions
• The logical instructions are the instructions which are used for performing some
operations like AND, OR, NOT, X-OR and etc., on the operands.
• ANL A, Rn // AND register to accumulator
• ORL A, Rn // OR register to accumulator
• XRL A, Rn // Exclusive OR Reg to Acc
• CLR A // Clear Accumulator
• CPL A // Complement Accumulator
• RR A //Rotate right A
• RL A // Rotate left A
• SWAP A // swaps lower nibble with upper nibble
• XCHG //exchange 2 memory location
• XCHD ///exchange digit
8051 Instruction Set
Data Transfer Instructions

• These instruction are used to transfer the data from source operand to
destination operand. All the store, move, load, exchange input and output
instructions belong to this to this group.
• MOV A, Rn // Move Reg to Acc
• MOVX A,@DPTR // Move external RAM to Accumulator
• PUSH direct // PUSH direct byte on to stack ( Ex. - PUSH 0E0H/05)
• POP direct // POP direct byte from stack ( Ex. - POP 0F0H/06)
8051 Instruction Set
Branch and Looping Instructions
• These instructions are used for both branching as well as looping.
• These instructions include conditional & unconditional jump or loop instructions.
 Conditional Jump Instructions
• JC // Jump if carry equal to one
• JNC // Jump if carry equal to zero
• JB // Jump if bit equal to one
• JNB // Jump if bit equal to zero
• JBC // Jump if bit equal to one and clear bit
• JZ // Jump if A=Zero
• JNZ // Jump if A not equal to zero
• DJNZ // Decrement and Jump if not equal to zero.
8051 Instruction Set
Unconditional Jump Instructions
• SJMP // Short jump { SJMP (address)}
• LJMP // Long jump { LJMP (address 16)}
• AJMP // absolute jump to memory location {AJMP (address 11)}
• ajmp 2000h or ajmp next
• JMP @A+DPTR

• Example: start : mov C, p3.7 (infinite loop)


mov p1.6, C
sjmp start
8051 Instruction Set
• CALL // call a subroutine

• LCALL // long call - allows to jump to a subroutine anywhere in the 64K code
space.

• ACALL //absolute call - allows to jump to a subroutine within the same 2K page
• JMP //jump

• AJMP //absolute jump like acall

• LJMP // long jump like lcall


8051 Instruction Set
Example: Getting the Average of a Set of Numbers
MOV R0, #30H ; initialize the subroutine by putting the start address into R0
MOV R1, #05H ; and by putting the size of the set into R1
CALL average
average:
MOV A, 30H
ADD A, 31H
ADD A, 32H
ADD A, 33H
ADD A, 34H
MOV B, #05H
DIV AB
MOV 20H, A
MOV 21H, B
RET
8051 Instruction Set
Bit control instructions
• SETB bit // set the bit

• CLR bit // clear the bit

• CPL bit //complement the bit

• Examples:
• ANL C, bit (and carry with bit)
• CLR C
• SETB C
• SETB bit (SETB PSW.3)
Add values at location 78h, 79h and store at 7Ah (z= x+y)
x equ 78h
Y equ 79h
Z equ 7Ah

org 00h
ljmp main

org 0100h
main: mov a, x
add a, y
mov z, a
end
16 –bit addition using LJMP
x equ 78h
Y equ 7Ah
Z equ 7Ch

org 00h
ljmp main

org 0100h
main: mov a, x
add a, y
mov z, a
mov a, x+1
addc a, y+1
mov z+1, a
end
Increment of 16 –bit word/number
• Assume 16-bit number in r3:r2

org 0000h
mov a, r2
add a, #01
mov r2, a
mov a, r3
addc a, # 0
mov r3, a
end
Decimal adjust for BCD addition
• DA a; decimal adjust a

• Used for BCD addition . Add 6 to either higher or lower nibble after an
addition to create a valid BCD number.

• Example:
Mov a, #23h
mov b, #29h
add a, b // a= 23h+29h = 4ch (wanted 52)
DA a // a= a+ 6 = 52
To add 3 to accumulator 4 times
• DJNZ Rx, add // repeat the label add till Rx=0
• CJNE

• Example
Mov a , #0 // clear accumulator
Mov r2, #04 //load counter r2=4
Again:
add a, #03h //add 03 to accumulator
DJNZ r2, Again //repeat until r2=0
mov r5, a // save in R5

You might also like