Bài giảng The 8051 Microcontroller - Chapter 4: Instruction set how to write a program - Lê Chí Thông
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
The 8051 Microcontroller
Chapter 4
Instruction Set
How to write a program
Lê Chí Thông
sites.google.com/site/chithong
Ho Chi Minh City University of Technology
Lê Chí Thông
1
Chapter 4
1. Addressing Modes
2. Instruction Set
3. How to write a program
Lê Chí Thông
2
sites.google.com/site/chithong
1
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
1. Addressing Modes
•
•
•
•
•
•
•
•
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Lê Chí Thông
3
Coding format- Addressing modes
Lê Chí Thông
4
sites.google.com/site/chithong
2
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Addressing Modes
•
•
•
•
•
•
•
•
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Lê Chí Thông
5
Immediate Addressing
- Source operand is constant
- Use # sign
(Source)
A
0 0 0 0 1 1 0 0
1 1 0 0 0 1 0 0
0 0 0 0 1 1 1 0
MOV
MOV
MOV
ADD
MOV
A,#12
A
A,#0C4H
R1
R1,#1110B
A,#11110001B
DPTR,#2000H
CY
1
A
1 0 1 1 0 1 0 1
DPH 0 0 1 0 0 0 0 0
DPL 0 0 0 0 0 0 0 0
Lê Chí Thông
6
sites.google.com/site/chithong
3
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Addressing Modes
•
•
•
•
•
•
•
•
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Lê Chí Thông
7
Register Addressing
R1
0 0 1 1 0 1 1 0
Ex: MOV
R1,#36H
A
0 0 1 1 0 1 1 0
0 0 0 0 1 1 1 1
0 0 0 0 0 1 1 0
MOV
A,R1
R7
A
MOV R7,#0FH
ANL
INC
A,R7
A
A
A
0 0 0 0 0 1 1 1
0 0 0 0 0 1 1 0
DEC
MOV
INC
A
DPTR,#2000
DPTR
(source)
DPH 0 0 1 0 0 0 0 0
DPL 0 0 0 0 0 0 0 1
Lê Chí Thông
8
sites.google.com/site/chithong
4
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Addressing Modes
•
•
•
•
•
•
•
•
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Lê Chí Thông
9
Direct Addressing
• Access internal RAM and SFRs
MOV A,70H
MOV R0,40H
MOV 56H,A
; copy contents of RAM at 70H to A
; copy contents of RAM at 40H to A
; put contents of A into RAM at 56H
MOV 0D0H,A ; put contents of A into PSW
MOV PSW,A ; same
Lê Chí Thông
10
sites.google.com/site/chithong
5
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Direct Addressing vs. Immediate Addressing
≠
Lê Chí Thông
11
Play with R0-R7 (bank 0) by Direct Addressing
MOV A,04H ≡ MOV A,4 ≡ MOV A,R4
MOV A,07H ≡ MOV A,7 ≡ MOV A,R7
MOV 07H,06H ≡ MOV 7,6 ≠ MOV R7,R6
MOV R2,#5 ; Put 5 in R2
MOV R2,5 ; Put content of RAM at address 5 in R2
MOV R2,5
MOV R2,#5
≠
R2
0 0 0 0 0 1 0 1
R2
05H
Lê Chí Thông
12
sites.google.com/site/chithong
6
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Addressing Modes
•
•
•
•
•
•
•
•
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Lê Chí Thông
13
Indirect Addressing
• 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 of internal RAM at
; address specified by R0 to A
Internal RAM
R0
A
7FH
30H
00H
Lê Chí Thông
14
sites.google.com/site/chithong
7
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Indirect Addressing – External 8-bit address RAM
Ex: MOV
R0,#30H ;R0 ꢀ 30H
MOVX A,@R0 ;Aꢀ(R0): read content of eXternal RAM at
; address specified by R0 to A
External RAM
R0
A
0 0 1 1 0 0 0 0
30H
00H
Lê Chí Thông
15
Indirect Addressing – External 16-bit address RAM
• Uses DPTR register for 16-bit addresses (external memory)
• Use @ sign to access the content of the memory location:@DPTR
Ex:
; DPTR ꢀ Address
; read content of external RAM at
address 4000H to A
;
External RAM
DPTR
40H
A
00H
4000H
00H
Lê Chí Thông
16
sites.google.com/site/chithong
8
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Indirect Addressing – External 16-bit address RAM
Ex:
; DPTR ꢀ Address
; write content of A to external RAM
; at address 31FFH
External RAM
DPTR
31H
A
FFH
31FFH
00H
Lê Chí Thông
17
Register Addressing vs. Indirect Addressing
≠
Internal RAM
R1
7FH
00H
A
Lê Chí Thông
18
sites.google.com/site/chithong
9
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Example of Indirect Addressing
Ex: (Implementation of array)
R0 ꢀ 60H
Clear internal RAM from address
60H to 7FH
(R0) ꢀ 0
MOV R0, #60H
LOOP: MOV @R0, #0
R0 ꢀ R0+1
INC
R0
CJNE R0, #80H, LOOP
N
R0= 80h?
Y
Lê Chí Thông
19
Your Turn!
Write a program to output (send) the content of internal RAM,
from address 20H to 45H, to Port 1
Lê Chí Thông
20
sites.google.com/site/chithong
10
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Addressing Modes
•
•
•
•
•
•
•
•
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Lê Chí Thông
21
Relative Addressing
• 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 memory
at locations 0100H and 0101H
ꢁ PC = 0102H
If LABEL1 is the label representing
an instruction at location 0107H
ꢁ Relative offset is
0107H – 0102H = 5
ꢁ Opcode of SJMP LABEL1 is 80 05
Lê Chí Thông
sites.google.com/site/chithong
11
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Relative Addressing
Ex: SJMP LABEL2 is in memory
at locations 2040H and 2041H
ꢁ PC = 2042H
If LABEL2 is the label representing
an instruction at location 2038H
ꢁ Relative offset is
2038H – 2042H = -10 = F6H
ꢁ Opcode of SJMP LABEL1 is 80 F6
Lê Chí Thông
23
Your Turn!
Ex:
ORG 0000H
SETB P1.0
NOP
AGAIN:
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?
Lê Chí Thông
24
sites.google.com/site/chithong
12
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Addressing Modes
•
•
•
•
•
•
•
•
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Lê Chí Thông
25
Absolute Addressing
• Used in AJMP, ACALL instruction
• Usually used with label ☺
Within any 2K page, the upper five address
bits are the same for the source and
destination addresses.
The lower 11 bits of the destination are
supplied in the instruction
Memory map showing 2K pages
Lê Chí Thông
26
sites.google.com/site/chithong
13
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Addressing Modes
•
•
•
•
•
•
•
•
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Lê Chí Thông
27
Long Addressing
• Used in LCALL and LJMP instruction
• Use full 16-bit address
• Usually used with label ☺
Ex: LCALL SUBPROGRAM1
LJMP
LABEL2
Lê Chí Thông
28
sites.google.com/site/chithong
14
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Addressing Modes
•
•
•
•
•
•
•
•
Immediate Addressing
Register Addressing
Direct Addressing
Indirect Addressing
Relative Addressing
Absolute Addressing
Long Addressing
Indexed Addressing
Lê Chí Thông
29
Indexed Addressing
• Base address (PC or DPTR) + Offset (A) ꢁ Effective Address
• Used with JMP or MOVC
Ex: MOVC
MOVC
A, @A+DPTR
A, @A+PC
@A+DPTR
JMP
Lê Chí Thông
30
sites.google.com/site/chithong
15
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Indexed Addressing
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.
Lê Chí Thông
31
2. 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í Thông
32
sites.google.com/site/chithong
16
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Instruction Types
• Data transfer
• Arithmetic
• Logical
• Boolean variable
• Program branching
Lê Chí Thông
33
8051 Instruction Set Summary
Legend
Lê Chí Thông
34
sites.google.com/site/chithong
17
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
8051 Instruction Set Summary
Data Transfer
Lê Chí Thông
35
8051 Instruction Set Summary
Data Transfer
Lê Chí Thông
36
sites.google.com/site/chithong
18
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
Data Transfer
MOV dest, source
; dest ꢀ source
MOV A,#72H
MOV R4,#62H
MOV B,0F9H
;A=72H
;R4=62H
;B=the content of address F9H of RAM
MOV DPTR,#7634H
MOV DPL,#34H
MOV DPH,#76H
MOV P1,A
;move (out) A to Port 1
≠ MOV A,72H
Note 1:
MOV A,#72H
Note 2:
MOV A,R3 ≡ MOV A,3 ;R3 of bank 0 is location 3
Lê Chí Thông
37
Instruction Types
• Data transfer
• Arithmetic
• Logical
• Boolean variable
• Program branching
Lê Chí Thông
38
sites.google.com/site/chithong
19
ĐH Bách Khoa - ĐHQG TP.HCM
Lê Chí Thông - [email protected]
8051 Instruction Set Summary
Arithmetic Operations
Lê Chí Thông
39
8051 Instruction Set Summary
Arithmetic Operations
Lê Chí Thông
40
sites.google.com/site/chithong
20
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng The 8051 Microcontroller - Chapter 4: Instruction set how to write a program - Lê Chí Thông", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_the_8051_microcontroller_chapter_4_instruction_set.pdf

