You are on page 1of 17

8051 Assembly Language

Addressing modes
And
Instructions set

Addressing Modes
The various ways of accessing data are called
addressing modes
The data could be in register, or in memory or be
provided as an immediate value

Immediate
Register
Direct
Indirect

Addressing Modes
So 8051 has lot of instructions for
movement of data
They are
MOV destination, source
PUSH source or POP destination
XCH destination, source

Immediate addressing mode


The data is part of the instruction
The mnemonic for immediate data is the
pound sign ( # )
Mnemonic

operation

MOV Rr,#n
copy 8 bit data to register Rr
Mov A,#n
copy 8 bit data to accumulator
Mov DPTR,#nn copy 16 bit data to DPTR

Immediate Addressing Mode


MOV
MOV
MOV
MOV
MOV

A,#65H
A,#A
R6,#65H
DPTR,#2343H
P1,#65H

Register addressing mode


Certain register names may be used as
part of the opcode mnemonic as sources
or destinations of data.
Register to register moves are using the
register addressing mode occur between
registers A and R0 to R7

Register Addressing Mode


MOV
ADD
MOV

Rn, A
A, Rn
DPL, R6

MOV
MOV

DPTR, A
Rm, Rn

;n=0,..,7

Direct Addressing Mode


The address of the data location is
specified in the instruction
All 128 bytes of internal RAM and SFRs
may be addressed directly using their
single byte address

Direct Addressing Mode


Although the entire of 128 bytes of RAM can be accessed using direct
addressing mode, it is most often used to access RAM loc. 30 7FH.
MOV
MOV
MOV
MOV

R0, 40H
56H, A
A, 4
6, 2

; MOV A, R4
; copy R2 to R6
; MOV R6,R2 is invalid !

SFR register and their address


MOV
MOV
MOV

0E0H, #66H
0F0H, R2
80H,A

; MOV A,#66H
; MOV B, R2
; MOV P1,A

Indirect Addressing Mode


The indirect addressing mode uses a
register to hold the actual address that will
finally be used in the data move
The register itself is not the address, but
rather the number in the register
The mnemonic symbol for indirect
addressing is the at ( @ ) sign

Indirect Addressing Mode

In this mode, register is used as a pointer to the data.

MOV

A,@Ri

MOV

@R1,B

; move content of RAM loc.Where address is held by Ri into A


( i=0 or 1 )

In other word, the content of register R0 or R1 is sources or target in MOV, ADD and SUBB
insructions.
Example:
Write a program to copy a block of 10 bytes from RAM location sterting at 37h to RAM
location starting at 59h.
Solution:
MOV R0,37h
MOV R1,59h
MOV R2,10
L1: MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R2,L1

; source pointer
; dest pointer
; counter

Indirect Addressing Mode

In this mode, register is used as a pointer to the data.

MOV
A,@Ri ; move content of RAM loc.Where address is held by Ri into A
( i=0 or 1 )
MOV
@R1,B
In other word, the content of register R0 or R1 is sources or target in MOV, ADD and
SUBB insructions.
Example:
Write a program to copy a block of 10 bytes from RAM location sterting at 37h to
RAM location starting at 59h.
Solution:
MOV R0,37h ; source pointer
MOV R1,59h ; dest pointer
MOV R2,10 ; counter
L1: MOV A,@R0
MOV @R1,A
INC R0
INC R1
DJNZ R2,L1

Stack in the 8051


The register used to access
the stack is called SP (stack
pointer) register.

7FH
Scratch pad RAM
30H

The stack pointer in the


8051 is only 8 bits wide,
which means that it can take
value 00 to FFH. When
8051 powered up, the SP
register contains value 07.

2FH
Bit-Addressable RAM
20H
1FH
18H
17H
10H
0FH
08H
07H
00H

Register Bank 3
Register Bank 2
Register Bank 1( Stack)
Register Bank 0

Example:
MOV
MOV
MOV
PUSH
PUSH
PUSH

R6,#25H
R1,#12H
R4,#0F3H
6
1
4

0BH

0BH

0BH

0BH

0AH

0AH

0AH

0AH

F3

09H

09H

09H

12

09H

12

08H

08H

08H

25

08H

25

Start SP=07H

25

SP=08H

SP=09H

SP=08H

On-Chip ROM Access


This mode is widely used in accessing data elements
of look-up table entries located in the program (code)
space ROM at the 8051
MOVC
A,@A+DPTR
A= content of address A +DPTR from ROM
Note:
Because the data elements are stored in the program
(code ) space ROM of the 8051, it uses the instruction
MOVC instead of MOV. The C means code.

Example:
Assuming that ROM space starting at 250h contains Hello., write a program to transfer the
bytes into RAM locations starting at 40h.
Solution:
ORG
0
MOV
DPTR,#MYDATA
MOV
R0,#40H
L1: CLR
A
MOVC
A,@A+DPTR
JZ
L2
MOV
@R0,A
INC
DPTR
INC
R0
SJMP
L1
L2: SJMP
L2
;------------------------------------ORG
250H
MYDATA:DB
Hello,0
END
Notice the NULL character ,0, as end of string and how we use the JZ instruction to
detect that.

Example:
Write a program to get the x value from P1 and send x 2 to P2, continuously .
Solution:
ORG
0
MOV DPTR, #TAB1
MOV A,#0FFH
MOV P1,A
L01:
MOV A,P1
MOVC A,@A+DPTR
MOV
P2,A
SJMP
L01
;---------------------------------------------------ORG
300H
TAB1: DB
0,1,4,9,16,25,36,49,64,81
END

You might also like