8051 chap3 instruction ver 02
TRANSCRIPT
-
7/31/2019 8051 Chap3 Instruction Ver 02
1/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 1
Chapter 3
Instruction Set
How to write a program
The 8051 Microcontroller
L Ch Thng 1
L Ch Thng
Ho Chi Minh City University of Technology
Instruction Set
255 instructions
1-byte instructions: 139
2-byte instructions: 92
3-byte instructions: 24
Instruction set summary (pdf)
Full instruction set (pdf)
L Ch Thng 2
-
7/31/2019 8051 Chap3 Instruction Ver 02
2/65
-
7/31/2019 8051 Chap3 Instruction Ver 02
3/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 3
Internal RAM
128 locations from address 00H to 7FH (256 locations for 8052)
The content of one location is 8 bit.
Register banks: address 00H to 1FH
Bit Addressable RAM: address 20H to 2FH
General purpose RAM: address from 30H to 7FH
L Ch Thng 5
Bit-
addressable
RAM(20H-2FH)
Register
banks
(00H-1FH)
General
purpose
RAM
(30H-7FH)Special
function
registers
(SFRs)(80H-FFH)
L Ch Thng 6
-
7/31/2019 8051 Chap3 Instruction Ver 02
4/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 4
Address from 30H to 7FH
The content of one location is 8 bit.
Ex: MOV A,30H
This instruction moves (reads) the content of location 30H
(NOT data 30H) to register A.
Ex:MOV 31H,R4
This instruction moves (writes) the content of register R4
to location 31H.
LCh Thng
General Purpose RAM
A 30H
31H R4
7
Bit-
addressable
RAM(20H-2FH)
Register
banks
(00H-1FH)
General
purpose
RAM
(30H-7FH)Special
function
registers
(SFRs)(80H-FFH)
L Ch Thng 8
-
7/31/2019 8051 Chap3 Instruction Ver 02
5/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 5
Address from 20H to 2FH
The content of one location is 8 bit.
Can read/write a byte or a bit
Ex: MOV 20H, A ; writes the content of register A to location 20H.
Ex: SETB 20H.0
or SETB 00H
Ex: MOV C,31H
or MOV C, 26H.1
LCh Thng
Bit Addressable RAM
20H A
20H 1
Not affected
26HCY
9
Bit-
addressable
RAM(20H-2FH)
Register
banks
(00H-1FH)
General
purpose
RAM
(30H-7FH)Special
function
registers
(SFRs)(80H-FFH)
L Ch Thng 10
-
7/31/2019 8051 Chap3 Instruction Ver 02
6/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 6
4 banks: Bank 0 (default), Bank 1, Bank 2, and Bank 3
Change register bank by selecting bits RS1 and RS0 (in register PSW)
One bank includes 8 registers: R0 through R7
o R0 of Bank 0 is location 00H
o
o R7 of Bank 0 is location 07H
o R0 of Bank 1 is location 08H
o
o R7 of Bank 1 is location 0FH
oR0 of Bank 2 is location 10H
o
o R7 of Bank 2 is location 17H
o R0 of Bank 3 is location 18H
o
o R7 of Bank 3 is location 1FH L Ch Thng
Register Banks
11
Ex: use default bank (Bank 0)
MOV A, R5 = MOV A, 05H
MOV R0, A = MOV 00H, A
L Ch Thng
Register Banks
R5 05H
Read the contents of location 05H
into the accumulator
Read the contents of register R5
into the accumulator
Read the contents of register A
into register R0
Read the contents of register A
into location 00H
12
-
7/31/2019 8051 Chap3 Instruction Ver 02
7/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 7
L Ch Thng
Register PSW (Program Status Word)
13
The active bank is selected by bit RS1 (PSW.4) & RS0 (PSW.3)
Ex: MOV PSW, #00010000B ; select bank 2
or
SETB RS1
CLR RS0
L Ch Thng
Selecting Register Bank
RS1 RS0 Bank
0 0 0
0 1 1
1 0 2
1 1 3
14
-
7/31/2019 8051 Chap3 Instruction Ver 02
8/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 8
Bit-
addressable
RAM
(20H-2FH)
Register
banks
(00H-1FH)
General
purpose
RAM
(30H-7FH)Special
function
registers
(SFRs)
(80H-FFH)
L Ch Thng 15
L Ch Thng
Special Function Registers (SFRs)
SFRs include register A, B, PSW, P0, P1, P2, P3, DPTR,TMOD, SCON
All SFRs are accessible by name and direct address.
Both of them must be coded as direct addressEx: MOV P1, A ; Accumulator Port 1
MOV 90H, A ; same
Same opcode: F5 90Ex: MOV R1, ACC
MOV R1, 0E0H
Same opcode: A9 E0Ex: MOV R1, ACC
MOV R1, A
Same function BUT different opcode (A9 E0 vs. F9)
16
-
7/31/2019 8051 Chap3 Instruction Ver 02
9/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 9
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Addressing Modes
L Ch Thng 17
L Ch Thng
Coding format- Addressing modes
18
-
7/31/2019 8051 Chap3 Instruction Ver 02
10/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 10
- Source operand is constant- Use # sign
Ex 3.1:
MOV A,#12
MOV A,#0C4H
MOV R1,#1110B
ADD A,#11110001B
MOV DPTR,#2000H
Immediate Addressing
L Ch Thng
A 0 0 0 0 1 1 0 0
A 1 1 0 0 0 1 0 0
R1 0 0 0 0 1 1 1 0
A 1 0 1 1 0 1 0 1CY 1
DPH 0 0 1 0 0 0 0 0 DPL 0 0 0 0 0 0 0 0
19
Ex: MOV R1,#36H
MOV A,R1
MOV R7,#0FH
ANL A,R7
INC A
DEC A
MOV DPTR,#2000
INC DPTR
L Ch Thng
Register Addressing
R1 0 0 1 1 0 1 1 0
A 0 0 1 1 0 1 1 0
R7 0 0 0 0 1 1 1 1
A 0 0 1 1 0 0 0 0
A 0 0 1 1 0 0 0 1
A 0 0 1 1 0 0 0 0
DPH 0 0 1 0 0 0 0 0 DPL 0 0 0 0 0 0 0 1
20
-
7/31/2019 8051 Chap3 Instruction Ver 02
11/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 11
Direct Addressing
L Ch Thng
Access internal RAM and SFRs
MOV A,70H ; copy contents of RAM at 70H to A
MOV R0,40H ; copy contents of RAM at 40H to A
MOV 56H,A ; put contents of A at 56H
MOV 0D0H,A ; put contents of A into PSW
MOV PSW,A ; same
21
Direct Addressing vs. Immediate Addressing
L Ch Thng
MOV A,30H MOV A,#30H
Direct addressing Immediate addressing
A 30H A 0 0 1 1 0 0 0 0
22
-
7/31/2019 8051 Chap3 Instruction Ver 02
12/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 12
Play with R0-R7 (bank 0) by Direct Addressing
L Ch Thng
MOV A,4 MOV A,R4
MOV A,7 MOV A,R7
MOV 7,6 MOV R7,R6
MOV R2,#05 ; Put 5 in R2
MOV R2,5 ; Put content of RAM at address 5 in R2
R2 05H R2 0 0 0 0 0 1 0 1
23
MOV R2,5 MOV R2,#5
The address of the source or destination is specified in registers.
Use registers R0 or R1 for 8-bit address (internal or external
RAM)
Use @ sign to access the content of the memory location: @R0,@R1
Ex: MOV R0,#30H ;R0 30H
MOV A,@R0 ;A(R0): read content ofinternal RAM at; address specified by R0 to A
Indirect Addressing
L Ch Thng
A
7FH
30H
00H
R0 0 0 1 1 0 0 0 0Internal RAM
24
-
7/31/2019 8051 Chap3 Instruction Ver 02
13/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 13
Ex: MOV R0,#30H ;R0 30H
MOVX A,@R0 ;A(R0): read content ofeXternal RAM at; address specified by R0 to A
Indirect Addressing
L Ch Thng
A 30H
00H
External RAM
R0 0 0 1 1 0 0 0 0
25
Uses DPTR register for 16-bit addresses (external memory)
Use @ sign to access the content of the memory location:@DPTR
Ex: MOV DPTR,#4000H
MOVX A,@DPTR ; read content of external RAM at
; address 4000H to A
Indirect Addressing
LCh Thng
A 4000H
00H
External RAMDPTR 40H 00H
26
-
7/31/2019 8051 Chap3 Instruction Ver 02
14/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 14
Ex: MOV DPTR,#31FFH
MOVX @DPTR,A ; write content of A to external RAM
; address 31FFH
Indirect Addressing
A 31FFH
00H
External RAM
DPTR 31H FFH
L Ch Thng 27
Register Addressing vs. Indirect Addressing
L Ch Thng
MOV A,R1 MOV A,@R1
Register addressing
Indirect addressing
A R1
28
A
7FH
00H
R1Internal RAM
-
7/31/2019 8051 Chap3 Instruction Ver 02
15/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 15
Ex: Implementation of array
MOV R0, #60H
LOOP: MOV @R0, #0
INC R0
CJNE R0, #80H, LOOP
Example of Indirect Addressing
L Ch Thng 29
R0 60H
(R0) 0
R0 R0+1
R0= 80h?N
Y
Used in SJMP instruction
A relative address (or offset) is an 8-bit signed value.
It is added to PC to form a new value for PC.
Range: -128 ~ +127
Usually used with label
Ex: SJMP LABEL1 is in memoryat locations 0100H and 0101H
PC = 0102H
If LABEL1 is the label representingan instruction at location 0107H
Relative offset is0107H 0102H = 5
Opcode of SJMP LABEL1 is 80 05
Relative Addressing
L Ch Thng 30
-
7/31/2019 8051 Chap3 Instruction Ver 02
16/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 16
Ex: SJMP LABEL2 is in memoryat locations 2040H and 2041H
PC = 2042H
If LABEL2 is the label representingan instruction at location 2038H
Relative offset is2038H 2042H = -10 = F6H
Opcode of SJMP LABEL1 is 80 F6
Relative Addressing
L Ch Thng 31
Ex: ORG 0000H
AGAIN: SETB P1.0
NOP
NOP
CLR P1.0
NOP
SJMP AGAIN
END
a. Find the opcode of SJMP AGAIN instruction?
b. Find the duration of P1.0=1? P1.0=0?
c. What is the result of this program?
Your Turn!
L Ch Thng 32
-
7/31/2019 8051 Chap3 Instruction Ver 02
17/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 17
Used in AJMP, ACALL instruction
Usually used with label
Absolute Addressing
Memory map showing 2K pages
L Ch Thng 33
Within any 2K page, the upper five addressbits are the same for the source anddestination addresses.The lower 11 bits of the destination aresupplied in the instruction
Used in LCALL and LJMP instruction
Use full 16-bit address
Usually used with label
Ex: LCALL SUBPROGRAM1
LJMP LABEL2
Long Addressing
L Ch Thng 34
-
7/31/2019 8051 Chap3 Instruction Ver 02
18/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 18
Base address (PC or DPTR) + Offset (A) Effective Address
Used with JMP or MOVC
Ex: MOVC A, @A+DPTR
MOVC A, @A+PC
JMP @A+DPTR
Indexed Addressing
L Ch Thng 35
Ex: An even number from 0 to 6 is in the Accumulator. The following
sequence of instructions branches to one of four AJMP
instructions in a jump table starting at JMP_TBL.
MOV DPTR, # JMP_TBL
JMP @A + DPTR
JMP_TBL: AJMP LABEL0
AJMP LABEL1
AJMP LABEL2
AJMP LABEL3
If the Accumulator equals 04H when starting this sequence,execution jumps to label LABEL2. Because AJMP is a 2-byte
instruction, the jump instructions start at every other address.
Indexed Addressing
L Ch Thng 36
-
7/31/2019 8051 Chap3 Instruction Ver 02
19/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 19
Instruction Types Data transfer
Arithmetic
Logical
Boolean variable
Program branching
L Ch Thng 37
8051 Instruction Set SummaryLegend
L Ch Thng 38
-
7/31/2019 8051 Chap3 Instruction Ver 02
20/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 20
8051 Instruction Set SummaryData Transfer
L Ch Thng 39
8051 Instruction Set SummaryData Transfer
L Ch Thng 40
-
7/31/2019 8051 Chap3 Instruction Ver 02
21/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 21
Data TransferMOV dest, source ; dest source
MOV A,#72H ;A=72HMOV R4,#62H ;R4=62HMOV B,0F9H ;B=the content of address F9H of RAM
MOV DPTR,#7634HMOV DPL,#34HMOV DPH,#76H
MOV P1,A ;move (out) A to Port 1
Note 1:MOV A,#72H MOV A,72H
Note 2:MOV A,R3 MOV A,3 ;R3 of bank 0 is location 3
L Ch Thng 41
Data TransferEx: Write a program to move (write) 40H to location 30H in internal
RAM using 2 methods of addressing: direct addressing and indirect
addressing.
Method 1: Direct addressing
ORG 0000H
MOV 30H,#40H
ENDMethod 2: Indirect addressing
ORG 0000H
MOV R0,#30H
MOV @R0,#40H
END
L Ch Thng 42
-
7/31/2019 8051 Chap3 Instruction Ver 02
22/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 22
Creating a Loop (1)Ex: Write a program to write 40H to internal RAM from location
30H to location 36H.
ORG 0000H
MOV 30H,#40H
MOV 31H,#40H
MOV 32H,#40H
MOV 33H,#40H
MOV 34H,#40H
MOV 35H,#40H
MOV 36H,#40HEND
L Ch Thng 43
Ex: Write a program to write 40H to internal RAM
from location 30H to location 36H.
(source)
ORG 0000H
MOV R5,#7 ;Loop=7
MOV R1,#30H;Address=30H
Again: MOV @R1,#40HINC R1
DEC R5
CJNE R5,#0,Again
END
L Ch Thng 44
Addr 30H
(Addr) 40H
AddrAddr+1
Loop=0?N
Y
Loop 7
LoopLoop-1
Creating a Loop (2)
-
7/31/2019 8051 Chap3 Instruction Ver 02
23/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 23
Ex: Write a program to write 40H to internal RAM
from location 30H to location 36H.
(source)ORG 0000H
MOV R5,#7;Loop=7
MOV R1,#30H;Address=30H
Again: MOV @R1,#40H
INC R1
DJNZ R5,Again
END
L Ch Thng 45
Creating a Loop (3)
Addr 30H
(Addr) 40H
AddrAddr+1
Loop=0?N
Y
Loop 7
LoopLoop-1
Ex: Write a program to write 40H to internal RAM
from location 30H to location 36H.
(source)ORG 0000H
MOV R1,#30H;Address=30H
Again: MOV @R1,#40H
INC R1CJNE R1,#37H,Again
END
L Ch Thng 46
Creating a Loop (4)
Addr 30H
(Addr)
40H
AddrAddr+1
Addr=37H?N
Y
-
7/31/2019 8051 Chap3 Instruction Ver 02
24/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 24
Your Turn!
Write a program to read data from internal RAM from
location 20H to location 29H and output to Port 1
L Ch Thng 47
Your Turn!Write a program to read data from internal RAM from location 20H
to location 29H and output to Port 1
Method 1: using DJNZ
ORG 0000H
MOV R7,#10
MOV R0,#20H
Loop:MOV P1,@R0 ;Output to Port1INC R0
DJNZ R7,loop
END
L Ch Thng 48
-
7/31/2019 8051 Chap3 Instruction Ver 02
25/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 25
Your Turn!Ex: Write a program to read data from internal RAM from location
20H to location 29H and output to Port 1
Method 2: using CJNE
ORG 0000H
MOV R0,#20H
Loop:MOV P1,@R0 ;Output to Port1
INC R0
CJNE R0,#2AH,loop
END
L Ch Thng 49
Data Transfer More ExamplesEx: Write a program to clear location 31H in internal RAM using 2
methods of addressing: direct addressing and indirect addressing.
Hint: to clear means to reset the data to zero.
Method 1: Direct addressing
ORG 0000H
MOV 31H,#0
ENDMethod 2: Indirect addressing
ORG 0000H
MOV R0,#31H
MOV @R0,#0
END
L Ch Thng 50
-
7/31/2019 8051 Chap3 Instruction Ver 02
26/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 26
Ex: Write a program to move (write) the content of A to location
32H in internal RAM using 2 methods of addressing: direct
addressing and indirect addressing.
Method 1: Direct addressing
ORG 0000H
MOV 32H,A
END
Method 2: Indirect addressing
ORG 0000H
MOV R0,#32H
MOV @R0,AEND
L Ch Thng 51
Data Transfer More Examples
Ex: Write a program to move the content of location 33H in internal
RAM to register A using 2 methods of addressing: direct addressing
and indirect addressing.
Method 1: Direct addressing
ORG 0000H
MOV A,33H
ENDMethod 2: Indirect addressing
ORG 0000H
MOV R0,#33H
MOV A,@R0
END
L Ch Thng 52
Data Transfer More Examples
-
7/31/2019 8051 Chap3 Instruction Ver 02
27/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 27
Ex: Write a program to move the content of location 33H in internal
RAM to register A using 2 methods of addressing: direct addressing
and indirect addressing.
Method 1: Direct addressing
ORG 0000H
MOV A,33H
END
Method 2: Indirect addressing
ORG 0000H
MOV R0,#33H
MOV A,@R0END
L Ch Thng 53
Data Transfer More Examples
Ex: Write a program to move the content of location 34H in internal
RAM to location 35H in internal RAM using 2 methods of
addressing: direct addressing and indirect addressing.
Method 1: Direct addressing
ORG 0000H
MOV 35H,34H
ENDMethod 2: Indirect addressing
ORG 0000H
MOV R0,#34H
MOV A,@R0
INC R0
MOV @R0,A
END
L Ch Thng 54
Data Transfer More Examples
-
7/31/2019 8051 Chap3 Instruction Ver 02
28/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 28
Ex: Write a program to write 40H to location 0030H in external
RAM.
ORG 0000H
MOV A,#40H
MOV DPTR,#0030H
MOVX @DPTR,A
END
L Ch Thng 55
Data Transfer More Examples
Ex: Write a program to clear location 0031H in externalRAM.
ORG 0000H
CLR A
MOV DPTR,#0031H
MOVX @DPTTR,A
END
L Ch Thng 56
Data Transfer More Examples
-
7/31/2019 8051 Chap3 Instruction Ver 02
29/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 29
Ex: Write a program to move the content of location 0032H in
externalRAM to register A.
ORG 0000H
MOV DPTR,#0032H
MOVX A,@DPTTR
END
L Ch Thng 57
Data Transfer More Examples
Ex: Write a program to move the content of register A to location
0033H in externalRAM.
ORG 0000H
MOV DPTR,#0033H
MOVX @DPTTR,A
END
L Ch Thng 58
Data Transfer More Examples
-
7/31/2019 8051 Chap3 Instruction Ver 02
30/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 30
Ex: Write a program to move the content of location 0034H in
externalRAM to location 0035H in externalRAM.
ORG 0000H
MOV DPTR,#0034H
MOVX A,@DPTTR
INC DPTR
MOVX @DPTR,A
END
L Ch Thng 59
Data Transfer More Examples
MOV A,#ENTRY_NUMBER
MOV DPTR,#TABLE
MOVC A,@A+DPTR
TABLE: DB data1, data2, data3,
L Ch Thng 60
Look-up Table
-
7/31/2019 8051 Chap3 Instruction Ver 02
31/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 31
Given a packed-BCD number in location 33H of internal RAM. Write
a program that calculate the square of high decade of this number,
and store the result in internal RAM at address 34H
ORG 0
MOV A,33H
SWAP A
ANL A,#0FH
MOV DPTR,#TABLE
MOVC A,@A+DPTR
TABLE:
DB 0,1,4,9,16,25,36,49,64,81END
L Ch Thng 61
Look-up Table
MOV A, #ENTRY_NUMBER
CALL LOOK_UP
LOOK_UP:
INC AMOVC A, @A+PC
RET
TABLE:
DB data1, data2, data3,
L Ch Thng 62
Look-up Table
-
7/31/2019 8051 Chap3 Instruction Ver 02
32/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 32
8051 Instruction Set SummaryArithmetic Operations
L Ch Thng 63
8051 Instruction Set SummaryArithmetic Operations
L Ch Thng 64
-
7/31/2019 8051 Chap3 Instruction Ver 02
33/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 33
8051 Instruction Set SummaryArithmetic Operations
L Ch Thng 65
8051 Instruction Set SummaryFlag Effect
L Ch Thng 66
-
7/31/2019 8051 Chap3 Instruction Ver 02
34/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 34
Arithmetic Operations (1)
ADD A,#34H ; Immediate addressing
ADD A,34H ;Direct addressing
ADD A,R1 ;Register addressing
ADD A,@R1 ;Indirect addressing
L Ch Thng 67
Arithmetic Operations (2)
MOV A,#23H ; A = 23H, CY = 0, P = 1, OV = 0
ADD A,#22 ; A = 39H, CY = 0, P = 0, OV = 0
ADD A,#11100111B ; A = 20H, CY = 1, P = 1, OV = 0
ADDC A,#09H ; A = 2AH, CY = 0, P = 1, OV = 0
SUBB A,#00100111B ; A = 03H, CY = 0, P = 0, OV = 0
SUBB A,#3 ; A = 00H, CY = 0, P = 0, OV = 0
SUBB A,#0F2H ; A = 0EH, CY = 1, P = 1, OV = 0
L Ch Thng 68
-
7/31/2019 8051 Chap3 Instruction Ver 02
35/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 35
Arithmetic Operations (3)
MOV A,#45 ; A = 2DH, CY = 1, P = 0, OV = 0
MOV B,#12H ; B = 12H
MUL AB ; A = 2AH, B = 03H
MOV B,#10 ; B = 10
DIV AB ; A = 04H, B = 02H
MOV A,#-100 ; A = 9CH, CY = 0, P = 0, OV = 0
ADD A,#-50 ; A = 6AH, CY = 1, P = 0, OV = 1
MOV A,#120 ; A = 78H, CY = 1, P = 0, OV = 1
ADD A,#30 ; A = 44H, CY = 0, P = 0, OV = 1
L Ch Thng 69
Your Turn!
Given 10 8-bit unsigned numbers in internal RAM at starting
address 30H. Write a program to calculate the sum of these
numbers and store the result in internal RAM at address 2FH.
Assume that the result is less than or equal 255.
L Ch Thng 70
-
7/31/2019 8051 Chap3 Instruction Ver 02
36/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 36
Your Turn!
Given 10 8-bit unsigned numbers in internal RAM at starting
address 30H. Write a program to calculate the sum of these
numbers and store the result in internal RAM at address 2FH.
Assume that the result is less than or equal 255.
ORG 0
MOV R0,#30H
CLR A
MOV R2,#10
loop: ADD A,@R0
INC R0
DJNZ R2,loopMOV 2FH,A
END
L Ch Thng 71
Rotate instructions operate only on A
RL A
MOV A,#0F0H ; A 11110000
RR A ; A 11100001
RR A
MOV A,#0F0H ; A 11110000
RR A ; A 01111000
Rotate
72L Ch Thng
-
7/31/2019 8051 Chap3 Instruction Ver 02
37/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 37
RRC A
MOV A, #0A9H ; A A9H
ADD A, #14H ; A BDH (10111101), C0
RRC A ; A 01011110, C1
RLC A
MOV A, #3CH ; A 3CH(00111100)
SETB C ; C 1
RLC A ; A 01111001, C1
C
C
Rotate through Carry
73L Ch Thng
Note that a shift left is the same asmultiplying by 2, shift right is divide by 2
MOV A, #3 ; A 00000011 (3)
CLR C ; C 0
RLC A ; A 00000110 (6)
RLC A ; A 00001100 (12)
RRC A ; A 00000110 (6)
Rotate and Multiplication/Division
74L Ch Thng
-
7/31/2019 8051 Chap3 Instruction Ver 02
38/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 38
SWAP A
MOV A, #72H ; A 72H
SWAP A ; A = 27H
Swap
75L Ch Thng
8051 Instruction Set SummaryLogic Operations
L Ch Thng 76
-
7/31/2019 8051 Chap3 Instruction Ver 02
39/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 39
Logic Operations (1)
L Ch Thng 77
MOV A,#46H
MOV R3,A
ANL A,#0FH
MOV R7,A
MOV A,R3
ANL A,#0F0H
SWAP A
MOV R6,A
R6 = .; R7 = .
Logic Operations (2)
L Ch Thng 78
MOV R2,#05H
MOV R3,#07H
MOV A,R2
SWAP A
ORL A,R3
A = .
-
7/31/2019 8051 Chap3 Instruction Ver 02
40/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 40
8051 Instruction Set SummaryBoolean Variable
L Ch Thng 79
Boolean Variable
L Ch Thng 80
ORG 0
LOOP: MOV C,P1.0
ANL C,P1.1
MOV P1.2,C
SJMP LOOP
END
-
7/31/2019 8051 Chap3 Instruction Ver 02
41/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 41
Your Turn!
L Ch Thng 81
Your Turn!
L Ch Thng 82
ORG 0
LOOP: MOV C,P1.4
CPL C
ANL C,P1.5
CPL C
ORL C,P1.6
MOV P1.7,C
SJMP LOOP
END
-
7/31/2019 8051 Chap3 Instruction Ver 02
42/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 42
8051 Instruction Set SummaryProgram Branching
L Ch Thng 83
8051 Instruction Set SummaryProgram Branching
L Ch Thng 84
-
7/31/2019 8051 Chap3 Instruction Ver 02
43/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 43
Program Branching
L Ch Thng 85
LJMP(long jump)LJMP is an unconditional jump. It is a 3-byte instruction. It allows a
jump to any memory location from 0000 to FFFFH.
AJMP(absolute jump)In this 2-byte instruction, It allows a jump to any memory locationwithin the 2k block of program memory.
SJMP(short jump)In this 2-byte instruction. The relative address range of 00-FFH isdivided into forward and backward jumps, that is , within -128 to
+127 bytes of memory relative to the address of the current PC.
SJMP : 8-bit offset
LJMP : 11-bit address (2KB segment)
AJMP : 16-bit
Unconditional
jumps
Conditional Jumps
L Ch Thng 86
JZ Jump if A=0
JNZ Jump if A 0
DJNZ Decrement and jump if 0
CJNE A,byte Jump if A byte
CJNE reg,#data Jump if byte #data
JC Jump if CY=1
JNC Jump if CY=0
JB Jump if bit=1
JNB Jump if bit=0
JBC Jump if bit=1 and clear bit
-
7/31/2019 8051 Chap3 Instruction Ver 02
44/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 44
CALL Instruction
L Ch Thng 87
LCALL(long call)
This 3-byte instruction can be used to call subroutines
located anywhere within the 64K byte address space of
the 8051.
ACALL (absolute call)
ACALL is 2-byte instruction. the target address of thesubroutine must be within 2K byte range.
Call is similar to a jump, but
Call pushes PC on stack before branching
ACALL ; stack PC
; PC address 11 bit
LCALL ; stack PC
; PC address 16 bit
Call and Return
88L Ch Thng
-
7/31/2019 8051 Chap3 Instruction Ver 02
45/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 45
Return is also similar to a jump, but
Return instruction pops PC from stack to getaddress to jump to
RET ; PC stack
Call and Return
89L Ch Thng
MAIN: ...
ACALL SUBLABEL
...
...
SUBLABEL: ...
...
RETthe subroutine
call to the subroutine
Subroutines
90L Ch Thng
-
7/31/2019 8051 Chap3 Instruction Ver 02
46/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 46
DJNZ a For loop
L Ch Thng 91
Use DJNZ to create a for loop control
Ex: a 10-time loop
MOV R7,#10
LOOP: (begin loop)
(end loop)
DJNZ R7,LOOP(continue)
DJNZ a For loop
L Ch Thng 92
Use DJNZ to create a 1000-time loop?
-
7/31/2019 8051 Chap3 Instruction Ver 02
47/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 47
DJNZ an example
L Ch Thng 93
Ex: Write a program to write 40H to internal
RAM from location 30H to location 36H.
(source)
ORG 0000H
MOV R5,#7;Loop=7
MOV R1,#30H;Address=30H
Again: MOV @R1,#40H
INC R1
DJNZ R5,Again
END
Addr 30H
(Addr) 40H
AddrAddr+1
Loop=0?N
Y
Loop 7
LoopLoop-1
Write a program toclear ACC, then add 3 to the
accumulator ten times.
Solution:
MOV A,#0
MOV R2,#10
AGAIN: ADD A,#03
DJNZ R2,AGAIN ;repeat until R2=0 (10 times)
MOV R5,A
L Ch Thng 94
DJNZ another example
-
7/31/2019 8051 Chap3 Instruction Ver 02
48/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 48
Write a program to copy a block of 10 bytes from RAMlocation starting at 37h to RAM location starting at 59h.
L Ch Thng 95
DJNZ Your Turn!
Write a program to copy a block of 10 bytes from RAMlocation starting at 37h to RAM location starting at 59h.
Solution:MOV R0,#37h ; source pointerMOV R1,#59h ; dest pointerMOV R2,#10 ; counter
L1: MOV A,@R0MOV @R1,AINC R0INC R1DJNZ R2,L1
L Ch Thng 96
DJNZ Your Turn!
-
7/31/2019 8051 Chap3 Instruction Ver 02
49/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 49
L Ch Thng 97
Blinky Program
P1.0 1
Delay
P1.0 0
Delay
schematic
ORG 0LOOP: SETB P1.0
ACALL DELAYCLR P1.0ACALL DELAYSJMP LOOP
DELAY: MOV R6,#200DL1: MOV R7,#250
DJNZ R7,$DJNZ R6,DL1RETEND
(source)
Waveform? Period? Frequency?
L Ch Thng 98
Blinky Program
-
7/31/2019 8051 Chap3 Instruction Ver 02
50/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 50
ORG 0LOOP: SETB P1.0
ACALL DELAYCLR P1.0ACALL DELAYSJMP LOOP
DELAY: MOV R6,#200DL1: MOV R7,#250 1MC x 1
DJNZ R7,$ 2 MC x 250DJNZ R6,DL1RET
END
L Ch Thng 99
Blinky Program
501 MC
ORG 0LOOP: SETB P1.0
ACALL DELAYCLR P1.0ACALL DELAYSJMP LOOP
DELAY: MOV R6,#200DL1: MOV R7,#250
DJNZ R7,$DJNZ R6,DL1 2 MC x 200RETEND
L Ch Thng 100
Blinky Program
501 MC x 200 100,601 MC
-
7/31/2019 8051 Chap3 Instruction Ver 02
51/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 51
ORG 0LOOP: SETB P1.0
ACALL DELAYCLR P1.0ACALL DELAYSJMP LOOP
DELAY: MOV R6,#200DL1: MOV R7,#250
DJNZ R7,$DJNZ R6,DL1RET 2 MC
END
L Ch Thng 101
Blinky Program
100,601 MC100,603 MC
ORG 0LOOP: SETB P1.0 1 MC
ACALL DELAY 100,063 + 2 MCCLR P1.0 1 MCACALL DELAY 100,063 + 2 MCSJMP LOOP 2 MC
DELAY: MOV R6,#200DL1: MOV R7,#250
DJNZ R7,$DJNZ R6,DL1RETEND
tH = 100,063 + 2 + 1 = 100,066 MCtL = 100,063 + 2 + 2 + 1 = 100,068 MC
L Ch Thng 102
Blinky Program
-
7/31/2019 8051 Chap3 Instruction Ver 02
52/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 52
If using 12 MHz crystal, 1 MC = 1 s
tH = 100,066 MC = 100,066 s
tL = 100,068 MC = 100,068 s
T = tH + tL = 200,134 s
f = 1/T = 4.99 Hz
L Ch Thng 103
Blinky Program
tH tL
T
ORG 0LOOP: SETB P1.0
ACALL DELAYCLR P1.0ACALL DELAYSJMP LOOP
DELAY: MOV R6,#200DL1: MOV R7,#250
DJNZ R7,$DJNZ R6,DL1RETEND
tH = tL tDELAY 200 x 250 x 2 MC = 100,000 MC = 100,000 s T 200,000 s f 5 Hz
L Ch Thng 104
Blinky Program - Estimating
-
7/31/2019 8051 Chap3 Instruction Ver 02
53/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 53
ORG 0LOOP: CPL P1.0
ACALL DELAYSJMP LOOP
DELAY: MOV R6,#200DL1: MOV R7,#250
DJNZ R7,$DJNZ R6,DL1RETEND
L Ch Thng 105
Blinky Program Alternative Method
P1.0 NOT (P1.0)
Delay
Write a program that creates a 10-KHz square wave at pin P1.3.
Assume that crystal is 24 MHz.
L Ch Thng 106
10-kHz square wave
-
7/31/2019 8051 Chap3 Instruction Ver 02
54/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 54
Write a program that creates a 10-KHz square wave at pin P1.3.
Assume that crystal is 24 MHz.
ORG 0000H
lap: CPL P1.3
ACALL delay50
SJMP lap
delay50:
MOV R4,#25
DJNZ R4,$
RET
END
L Ch Thng 107
10-kHz square wave
Write a program that creates a 10-KHz square wave with duty cycle
30% at pin P1.3. Assume that crystal is 24 MHz.
L Ch Thng 108
10-kHz square wave, duty cycle 30%
-
7/31/2019 8051 Chap3 Instruction Ver 02
55/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 55
Write a program that creates a 10-KHz square wave with duty cycle
30% at pin P1.3. Assume that crystal is 24 MHz.
ORG 0000H
lap: SETB P1.3
ACALL delay30
CLR P1.3
ACALL delay70
SJMP lap
Delay30: MOV R4,#15
DJNZ R4,$
RET
Delay70: MOV R4,#35
DJNZ R4,$
RET
ENDL Ch Thng 109
10-kHz square wave, duty cycle 30%
Write a program that creates a 100-kHz square wave at pin P1.1.
Assume that crystal is 12 MHz.
L Ch Thng 110
100-kHz square wave
-
7/31/2019 8051 Chap3 Instruction Ver 02
56/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 56
Write a program that creates a 100-kHz square wave at pin P1.1.
Assume that crystal is 12 MHz.
ORG 0000H
lap: CPL P1.1
NOP
NOP
SJMP lap
END
L Ch Thng 111
100-kHz square wave
Write a program that creates a 100-kHz square wave with duty cycle
40% at pin P1.2. Assume that crystal is 12 MHz.
L Ch Thng 112
100-kHz square wave, duty cycle 40%
-
7/31/2019 8051 Chap3 Instruction Ver 02
57/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 57
Write a program that creates a 100-kHz square wave with duty cycle
40% at pin P1.2. Assume that crystal is 12 MHz.
ORG 0000H
lap: SETB P1.2
NOP
NOP
NOP
CLR P1.2
NOP
NOP
NOP
SJMP lap
END
L Ch Thng 113
100-kHz square wave, duty cycle 40%
CJNE A,#05H,Skip
(Statement 1)
Skip: (Continue)
L Ch Thng 114
CJNE Equal/Not Equal (1)
A = 05H?N
Y
Statement 1
-
7/31/2019 8051 Chap3 Instruction Ver 02
58/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 58
Ex: Write a program to write 40H to internal RAM from location
30H to location 36H.
(source)
ORG 0000H
MOV R1,#30H;Address=30H
Again: MOV @R1,#40H
INC R1
CJNE R1,#37H,Again
END
L Ch Thng 115
An Example
Addr 30H
(Addr) 40H
AddrAddr+1
Addr=37H?N
Y
CJNE A,#05H,Not_Eq
(Statement 1)
SJMP Next
Not_Eq: (Statement 2)
Next: (Continue)
L Ch Thng 116
CJNE Equal/Not Equal (2)
A = 05H?N
Y
Statement 1Statement 2
-
7/31/2019 8051 Chap3 Instruction Ver 02
59/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 59
CJNE A,#05H,Next
Next: JNC GT_Eq
SJMP Continue
GT_Eq: (Statement 1)
Continue: (Continue)
L Ch Thng 117
CJNE Greater Than or Equal/Less Than (1)
A 05H?N
Y
Statement 1
CJNE A,#05H,$+3
JNC GT_Eq
SJMP Continue
GT_Eq: (Statement 1)
Continue: (Continue)
Ex: Examine the content of A, if 5 A 10 then output A to
Port 1; if not, output A to Port 2.
ORG 0
CJNE A,#5,$+3
JC PORT2
CJNE A,#11,$+3JNC PORT2
MOV P1,A
SJMP DONE
PORT2:MOV P2,A
DONE: NOP
END
118L Ch Thng
An Example
-
7/31/2019 8051 Chap3 Instruction Ver 02
60/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 60
CJNE A,#05H,$+3
JNC GT_Eq
(Statement 2)
SJMP Next
GT_Eq: (Statement 1)
Next: (Continue)
L Ch Thng 119
CJNE Greater Than or Equal/Less Than (2)
A 05H?N
Y
Statement 1Statement 2
MOV DPTR,#JUMP_TABLE
MOV A,INDEX_NUMBER
RL A
JMP @A+DPTR
JUMP_TABLE: AJMP CASE0
AJMP CASE1AJMP CASE2
L Ch Thng
Jump Table
120
-
7/31/2019 8051 Chap3 Instruction Ver 02
61/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 61
Given a 100-byte unsigned number string in external RAM at
address starting from 0100H. Write a program that sends positive
numbers to Port 1 and negative numbers to Port 2.
Hint:
- A positive number has MSB = 0.
- A negative number has MSB = 1.
- Use JB / JNB instruction
L Ch Thng
Bit Testing
121
Given a 100-byte unsigned number string in external RAM at
address starting from 0100H. Write a program that sends positive
numbers to Port 1 and negative numbers to Port 2.
ORG 0000H
MOV DPTR,#0100H
MOV R4,#100
loop: MOVX A,@DPTRJNB ACC.7,positive
MOV P2,A
SJMP next
positive: MOV P1,A
next: INC DPTR
DJNZ R4,loop
END
L Ch Thng
Bit Testing
122
-
7/31/2019 8051 Chap3 Instruction Ver 02
62/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 62
Ex: Given a 20-byte string in internal RAM, starting at
address 40H. Write a program that output even numbers
to Port 2.
123L Ch Thng
Your Turn!
Ex: Given a 20-byte string in internal RAM, starting at
address 40H. Write a program that output even numbers
to Port 2.
ORG 0
MOV R4,#20 ; Number of loops
MOV R0,#40H ; Address pointer
LOOP:
MOV A,@R0 ; Read data from internal RAM to A
JB ACC.0,NEXT ; skip if odd number
MOV P2,A ; Output to Port 2 if even number
NEXT:
INC R0
DJNZ R4,LOOP
END
124L Ch Thng
Your Turn!
-
7/31/2019 8051 Chap3 Instruction Ver 02
63/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 63
Ex: Given a 20-byte string in external RAM, starting at
address 4000H. Write a program that output odd numbers
to Port 2.
125L Ch Thng
Your Turn!
Ex: Given a 20-byte string in external RAM, starting at
address 4000H. Write a program that output odd numbers
to Port 2.
ORG 0
MOV R4,#20 ; Number of loops
MOV DPTR,#4000H ; Address of external RAM
LOOP:
MOVX A,@DPTR ; Read data from external RAM to A
JNB ACC.0,NEXT ; skip if even number
MOV P2,A ; Output to Port 2 if odd number
NEXT:
INC DPTR
DJNZ R4,LOOP
END
126L Ch Thng
Your Turn!
-
7/31/2019 8051 Chap3 Instruction Ver 02
64/65
H Bch Khoa TP.HCM L Ch Thng
www.tinyurl.com/thongchile 64
L ChThng
Bit Testing
LOOP: MOV C,P1.0
JNB P1.1,SKIP
CPL C
SKIP: MOV P1.2,C
SJMP LOOP
P1.0
P1.1
P1.2
127
L ChThng
Example Problem
A 4-bit DIP switch and a common-anode 7-segment LED are
connected to an 8051 as shown in the following figure. Write a
program that continually reads a 4-bit code from the DIP switch and
updates the LEDs to display the appropriate hexadecimal character.
For example, if the code 1100B is read, the hexadecimal character
C should appear, thus, segments a through g respectively should
be ON, OFF, OFF, ON, ON, ON, and OFF. Note that setting an 8051port pin to 1 turns the corresponding segment ON.
128
-
7/31/2019 8051 Chap3 Instruction Ver 02
65/65
H Bch Khoa TP.HCM L Ch Thng
129
References
L Ch Thng
I. Scott MacKenzie , The 8051 Microcontroller, 2ndEdition, Prentice-Hall, 1995
Kenneth J. Ayala, The 8051 Microcontroller:Architecture, Programming, and Applications, West
Publishing Company
[email protected] , Lecture notes