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
Trang 1Chapter 3 Instruction Set How to write a program
Trang 2• Summary instruction set ( pdf )
• Full instruction set ( pdf )
Trang 3The First Program
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 4Registers and Data Transfer Instructions
• Register
o Register A = accumulator
o Register B
o Register R0 – R7
• Data Transfer Instruction
o MOV destination, source
Ex:
Trang 5Internal 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
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 7• 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.
Trang 9• 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.
9
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 11• 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
Trang 13Ex: use default bank (Bank 0)
Read the contents of register R5
into the accumulator Read the contents of location 05H into the accumulator
Read the contents of register A
Trang 14Register PSW (Program Status Word)
Trang 15The active bank is selected by bit RS1 (PSW.4) & RS0 (PSW.3)
Ex: MOV PSW, #00010000B ; select bank 2
Trang 17Lê Chí Thông
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
Same function BUT different opcodes (A9 E0 vs F9)
Try it: Input Port 2 to register A ……… 17
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 19Lê Chí Thông
Coding format- Addressing modes
19
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 20- Source operand is constant
A 1 0 1 1 0 1 0 1
CY 1
DPH 0 0 1 0 0 0 0 0 DPL 0 0 0 0 0 0 0 0
Trang 22Direct Addressing
• 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 into RAM at 56H
MOV 0D0H,A ; put contents of A into PSW
MOV PSW,A ; same
Trang 23Direct Addressing vs Immediate Addressing
Lê Chí Thông
MOV A,30H MOV A,#30H
Direct addressing Immediate addressing
23
≠
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 24Play with R0-R7 (bank 0) by Direct Addressing
MOV A,4 MOV A,R4
MOV A,7 MOV A,R7
Trang 26• The address of the source or destination is specified in registers.
• Use registers R0 or R1 for 8-bit address (internal or external
Trang 27Ex: 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,A END
Ex: Write Data to Internal RAM (1)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 28Ex: 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
Trang 29Ex: 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
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 30Ex: 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
Ex: Read Data from Internal RAM
Trang 31Ex: 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
Ex: Data Transfer (Read/Write)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 33• Uses DPTR register for 16-bit addresses (external memory)
• Use @ sign to access the content of the memory location:@DPTR
Ex: MOV DPTR,#4000H ; DPTR Address
MOVX A,@DPTR ; read content of external RAM at
Trang 34Ex: MOV DPTR,#31FFH ; DPTR Address
MOVX @DPTR,A ; write content of A to external RAM
Trang 35Ex: Write a program to move the content of register A to location
0033H in external RAM.
ORG 0000H MOV DPTR,#0033H MOVX @DPTTR,A END
Write Data to External RAM (1)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 36Ex: Write a program to write 40H to location 0030H in external
RAM
ORG 0000H MOV A,#40H MOV DPTR,#0030H MOVX @DPTR,A
END
Write Data to External RAM (2)
Trang 37Ex: Write a program to clear location 0031H in external RAM
ORG 0000H CLR A
MOV DPTR,#0031H MOVX @DPTTR,A END
Clear External RAM
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 38Ex: Write a program to read data from external RAM at address
0032 to register A
ORG 0000H MOV DPTR,#0032H MOVX A,@DPTTR END
Read Data from External RAM
Trang 39Ex: 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
Read/Write External RAM
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 40Write a program to move data from internal RAM at address 56H
to external RAM at address 4123H.
ORG 0000HMOV A, 56HMOV DPTR, #4123HMOVX @DPTR, A
END
Your Turn!
Trang 41Register Addressing vs Indirect Addressing
Trang 42Ex: (Implementation of array)
Clear internal RAM from address
60H to 7FH
MOV R0, #60H LOOP: MOV @R0, #0
INC R0 CJNE R0, #80H, LOOP
Example of Indirect Addressing
R0 60H
(R0) 0 R0 R0+1
R0= 80h?
N
Y
Trang 43Write a program to output (send) the content of internal RAM, from address 20H to 45H, to Port 1
ORG 0000H MOV R0, #20H LOOP:
MOV A, @R0 MOV P1, A INC R0 CJNE R0, #46H, LOOP END
Your Turn!
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 45• 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
Trang 46Ex: SJMP LABEL2 is in memory
Trang 47Ex: 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!
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 48• Used in AJMP, ACALL instruction
• Usually used with label
Absolute Addressing
Memory map showing 2K pages
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
Trang 49• Used in LCALL and LJMP instruction
• Use full 16-bit address
• Usually used with label
Ex: LCALL SUBPROGRAM1
LJMP LABEL2
Long Addressing
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 50• 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
Trang 51Ex: 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
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 538051 Instruction Set Summary
Legend
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 548051 Instruction Set Summary
Data Transfer
Trang 558051 Instruction Set Summary
Data Transfer
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 56Data 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
Trang 57Creating 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,#40H END
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 58Ex: 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 DEC R5 CJNE R5,#0,Again END
Addr 30H
(Addr) 40H AddrAddr+1
Trang 59Ex: 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
Trang 60Ex: 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
Addr=37H?
N
Trang 62Solutions (1)
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 Port1
INC R0 DJNZ R7,loop END
Trang 63Solutions (2)
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
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 64MOV A,#ENTRY_NUMBER MOV DPTR,#TABLE
MOVC A,@A+DPTR TABLE: DB data1, data2, data3, …
Look-up Table
Trang 65Given 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 MOV 34H, A
TABLE:
DB 0,1,4,9,16,25,36,49,64,81 END
Look-up Table - Example
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 66Given a packed-BCD number in location 34H of internal RAM Write
a program that displays the low decade of this number on thecommon-anode 7-seg LED connected to Port 2 (schematic)
Your Turn!
Trang 67D4 e
D3 d
D2 c
D1 b
D0 a
Trang 68ORG 0000HMOV 34H,#25HMOV A,34H
ANL A,#0FHMOV DPTR,#TABLEMOVC A,@A+DPTRMOV P2,A
SJMP DONETABLE: DB 40h,79h,24h,30h,19h
DB 12h,02h,78h,00h,10hDONE: NOP
END(source)
Solutions
Trang 69MOV A, #ENTRY_NUMBER CALL LOOK_UP
…
LOOK_UP:
INC A MOVC A, @A+PC RET
Trang 70MOV DPTR,#JUMP_TABLE MOV A,INDEX_NUMBER
RL A JMP @A+DPTR
… JUMP_TABLE: AJMP CASE0
AJMP CASE1 AJMP CASE2
…
Jump Table
Trang 718051 Instruction Set Summary
Arithmetic Operations
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 728051 Instruction Set Summary
Arithmetic Operations
Trang 738051 Instruction Set Summary
Arithmetic Operations
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 748051 Instruction Set Summary
Flag Effect
Trang 75Arithmetic 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)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 78Your 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
Trang 79Given 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,loop MOV 2FH,A END
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 80• Rotate instructions operate only on A
Trang 82• Note that a shift left is the same as
multiplying by 2, shift right is divide by 2
Trang 848051 Instruction Set Summary
Logic Operations
Trang 85Logic 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 = ……….
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 86Logic Operations (2)
MOV R2,#05H MOV R3,#07H MOV A,R2
SWAP A ORL A,R3
A = ……….
Trang 878051 Instruction Set Summary
Boolean Variable
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 88Boolean Variable
ORG 0 LOOP: MOV C,P1.0
ANL C,P1.1 MOV P1.2,C SJMP LOOP END
Trang 89Your Turn!
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 90ORG 0 LOOP: MOV C,P1.4
CPL C ANL C,P1.5 CPL C
ORL C,P1.6 MOV P1.7,C SJMP LOOP
Trang 918051 Instruction Set Summary
Program Branching
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 928051 Instruction Set Summary
Program Branching
Trang 93SJMP : 8-bit offset LJMP : 11-bit address (2KB segment) AJMP : 16-bit
Unconditional
jumps
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 94Conditional Jumps
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
JNC Jump if CY=0
JB Jump if bit=1
JNB Jump if bit=0
Trang 95ACALL (absolute call)
ACALL is 2-byte instruction the target address of the
subroutine must be within 2K byte range.
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 96• Call is similar to a jump, but
– Call pushes PC on stack before branching
ACALL <address ll> ; stack PC
Trang 97• Return is also similar to a jump, but
– Return instruction pops PC from stack to get
Trang 98MAIN: .
ACALL SUBLABEL
SUBLABEL:
RET the subroutine
call to the subroutine
Subroutines
Trang 99DJNZ – a For loop
Use DJNZ to create a for loop control
Ex: a 10-time loop
MOV R7,#10
…
… (end loop)
DJNZ R7,LOOP
(continue)
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 100DJNZ – a For loop
Use DJNZ to create a 1000-time loop?
Trang 101DJNZ – an example
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
Trang 102Write a program to clear ACC, then add 3 to the
accumulator ten times.
Solution:
MOV A,#0 MOV R2,#10
DJNZ R2,AGAIN ;repeat until R2=0 (10 times) MOV R5,A
DJNZ – another example
Trang 103Write a program to copy a block of 10 bytes from RAM
location starting at 37h to RAM location starting at 59h.
DJNZ – Your Turn!
CuuDuongThanCong.com https://fb.com/tailieudientucntt
Trang 104Write a program to copy a block of 10 bytes from RAM
location starting at 37h to RAM location starting at 59h.
Solution:
L1: MOV A,@R0
MOV @R1,A INC R0
INC R1 DJNZ R2,L1
Solutions
Trang 106ORG 0LOOP: SETB P1.0
ACALL DELAYCLR P1.0
ACALL DELAYSJMP LOOPDELAY: MOV R6,#200
DL1: MOV R7,#250
DJNZ R7,$
DJNZ R6,DL1RET
END(source)
Trang 107ORG 0LOOP: SETB P1.0
ACALL DELAYCLR P1.0
ACALL DELAYSJMP LOOPDELAY: MOV R6,#200
DL1: MOV R7,#250 1MC x 1
DJNZ R7,$ 2 MC x 250DJNZ R6,DL1
RETEND
Blinky Program
501 MC
CuuDuongThanCong.com https://fb.com/tailieudientucntt