8051 Assembly Language
8051 Assembly Language
Language
Overview
Addressing modes
Data transfer instructions
Data processing (arithmetic and logic)
Program flow instructions
Addressing Modes
Immediate Mode specify data by its
value
mov a, #0
;put 0 in the accumulator
a = 00000000
mov a, #0x11
; put 11hex in the accumulator
a = 00010001
mov a, #11
; put 11 decimal in accumulator
a = 00001011
mov a, #77h
; put 77 hex in accumulator
a = 01110111
Mov P1,#55h
; put 55 hex in port 1
P1= 01010101
Addressing Modes
Register Addressing
either source or destination is one of R0R7
Ex: mov R0, a
mov a, R0
source & destination operands must match
same size
EX: mov a, dptr
dptr is a 16-bit register and A is 8-bit register
MOV R1,R2 (is invalid)
Addressing Modes
Direct Mode specify data by its 8-bit address
mov a, 0x70
or
Mov a,70h
Addressing Modes
Direct Mode specify data by its 8-bit
address
Ram
Ex:
Mov a,4;
mov R2,#5
or
mov a,2
Mov a,R4;
mov b,2
mov 2,7
(copying data from R2 to R7)
Since mov R2,R7 is invalid
Addressing Modes
Register Indirect the address of the source or
destination is specified in registers.
Use only registers R0 or R1 for 8-bit address:
R2 R7 cant use for storing the address
mov
mov
mov
;
psw,#0
; use register bank 0
r0,#3C
@r0,#3
; memory at 3C gets #3
M[3C] 3
Addressing Modes
Ex:
mov R0,50
mov R0,#50h
Here 50 is address location in
Here 50 is data is
loaded in R0
RAM
mov a,@R0
mov a,R0
Copy the content of data where content of R0 will
be copied in
Address is loaded in R0
Accumulator Register
a=[50]
a=50h
Addressing Modes
Indexed Mode source or destination
address is the sum of the base address and
the accumulator.
Accessing data from ROM(internal/external)
Base address can be DPTR or PC
mov dptr, #4000h
mov a, #5
movc
a, @a + dptr ;a M[4005]
Stacks
push
pop
stack pointer
stack
PUSH Y
PUSH X
INCREMENTING
BEFORE PUSHing
SP
POP Y
POP X
INTERNAL RAM
SP
SP1
SPDECEREMENT
2
AFTER POPing
7F
78
2E
2D
2C
Bit addressing:
mov C, 1Ah
or
mov C, 23h.2
2B
2A
29
28
27
26
25
24
23
1
A
22
10
21
20
0F
07
08
06
05
04
03
02
01
00
Address
SFRs
Pink are
implemented in
enhanced
C8051F020
Register
0xF8
SPI0CN
0xF0
0xE8
ADC0CN
0xE0
ACC
0xD8
PCA0CN
0xD0
PSW
0xC8
T2CON
0xC0
SMB0CN
0xB8
IP
0xB0
P3
0xA8
IE
0xA0
P2
0x98
SCON
0x90
P1
0x88
TCON
0x80
P0
dest source
6 basic types:
MOV a, byte
MOV Rn, byte
MOV direct, byte
MOV @Rn, byte
MOV DPTR, data16
Exchange instructions
XCH a, byte ;exchange accumulator and
;byte
XCHD a, byte ;exchange low nibbles of
;accumulator and byte
Arithmetic Instructions
Add
Mnemonic
Description
Subtract
ADD A, byte
ADDC A, byte
Increment
SUBB A, byte
INC A
increment A
Decremen
t
INC byte
INC DPTR
DEC A
decrement accumulator
Multiply
DEC byte
decrement byte
MUL AB
Divide
DIV AB
DA A
Decimal
Logic Instructions
Bitwise logic operations (AND, OR, XOR,
NOT)
Clear
Rotate
Swap
Logic instructions do NOT affect the flags in
PSW
Bitwise Logic
ANL AND
ORL OR
XRL eXclusive OR
CPL Complement
Examples:
00001111
ANL 10101100
00001100
ORL
00001111
10101100
10101111
XRL
00001111
10101100
10100011
CPL
10101100
01010011
A
byte
Ri
@Ri
(direct mode)
(register mode)
(register indirect mode)
Rotate
Rotate instructions operate only on a
rl a
mov a, #0xF0
rl a
; a 11110000
; a 11100001
mov a, #0A9h
add a, #14h
rrc a
; a A9
; a BD (10111101), C0
; a 01011110, C1
Swap
swap a
mov a, #72h
swap a
; a 27h
ANL C, /bit
ORL C, /bit
Rotate and
Multiplication/Division
Note that a shift left is the same as
multiplying by 2, shift right is divide by
2
mov
clr
rlc
rlc
rrc
a, #3 ; A 00000011 (3)
C ; C 0
a ; A 00000110 (6)
a ; A 00001100 (12)
a ; A 00000110 (6)
Unconditional Jumps
SJMP <rel addr>
Long jump
Absolute jump to
anywhere within 2K block of program memory
JMP @A + DPTR
Infinite Loops
Conditional Jumps
These instructions cause a jump to occur
only if a condition is true. Otherwise,
program execution continues with the
next instruction.
loop: mov a, P1
jz loop ; if a=0, goto loop,
;else goto next
;instruction
mov b, a
Conditional jumps
Mnemonic
JZ <rel addr>
JNZ <rel addr>
JC <rel addr>
JNC <rel addr>
JB <bit>, <rel addr>
JNB <bit>,<rel addr>
JBC <bir>, <rel addr>
CJNE A, direct, <rel
addr>
Description
Jump if a = 0
Jump if a != 0
Jump if C = 1
Jump if C != 1
Jump if bit = 1
Jump if bit != 1
Jump if bit =1, clear
bit
Compare
A
and
memory, jump if not
equal
condition
false
goto label
else
goto next
instruction
if a = 0 is true
send a 0 to LED
else
send a 1 to LED
true
label
jz led_off
setb C
mov P1.6, C
sjmp skipover
led_off: clr C
mov P1.6, C
skipover: mov A, P0
Description
Compare data in Rn
and memory, jump if
not equal
Iterative Loops
For A = 0 to 4 do
{}
For A = 4 to 0 do
{}
clr a
loop: ...
inc a
cjne a, #4, loop
mov R0, #4
loop: ...
...
djnz R0, loop
; stack PC
; PC address 11
; stack PC
; PC address 16
Return
Return is also similar to a jump, but
Return instruction pops PC from stack to
get address to jump to
ret
; PC stack
Subroutines
call to the subroutine
Main:
...
acall sublabel
...
...
sublabel:...
the subroutine
...
ret
Why Subroutines?
Subroutines allow us to have "structured"
assembly language programs.
This is useful for breaking a large design
into manageable parts.
It saves code space when subroutines
can be called many times in the same
program.