Professional Documents
Culture Documents
Chapter 3
Instruction Set
How to write a program
Lê Chí Thông
chithong@hcmut.edu.vn
sites.google.com/site/chithong
Ho Chi Minh City University of Technology
Lê Chí Thông 1
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 2
The First Program
First_program.asm First_program.lst
ORG 0000H LOC OBJ LINE SOURCE
MOV P1,#0FH 0000 1 ORG 0000H
SETB P1.7 0000 75900F 2 MOV P1,#0FH
CLR P1.0 0003 D297 3 SETB P1.7
END 0005 C290 4 CLR P1.0
5 END
First_program.hex 0000H 75H Code
Checksum =
Byte count 2’s complement 0001H 90H Memory
of all data
Load address 0002H 0FH
Opcode
Type 0003H D2H
0004H 97H
:0700000075900FD297C2902A
0005H C2H
:00000001FF Lê Chí Thông 3
end of record type 0006H 90H
Registers and Data Transfer Instructions
• Register
o Register A = accumulator
o Register B
o Register R0 – R7
• Data Transfer Instruction
o MOV destination, source
Ex:
A R0
MOV A,R0
R1 B
MOV R1,B
MOV R1,R3 Lê Chí Thông 4
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í Thông 5
General
purpose
RAM
(30H-7FH)
Special
function
Bit-
registers
addressable
(SFRs)
RAM
(80H-FFH)
(20H-2FH)
Register
banks
(00H-1FH)
Lê Chí Thông 6
General Purpose RAM
• 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.
A 30H
Lê Chí Thông 7
General
purpose
RAM
(30H-7FH)
Special
function
Bit-
registers
addressable
(SFRs)
RAM
(80H-FFH)
(20H-2FH)
Register
banks
(00H-1FH)
Lê Chí Thông 8
Bit Addressable RAM
• 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.
20H A
Register
banks
(00H-1FH)
Lê Chí Thông 10
Register Banks
• 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
o R0 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í Thông 11
Register Banks – Your Turn!
o R1 of Bank 0 is location ………
o R5 of Bank 0 is location ………
o R5 of Bank 1 is location ………
o R1 of Bank 2 is location ………
o R2 of Bank 3 is location ………
o R6 of Bank 3 is location ………
Lê Chí Thông 12
Register Banks
Ex: use default bank (Bank 0)
R5 ≡ 05H
Lê Chí Thông 13
Register PSW (Program Status Word)
Lê Chí Thông 14
Selecting Register Bank
The active bank is selected by bit RS1 (PSW.4) & RS0 (PSW.3)
RS1 RS0 Bank
0 0 0
0 1 1
1 0 2
1 1 3
Register
banks
(00H-1FH)
Lê Chí Thông 16
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 address
Ex: MOV P1, A ; Output register A to Port 1
MOV 90H, A ; same
Same opcode: F5 90
Ex: MOV R1, ACC
MOV R1, 0E0H
Same opcode: A9 E0
Ex: MOV R1, ACC
MOV R1, A
Same function BUT different opcodes (A9 E0 vs. F9)
Try it: Input Port 2 to register ALê ……………………………
Chí Thông 17
Addressing Modes
• Immediate Addressing
• Register Addressing
• Direct Addressing
• Indirect Addressing
• Relative Addressing
• Absolute Addressing
• Long Addressing
• Indexed Addressing
Lê Chí Thông 18
Coding format- Addressing modes
Lê Chí Thông 19
Immediate Addressing
- Source operand is constant
- Use # sign
(Source)
MOV A,#12 A 0 0 0 0 1 1 0 0
MOV A,#0C4H A 1 1 0 0 0 1 0 0
MOV R1,#1110B R1 0 0 0 0 1 1 1 0
ADD A,#11110001B CY 1 A 1 0 1 1 0 1 0 1
MOV DPTR,#2000H
DPH 0 0 1 0 0 0 0 0 DPL 0 0 0 0 0 0 0 0
Lê Chí Thông 20
Register Addressing
Ex: MOV R1,#36H R1 0 0 1 1 0 1 1 0
MOV A,R1 A 0 0 1 1 0 1 1 0
MOV R7,#0FH R7 0 0 0 0 1 1 1 1
ANL A,R7 A 0 0 0 0 0 1 1 0
INC A A 0 0 0 0 0 1 1 1
DEC A A 0 0 0 0 0 1 1 0
MOV DPTR,#2000
Lê Chí Thông 22
Direct Addressing vs. Immediate Addressing
A 30H A 0 0 1 1 0 0 0 0
Lê Chí Thông 23
Play with R0-R7 (bank 0) by Direct Addressing
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
Lê Chí Thông 24
Addressing Modes
• Immediate Addressing
• Register Addressing
• Direct Addressing
• Indirect Addressing
• Relative Addressing
• Absolute Addressing
• Long Addressing
• Indexed Addressing
Lê Chí Thông 25
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 0 0 1 1 0 0 0 0
7FH
A 30H
Lê Chí Thông 27
Ex: Write Data to Internal RAM (2)
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
END
Method 2: Indirect addressing
ORG 0000H
MOV R0,#30H
MOV @R0,#40H
END
Lê Chí Thông 28
Ex: Clear Internal RAM
Ex: 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
END
Method 2: Indirect addressing
ORG 0000H
MOV R0,#31H
MOV @R0,#0
END
Lê Chí Thông 29
Ex: Read Data from Internal RAM
Ex: Write a program to move (read) 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,@R0
END
Lê Chí Thông 30
Ex: Data Transfer (Read/Write)
Ex: Write a program to move (transfer) the content of internal RAM
from address 34H to address 35H, using 2 methods of addressing:
direct addressing and indirect addressing.
Method 1: Direct addressing
ORG 0000H
MOV 35H,34H
END
Method 2: Indirect addressing
ORG 0000H
MOV R0,#34H
MOV A,@R0
INC R0
MOV @R0,A
END
Lê Chí Thông 31
Indirect Addressing – External 8-bit address RAM
External RAM
R0 0 0 1 1 0 0 0 0
A 30H
External RAM
DPTR 40H 00H
A 4000H
External RAM
DPTR 31H FFH
A 31FFH
Lê Chí Thông 35
Write Data to External RAM (2)
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í Thông 36
Clear External RAM
Ex: Write a program to clear location 0031H in external RAM.
ORG 0000H
CLR A
MOV DPTR,#0031H
MOVX @DPTTR,A
END
Lê Chí Thông 37
Read Data from External RAM
Ex: Write a program to read data from external RAM at address
0032 to register A.
ORG 0000H
MOV DPTR,#0032H
MOVX A,@DPTTR
END
Lê Chí Thông 38
Read/Write External RAM
Ex: Write a program to move the content of location 0034H in
external RAM to location 0035H in external RAM.
ORG 0000H
MOV DPTR,#0034H
MOVX A,@DPTTR
INC DPTR
MOVX @DPTR,A
END
Lê Chí Thông 39
Your Turn!
Write a program to move data from internal RAM at address 56H
to external RAM at address 4123H.
Lê Chí Thông 40
Luyện tập
1. Lấy ô nhớ 32H của RAM nội ghi vào ô nhớ 33H của RAM ngoài
2. Nhập từ Port 1 và ghi vào (34H) của RAM nội
3. Nhập từ Port 2 và ghi vào (1000H) của RAM ngoài
4. Lấy ô nhớ (56H) của RAM nội xuất ra Port 1
5. Lấy ô nhớ (2002H) của RAM ngoài xuất ra Port 2
6. Lấy ô nhớ (41H) của RAM nội cộng với Port 1 và ghi kết quả
vào (42H) của RAM nội.
7. Nhập từ Port 1, cộng với Port 2 và xuất kết quả ra Port 0
8. Lấy (2001H) của RAM ngoài cộng với (35H) của RAM nội, xuất
kết quả ra Port 3 và ghi vào (36H) của RAM nội & (2002H) của
RAM ngoài
Lê Chí Thông 41
Luyện tập
1. Lấy ô nhớ 32H của RAM nội ghi vào ô nhớ 33H của RAM ngoài
Lê Chí Thông 42
Luyện tập
4. Lấy ô nhớ (56H) của RAM nội xuất ra Port 1
6. Lấy ô nhớ (41H) của RAM nội cộng với Port 1 và ghi kết quả vào
(42H) của RAM nội.
Lê Chí Thông 43
Luyện tập
7. Nhập từ Port 1, cộng với Port 2 và xuất kết quả ra Port 0
8. Lấy (2001H) của RAM ngoài cộng với (35H) của RAM nội, xuất
kết quả ra Port 3 và ghi vào (36H) của RAM nội & (2002H) của RAM
ngoài
Lê Chí Thông 44
Register Addressing vs. Indirect Addressing
A R1
Register addressing
Internal RAM
R1
7FH
00H
Indirect addressing
Lê Chí Thông 45
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?
Lê Chí Thông 46
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 47
Addressing Modes
• Immediate Addressing
• Register Addressing
• Direct Addressing
• Indirect Addressing
• Relative Addressing
• Absolute Addressing
• Long Addressing
• Indexed Addressing
Lê Chí Thông 48
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
Lê Chí Thông 50
Your Turn!
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?
Lê Chí Thông 51
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
Lê Chí Thông 52
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 53
Indexed Addressing
• 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
Lê Chí Thông 54
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 55
Instruction Types
• Data transfer
• Arithmetic
• Logical
• Boolean variable
• Program branching
Lê Chí Thông 56
8051 Instruction Set Summary
Legend
Lê Chí Thông 57
8051 Instruction Set Summary
Data Transfer
Lê Chí Thông 58
8051 Instruction Set Summary
Data Transfer
Lê Chí Thông 59
Data Transfer
MOV dest, source ; dest source
MOV A,#72H ;A=72H
MOV R4,#62H ;R4=62H
MOV B,0F9H ;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
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í Thông 60
Creating a Loop (1)
Ex: Write a program to write 40H to internal RAM from location
30H to location 36H.
Lê Chí Thông 61
Creating a Loop (2)
Ex: Write a program to write 40H to internal RAM Loop 7
from location 30H to location 36H.
(source) Addr 30H
(Addr) 40H
AddrAddr+1
LoopLoop-1
N
Loop=0?
Lê Chí Thông Y 62
Creating a Loop (3)
Ex: Write a program to write 40H to internal RAM Loop 7
from location 30H to location 36H.
(source) Addr 30H
(Addr) 40H
AddrAddr+1
LoopLoop-1
N
Loop=0?
Lê Chí Thông Y 63
Creating a Loop (4)
Ex: Write a program to write 40H to internal RAM
from location 30H to location 36H.
(source)
Addr 30H
(Addr) 40H
AddrAddr+1
N
Addr=37H?
Y
Lê Chí Thông 64
Your Turn!
Write a program to read data from internal RAM from
location 20H to location 29H and output to Port 1
Lê Chí Thông 65
Solutions (1)
Write a program to read data from internal RAM from location 20H
to location 29H and output to Port 1
Lê Chí Thông 66
Solutions (2)
Write a program to read data from internal RAM from location 20H
to location 29H and output to Port 1
Lê Chí Thông 67
Look-up Table
MOV A,#ENTRY_NUMBER
MOV DPTR,#TABLE
MOVC A,@A+DPTR
TABLE: DB data1, data2, data3, …
Lê Chí Thông 68
Look-up Table - Example
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,81
END
Lê Chí Thông 69
Your Turn!
Given a packed-BCD number in location 34H of internal RAM. Write
a program that displays the low decade of this number on the
common-anode 7-seg LED connected to Port 2 (schematic).
Lê Chí Thông 70
Create the Table
D7 D6 D5 D4 D3 D2 D1 D0
g f e d c b a
0 0 1 0 0 0 0 0 0 40H
1 0 1 1 1 1 0 0 1 79H
2 0 0 1 0 0 1 0 0 24H
3 0 0 1 1 0 0 0 0 30H
4 0 0 0 1 1 0 0 1 19H
5 0 0 0 1 0 0 1 0 12H
6 0 0 0 0 0 0 1 0 02H
7 0 1 1 1 1 0 0 0 78H
8 0 0 0 0 0 0 0 0 00H
9 0 0 0 1 0 0 0 0 10H
Lê Chí Thông 71
Solutions
(source)
Lê Chí Thông 72
Look-up Table
MOV A, #ENTRY_NUMBER
CALL LOOK_UP
…
LOOK_UP:
INC A
MOVC A, @A+PC
RET
TABLE:
DB data1, data2, data3, …
Lê Chí Thông 73
Jump Table
MOV DPTR,#JUMP_TABLE
MOV A,INDEX_NUMBER
RL A
JMP @A+DPTR
…
JUMP_TABLE: AJMP CASE0
AJMP CASE1
AJMP CASE2
…
Lê Chí Thông 74
Instruction Types
• Data transfer
• Arithmetic
• Logical
• Boolean variable
• Program branching
Lê Chí Thông 75
8051 Instruction Set Summary
Arithmetic Operations
Lê Chí Thông 76
8051 Instruction Set Summary
Arithmetic Operations
Lê Chí Thông 77
8051 Instruction Set Summary
Arithmetic Operations
Lê Chí Thông 78
8051 Instruction Set Summary
Flag Effect
Lê Chí Thông 79
Arithmetic Operations (1)
ADD A,#34H ; Immediate addressing A A + 34H
ADD A,34H ;Direct addressing A A + (34H)
ADD A,R1 ;Register addressing A A + R1
ADD A,@R1 ;Indirect addressing A A + (R1)
Lê Chí Thông 80
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í Thông 81
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í Thông 82
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í Thông 83
Solutions
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í Thông 84
Rotate
• 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
Lê Chí Thông 85
Rotate through Carry
C
RRC A
Lê Chí Thông 87
Swap
SWAP A
Lê Chí Thông 88
Instruction Types
• Data transfer
• Arithmetic
• Logical
• Boolean variable
• Program branching
Lê Chí Thông 89
8051 Instruction Set Summary
Logic Operations
Lê Chí Thông 90
Logic Operations (1)
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 = …………………………….
Lê Chí Thông 91
Logic Operations (2)
MOV R2,#05H
MOV R3,#07H
MOV A,R2
SWAP A
ORL A,R3
A = ………………………….
Lê Chí Thông 92
Instruction Types
• Data transfer
• Arithmetic
• Logical
• Boolean variable
• Program branching
Lê Chí Thông 93
8051 Instruction Set Summary
Boolean Variable
Lê Chí Thông 94
Boolean Variable
ORG 0
LOOP: MOV C,P1.0
ANL C,P1.1
MOV P1.2,C
SJMP LOOP
END
Lê Chí Thông 95
Your Turn!
Lê Chí Thông 96
Solutions
Lê Chí Thông 97
Instruction Types
• Data transfer
• Arithmetic
• Logical
• Boolean variable
• Program branching
Lê Chí Thông 98
8051 Instruction Set Summary
Program Branching
Lê Chí Thông 99
8051 Instruction Set Summary
Program Branching
AJMP(absolute jump)
In this 2-byte instruction, It allows a jump to any memory location
within the 2k block of program memory.
SJMP(short jump)
In this 2-byte instruction. The relative address range of 00-FFH is
divided into forward and backward jumps, that is , within -128 to
+127 bytes of memory relative to the address of the current PC.
RET ; PC stack
MAIN: ...
ACALL SUBLABEL
...
...
SUBLABEL: ...
... the subroutine
RET
(Addr) 40H
AddrAddr+1
LoopLoop-1
N
Loop=0?
P1.0 1
Delay
P1.0 0
Delay
schematic
Delay tL
tH tL
Delay
P1.0 0
Delay
If tH = tL
P1.3 1
tH tL
Delay tH P1.3 NOT (P1.3)
T
P1.3 0 Delay
Delay tL
Delay
P1.3 1
tH tL
T
Delay tH
P1.3 0
Delay tL
P1.3 1
Delay tH
P1.3 0
Delay tL
N CJNE A,#05H,Skip
A = 05H?
(Statement 1)
Skip: (Continue)
Y
Statement 1
(Addr) 40H
AddrAddr+1
N
Addr=37H?
Y
Lê Chí Thông 132
CJNE – Equal/Not Equal (2)
CJNE A,#05H,Not_Eq
N
A = 05H? (Statement 1)
SJMP Next
Y Not_Eq: (Statement 2)
Statement 2 Statement 1 Next: (Continue)
CJNE A,#05H,$+3
JC LessThan
(Statement 1)
LessThan: (Continue)
CJNE A,#05H,$+3
JNC GT_Eq
SJMP Continue
GT_Eq: (Statement 1)
Continue: (Continue)
N
CJNE A,#05H,$+3
A ≥ 05H? JNC GT_Eq
(Statement 2)
Y SJMP Next
Statement 2 Statement 1 GT_Eq: (Statement 1)
Next: (Continue)
Hint:
- A positive number has MSB = 0.
- A negative number has MSB = 1.
- Use JB / JNB instruction
--