chương'3 tập'lệnh'họ' msc151 -...
TRANSCRIPT
Chương'3Tập'lệnh'họ'MSC151
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ• Có$8$kiểu$định$địa$chỉ$trong$8051:
1. Thanh'ghi'(register)'dùng'trong'trường'hợp'dữ'liệu'chứa'trong'thanh'ghi'(register'data).
2. Tức'thời'(immediate)'dùng'cho'dữ'liệu'hằng'số'(constant'data).
3. Trực'tiếp'(direct)'dùng'cho'dữ'liệu'chứa'trong'RAM'(RAM'data).
4. Gián'tiếp'(register'indirect)'dùng'cho'dữ'liệu'chứa'trong'RAM'(RAM'data).
5. Chỉ'số'(indexed)'dùng'cho'dữ'liệu'chứa'trong'ROM'(ROM'data).
6. Tương'đối'(relative)
7. Tuyệt'đối'(absolute)
8. Dài'(long)
2
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ1. Định'địa'chỉ'thanh'ghi'(register)$:
‣ Cả'toán'hạng'nguồn'và'toán'hạng'đích'đều'có'thể'là'một'trong'các'thanh'ghi'của'CPU.
‣ Các'thanh'ghi'trong'8051':
3
A B
R0 R1
R3 R4
R2
R5
R7 R6
DPH DPL
PC
DPTR
PC
Some 8051 16-bit Register
Some 8-bit Registers of the 8051
! A (Accumulator) ! B ! PSW (Program Status Word) ! SP (Stack Pointer) ! PC (Program Counter) ! DPTR (Data Pointer)
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ1. Định'địa'chỉ'thanh'ghi'(register)$:
‣ Ví'dụ':
' MOV'R0,A
' MOV'A,R7
' ADD'A,R4
' ADD'A,R7
' MOV'DPTR,#25F5H
' MOV'R5,DPL
' MOV'R1,DPH
‣ Chú'ý':'có'thể'chuyển'dữ'liệu'giữa'thanh'ghi'A'và'Rn'(n'='0...7)'nhưng'không'thể'chuyển'dữ'liệu'giữa'các'thanh'ghi'Rn.
Ví'dụ':'MOV'R4,'R7'là'sai.
4
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ2. Định'địa'chỉ'tức'thời'(Immediate)$:$
‣ Toán'hạng'nguồn'là'một'hằng'số'được'xác'định'bởi'giá'trị'của'nó.
‣ Toán'hạng'tức'thời'được'nhận'biết'nhờ'vào'ký'tự'#'đặt'trước'chúng.
5
mov A, #0 ;put 0 in the accumulator ;A = 00000000
mov R4, #11h ;put 11hex in the R4 register ;R4 = 00010001
mov B, #11 ;put 11 decimal in b register ;B = 00001011
mov DPTR,#7521h ;put 7521 hex in DPTR ;DPTR = 0111010100100001
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ2. Định'địa'chỉ'tức'thời'(Immediate)$:$
‣ Một'số'chú'ý:
✓ Luôn'đặt'dấu'#'trước'dữ'liệu'tức'thời.
✓ Chỉ'có'toán'hạng'nguồn'mới'được'phép'định'địa'chỉ'tức'thời.
✓ Thêm'“h”'vào'sau'số'thập'lục'phân,'“b”'sau'số'nhị'phân.'Nếu'không'ghi'chú'gì'thêm'thì'mặc'định'là'số'thập'phân.
✓ Dùng'dấu'nháy'đơn'‘’'để'đóng'khung'ký'tự.
✓ Với'những'số'thập'lục'phân'bắt'đầu'bởi'A1F,'phải'thêm'số'0'phía'trước.
Ví'dụ:'
6
Add “#” before any immediate dataOnly the source operand can be immediateAdd “h” after a base-16 number, “b” after a base-2 number; otherwise assumed base-10Use ‘ ’ to enclose any character
Notes of Immediate AddressingNotes of Immediate Addressing
Use ‘ ’ to enclose any characterPrecede all base-16 numbers that begin with A-F by a “0”
MOV A,#ABh
MOV A,#0ABH
54
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ3. Định'địa'chỉ'trực'tiếp'(direct)$:
• truy'suất'dữ'liệu'bằng'cách'dùng'trực'tiếp'địa'chỉ'81bits'của'vùng'nhớ.
• thường'dùng'cho'vùng'nhớ'30H17FH'của'RAM.
• Ví'dụ':
7
mov a, 70h ;copy contents of RAM at 70h to amov R0,40h ;copy contents of RAM at 70h to amov 56h,a ;put contents of a at 56h to amov 0D0h,a ;put contents of a into PSW
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ3. Định'địa'chỉ'trực'tiếp'(direct)$:
• dùng'cho'các'thanh'ghi'R01R7'với'địa'chỉ'trực'tiếp.
• Ví'dụ':
• Chú'ý':
8
Addressing Modes2) Direct Mode – play with R0-R7 by direct
addressMOV A,4 ≡≡≡≡ MOV A,R4
MOV A,7 ≡≡≡≡ MOV A,R7MOV A,7 ≡≡≡≡ MOV A,R7
MOV 7,6 ≡≡≡≡ MOV R7,R6
MOV R2,#5 ;Put 5 in R2MOV R2,5 ;Put content of RAM at 5 in R2
56
Addressing Modes2) Direct Mode – play with R0-R7 by direct
addressMOV A,4 ≡≡≡≡ MOV A,R4
MOV A,7 ≡≡≡≡ MOV A,R7MOV A,7 ≡≡≡≡ MOV A,R7
MOV 7,6 ≡≡≡≡ MOV R7,R6
MOV R2,#5 ;Put 5 in R2MOV R2,5 ;Put content of RAM at 5 in R2
56
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ3. Định'địa'chỉ'trực'tiếp'(direct)$:
9
Examples of Direct AddressingExamples of Direct Addressing
Instruction OperationMOV 80h, A orMOV P0, A
Copy contents of register A to location 80h (Port 0 latch)
MOV A, 80h orMOV A, P0
Copy contents of location 80h (Port 0 pins) to register A
Note: No “#” sign in the instruction
MOV A, P0 register A
MOV A, ABC Copy contents from direct address with label ABC to register A
MOV R0, 12h Copy contents from RAM location 12h to register R0
MOV 0A8h, 77h orMOV IE, 77h
Copy contents from RAM location 77h to IE register of SFRs MOV direct,direct
ABC EQU 80h ; equateMOV A, ABC ; Port 0 to A
58
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ3. Định'địa'chỉ'trực'tiếp'(direct)$:
10
Examples of Direct AddressingExamples of Direct Addressing
MOV A, 2 ; copy location 02 (R2) to AMOV B, 2 ; copy location 02 (R2) to BMOV 7, 2 ; copy location 02 to 07 (R2 to R7)
; since “MOV R7, R2” is invalid
MOV DIRECT, DIRECT
59
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ3. Định'địa'chỉ'trực'tiếp'(direct)$:
• Chú'ý':'khi'làm'việc'với'ngăn'xếp'(stack),'phải'sử'dụng'cách'định'địa'chỉ'trực'tiếp.
' ' PUSH' direct
' ' POP' ' direct
• Ví'dụ':
11
Stack and Direct Addressing ModeStack and Direct Addressing Mode
Only direct addressing mode is allowed for pushing onto the stack
PUSH A (Invalid)PUSH 0E0h (Valid)
PUSH directPOP direct
PUSH 0E0h (Valid)PUSH R3 (Invalid)PUSH 03 (Valid)POP R4 (Invalid)POP 04 (Valid)
60
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ3. Định'địa'chỉ'trực'tiếp'(direct)$:
• Ví'dụ':'Hãy'viết'đoạn'chương'trình'chuyển'dữ'liệu'từ'các'thanh'ghi'R5,'R6'và'A'vào'stack,'sau'đó'lấy'dữ'liệu'ra'các'thanh'ghi'R2,'R3'và'B'sao'cho'nội'dung'thanh'ghi'B'='A,'R2'='R6,'R3'='R5.
12
ExampleExample
PUSH 05 ; push R5 onto stackPUSH 06 ; push R6 onto stackPUSH 0E0h ; push register A onto stack
Show the code to push R5, R6, and A onto the stack and then pop them back into R2, R3, and B, where register B = register A, R2 = R6, and R3 = R5.
PUSH 0E0h ; push register A onto stackPOP 0F0h ; pop top of stack into register B
; now register B = register APOP 02 ; pop top of stack into R2
; now R2 = R6POP 03 ; pop top of stack into R3
; now R3 = R5
61
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ3. Định'địa'chỉ'trực'tiếp'(direct)$:
• Một'số'ghi'chú':
‣ Giá'trị'địa'chỉ'được'giới'hạn'trong'1'byte'(00'1'FFH)'(tương'ứng'với'1281byte'RAM'và'SFR).
‣ Sử'dụng'lệnh'MOV'để'chuyển'dữ'liệu'từ'một'ô'nhớ'hay'thanh'ghi'vào'chính'nó'sẽ'gây'nên'những'kết'quả'không'chính'xác.
Ví'dụ':'MOV'A,'A'(lỗi).
‣ Dùng'lệnh'MOV'để'chuyển'dữ'liệu'ra'một'cổng'(move'data'to'port)'sẽ'làm'thay'đổi'mạch'chốt'của'cổng'(port'latch).
‣ Dùng'lệnh'MOV'để'nhận'dữ'liệu'từ'cổng'(move'data'from'port)'tương'ứng'với'việc'nhận'dữ'liệu'từ'các'chân'của'cổng'(data'from'port'pins).
13
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ4. Định'địa'chỉ'gián'tiếp'(indirect)$:
• Địa'chỉ'của'toán'hạng'nguồn'hay'toán'hạng'đích'được'đặt'trong'các'thanh'ghi.
• Dùng'các'thanh'ghi'R0'hoặc'R1'cho'các'địa'chỉ'81bit'(khoảng'địa'chỉ'từ'00h'đến'7Fh).
Ví'dụ':'MOV'A,'@R1
• Dùng'thanh'ghi'DPTR'cho'các'địa'chỉ'161bit.
• Phải'đặt'ký'hiệu'“@”'phía'trước'tên'của'thanh'ghi'để'chỉ'rằng'đang'ở'chế'độ'định'địa'chỉ'gián'tiếp.
14
Addressing Modes4) Register Indirect – the address of the source or
destination is specified in registers
Uses registers R0 or R1 for 8-bit address:mov psw, #0 ; use register bank 0mov r0, #3Chmov @r0, #3 ; M[3Ch] 3
Uses DPTR register for 16-bit addresses:mov dptr, #9000h ; dptr 9000hmovx a, @dptr ; a M[9000h]
Note that 9000h is an address in external memory67
Addressing Modes4) Register Indirect – the address of the source or
destination is specified in registers
Uses registers R0 or R1 for 8-bit address:mov psw, #0 ; use register bank 0mov r0, #3Chmov @r0, #3 ; M[3Ch] 3
Uses DPTR register for 16-bit addresses:mov dptr, #9000h ; dptr 9000hmovx a, @dptr ; a M[9000h]
Note that 9000h is an address in external memory67
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ4. Định'địa'chỉ'gián'tiếp'(indirect)$:
15
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ4. Định'địa'chỉ'gián'tiếp'(indirect)$:
16
Program memory
Addresses
ACC
R0ADD A, @R0200
201
10
31
ACC
R0
31
22⊕⊕⊕⊕
Register Indirect Addressing Register Indirect Addressing (eg. ADD A,@R0)(eg. ADD A,@R0)
AfterAfterBeforeBefore
201
Data memory
1231
32
30
31 31
8051 Internal data memory71
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ4. Định'địa'chỉ'gián'tiếp'(indirect)$:
17
Instruction Operation MOV @R1, A Copy the data in A to the address pointed to by
the contents of R1
MOV A, @R0 Copy the contents of the address pointed to by
register R0 to the A register
MOV @R1, #35h Copy the number 35h to the address pointed to
Examples of Indirect AddressingExamples of Indirect Addressing
MOV @R1, #35h Copy the number 35h to the address pointed to
by register R1
MOV @R0, 80h or
MOV @R0, P0
Copy the contents of the port 0 pins to the
address pointed to by register R0.
MOVX A, @R0 Copy the contents of the external data address
pointed to by register R0 to the A register
MOVX A, @DPTR Copy the contents of the external data address
pointed to by register DPTR to the A register
MOV @Ri,#data where i = 0 or 1
72
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ5. Định'địa'chỉ'chỉ'số':
• sử'dụng'một'thanh'ghi'nền'(base'address)'(hoặc'bộ'đếm'chương'trình'PC'hay'con'trỏ'dữ'liệu'DPTR)'và'một'offset'(thanh'ghi'tích'lũy'A)'để'tạo'thành'dạng'địa'chỉ'hiệu'dụng'cho'lệnh'JMP'hoặc'MOVC.
• Ví'dụ:
18
Addressing Modes
5) Register Indexed Mode – source or destination address is the sum of the base address and the accumulator (Index)
• Base address can be DPTR or PCmov dptr, #4000hmov a, #5movc a, @a + dptr ;a M[4005]
80
Addressing Modes
5) Register Indexed Mode continue
• Base address can be DPTR or PCORG 1000hORG 1000h
1000 mov a, #51002 movc a, @a + PC ;a M[1008]1003 Nop
• Lookup Table • MOVC only can read internal code memory
PC
81
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ5. Định'địa'chỉ'chỉ'số':
• Ví'dụ:'MOVC'A,'@A+DPTR
19
Program memory
ACC
DPTR
00 10
ACC
56⊕⊕⊕⊕
Indexed AddressingIndexed Addressing Example: MOVC A,@A+DPTR
AfterAfterBeforeBefore
DPTR
MOVC A, @A + DPTR2000
2001
41 3156
MOVC A, @A + DPTR
83
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ5. Định'địa'chỉ'chỉ'số':
• Ví'dụ:
20
Instruction Operation MOVC A, @A + DPTR Copy the code byte, found at the ROM
address formed by adding register A and the DPTR register, to A
MOVC A, @A + PC Copy the code byte, found at the ROM
Examples of Indexed AddressingExamples of Indexed Addressing
MOVC A, @A + PC Copy the code byte, found at the ROM address formed by adding A and the PC, to A
JMP @A + DPTR Jump to the address formed by adding A to the DPTR, this is an unconditional jump and will always be done.
84
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ5. Định'địa'chỉ'chỉ'số':
• Ví'dụ:'Hãy'viết'một'đoạn'chương'trình'liên'tục'lấy'giá'trị'x'từ'P1'và'đưa'giá'trị'x2'ra'P2.
21
Example (lookExample (look--up table)up table)Write a program to get the x value from P1 and send x2 to P2, continuously.
ORG 0MOV DPTR, #300h ; load look-up table addressMOV A, #0FFh ; A = FFMOV P1, A ; configure P1 as input port
BACK: MOV A, P1 ; get XBACK: MOV A, P1 ; get XMOV A, @A+DPTR ; get X square from tableMOV P2, A ; issue it to P2SJMP BACK ; keep doing it
ORG 300hTABLE:DB 0, 1, 4, 9, 16, 25, 36, 49, 64, 81
END
85
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ6. Định'địa'chỉ'tương'đối:
• Chỉ'được'sử'dụng'cho'các'lệnh'nhảy'(JMP).
• Địa'chỉ'tương'đối'(còn'gọi'là'offset)'là'một'giá'trị'81bit'có'dấu'(1128'đến'+127).
• Giá'trị'này'được'cộng'với'bộ'đếm'chương'trình'(PC)'để'tạo'ra'địa'chỉ'của'lệnh'tiếp'theo'cần'được'thực'thi.
• Bộ'đếm'chương'trình'(PC)'được'tăng'đến'địa'chỉ'theo'sau'lệnh'nhảy'⇒'địa'chỉ'mới'liên'quan'đến'lệnh'kế'tiếp,'không'liên'quan'đến'địa'chỉ'của'lệnh'nhảy.
22
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ6. Định'địa'chỉ'tương'đối'(relative)':
• Tính'offset'cho'kiểu'định'địa'chỉ'tương'đối:
23
The 8051 Microcontroller, 4eBy I. Scott MacKenzie and Raphael C.-W. Phan
© 2007 Pearson Education, Inc.Pearson Prentice Hall
Upper Saddle River, NJ 07458
FIGURE 3–2 Calculating the offset for relative addressing. (a) Short jump ahead in memory (b) Short jump back in memory
The 8051 Microcontroller, 4eBy I. Scott MacKenzie and Raphael C.-W. Phan
© 2007 Pearson Education, Inc.Pearson Prentice Hall
Upper Saddle River, NJ 07458
FIGURE 3–2 Calculating the offset for relative addressing. (a) Short jump ahead in memory (b) Short jump back in memory
Offset'tương'đối'='byte'thấp'của'(địa'chỉ'nhảy'đến'1'địa'chỉ'lệnh'kế)
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ6. Định'địa'chỉ'tương'đối:
24
Branch OpcodeOffset
Next Opcode
Program Counter
Relative AddressingRelative Addressing
Program counter + offset
= Effective address
= address of next instruction
+ Offset
Next Instruction
94
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ6. Định'địa'chỉ'tương'đối:
• Ví'dụ:
25
Instruction Operation SJMP NXT Jump to relative address with the label 'NXT'; this
is an unconditional jump and is always taken.
DJNZ R1, DWN Decrement register R1 by 1 and jump to the relative address specified by the label 'DWN' if
Examples of Relative AddressingExamples of Relative Addressing
relative address specified by the label 'DWN' if the result of R1 is not zero.
0035
95
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ7. Định'địa'chỉ'tuyệt'đối'(absolute)':
• Chỉ'được'sử'dụng'cho'các'lệnh'ACALL'và'AJMP.
• Các'lệnh'này'cho'phép'rẽ'nhánh'chương'trình'trong'trang'2K'hiện'hành'của'bộ'nhớ'chương'trình'bằng'cách'cung'cấp'11'bits'thấp'của'địa'chỉ'đích.
• 5'bits'cao'của'địa'chỉ'đích'là'5'bits'cao'hiện'hành'trong'bộ'đếm'chương'trình'⇒'lệnh'theo'sau'lệnh'rẽ'nhánh'và'đích'của'lệnh'rẽ'nhánh'phải'ở'trong'cùng'1'trang'2K.
26
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ7. Định'địa'chỉ'tuyệt'đối'(absolute)':
27
Absolute AddressingAbsolute AddressingACALL address11AJMP address11ORG 00H ; reset location
LJMP START ; 3 bytes instruction
ORG 3FFEHSTART: ACALL FORWARD ; 2 bytes instruction; now code address at 4000H
LJMP TEST
ORG 47FFH ; 010001111111111BORG 47FFH ; 010001111111111BFORWARD:
RET
ORG 5800H ; 0101100000000000BBACKWARD:
RET
ORG 5FFDHTEST: ACALL BACKWARD ; 2 bytes instruction; now code address at 5FFFH
SJMP $END
97
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ8. Định'địa'chỉ'dài'(long'distance)':
• Chỉ'dùng'cho'các'lệnh'LCALL'và'LJMP.
• Các'lệnh'này'chứa'địa'chỉ'đích'16'bits.
• Ưu'điểm':'giúp'sử'dụng'hết'toàn'bộ'không'gian'nhớ'chương'trình'64K.
• Khuyết'điểm':'lệnh'dài'đến'31byte'và'phụ'thuộc'vào'vị'trí.
28
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ8. Định'địa'chỉ'dài'(long'distance)':
29
8051 Instruction Format• Long distance address
• Range = (0000h ~ FFFFh)
0700 1 org 0700h
Op code A15-A8 A7-A0
0700 1 org 0700h0700 020707 2 ajmp next ;next=0707h0703 00 3 nop0704 00 4 nop0705 00 5 nop0706 00 6 nop
7 next:8 end
98
8051 Instruction Format• Long distance address
• Range = (0000h ~ FFFFh)
0700 1 org 0700h
Op code A15-A8 A7-A0
0700 1 org 0700h0700 020707 2 ajmp next ;next=0707h0703 00 3 nop0704 00 4 nop0705 00 5 nop0706 00 6 nop
7 next:8 end
98
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ
30
The 8051 Microcontroller, 4eBy I. Scott MacKenzie and Raphael C.-W. Phan
© 2007 Pearson Education, Inc.Pearson Prentice Hall
Upper Saddle River, NJ 07458
FIGURE 3–1 8051 Addressing modes. (a) Register addressing (b) Direct addressing (c) Indirect addressing (d) Immediate addressing (e) Relative addressing (f) Absolute addressing (g) Long addressing (h) Indexed addressing
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ
31
Op code Direct address
Op code Immediate data
! Immediate addressing
add a,#3dh ;machine code=243d
! Direct addressing
mov r3,0E8h ;machine code=ABE8
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ
32
Op code n n n
! Register addressing
070D E8 mov a,r0 ;E8 = 1110 1000 070E E9 mov a,r1 ;E9 = 1110 1001 070F EA mov a,r2 ;EA = 1110 1010 0710 ED mov a,r5 ;ED = 1110 1101 0711 EF mov a,r7 ;Ef = 1110 1111 0712 2F add a,r7 0713 F8 mov r0,a 0714 F9 mov r1,a 0715 FA mov r2,a 0716 FD mov r5,a 0717 FD mov r5,a
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ
33
Op code i
! Register indirect addressing
mov a, @Ri ; i = 0 or 1
070D E7 mov a,@r1 070D 93 movc a,@a+dptr 070E 83 movc a,@a+pc 070F E0 movx a,@dptr 0710 F0 movx @dptr,a 0711 F2 movx @r0,a 0712 E3 movx a,@r1
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ
34
A10-A8 Op code
! relative addressing
here: sjmp here ;machine code=80FE(FE=-2) Range = (-128 ~ 127)
! Absolute addressing (limited in 2k current mem block)
0700 1 org 0700h 0700 E106 2 ajmp next ;next=706h 0702 00 3 nop 0703 00 4 nop 0704 00 5 nop 0705 00 6 nop 7 next: 8 end
Op code Relative address
A7-A0 07FEh
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ
35
! Long distance address
Range = (0000h ~ FFFFh)
0700 1 org 0700h 0700 020707 2 ajmp next ;next=0707h 0703 00 3 nop 0704 00 4 nop 0705 00 5 nop 0706 00 6 nop 7 next: 8 end
Op code A15-A8 A7-A0
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ• Ví$dụ$:$Hãy$viết$đoạn$chương$trình$chép$giá$trị$55h$vào$bộ$
nhớ$RAM$tại$các$địa$chỉ$từ$40h$đến$44h$sử$dụng:(a)$Định$địa$chỉ$trực$tiếp.(b)$Định$địa$chỉ$gián$tiếp$không$dùng$vòng$lặp.(c)$Định$địa$chỉ$gián$tiếp$có$dùng$vòng$lặp.
36
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ• Ví$dụ$:$Hãy$viết$đoạn$chương$trình$chép$giá$trị$55h$vào$bộ$
nhớ$RAM$tại$các$địa$chỉ$từ$40h$đến$44h$sử$dụng:(a)$Định$địa$chỉ$trực$tiếp.
37
MOV A, #55h ; load A with value 55hMOV 40h, A ! ; copy A to RAM location 40h MOV 41h, A ! ; copy A to RAM location 41h MOV 42h, A ! ; copy A to RAM location 42h MOV 43h, A ! ; copy A to RAM location 43h MOV 44h, A ! ; copy A to RAM location 44h
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ• Ví$dụ$:$Hãy$viết$đoạn$chương$trình$chép$giá$trị$55h$vào$bộ$
nhớ$RAM$tại$các$địa$chỉ$từ$40h$đến$44h$sử$dụng:(b)$Định$địa$chỉ$gián$tiếp$không$dùng$vòng$lặp.
38
MOV A, #55h ; load A with value 55hMOV R0, #40h ; load the pointer. R0 = 40hMOV @R0, A ; copy A to RAM location R0 points toINC R0 ; increment pointer. Now R0 = 41hMOV @R0, A ; copy A to RAM location R0 points to
Solution to Example (b)Solution to Example (b)register indirect addressing mode without a loop
MOV @R0, A ; copy A to RAM location R0 points toINC R0 ; increment pointer. Now R0 = 42hMOV @R0, A ; copy A to RAM location R0 points toINC R0 ; increment pointer. Now R0 = 43h MOV @R0, A ; copy A to RAM location R0 points toINC R0 ; increment pointer. Now R0 = 44hMOV @R0, A ; copy A to RAM location R0 points to
75
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ• Ví'dụ':'Hãy'viết'đoạn'chương'trình'chép'giá'trị'55h'vào'bộ'nhớ'RAM'tại'
các'địa'chỉ'từ'40h'đến'44h'sử'dụng:
(c)'Định'địa'chỉ'gián'tiếp'có'dùng'vòng'lặp.
39
MOV A, #55h ; A = 55hMOV R0, #40h ; load pointer. R0 = 40h, RAM add.MOV R2, #05 ; load counter, R2 = 5
AGAIN: MOV @R0, A ; copy 55A to RAM location R0 points toINC R0 ; increment R0 pointer
Solution to Example (c)Solution to Example (c) Loop method
INC R0 ; increment R0 pointerDJNZ R2, AGAIN ; loop until counter = zero
“DJNZ” : decrement and jump if Not Zero
DJNZ direct, relativeDJNZ Rn, relative where n = 0,1,,,7
MOV R2, #05h ; exampleLP:
;--------------------------------; do 5 times inside the loop;--------------------------------DJNZ R2, LP ; R2 as counter
76
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ• Ví$dụ$:$Hãy$viết$đoạn$chương$trình$xóa$15$ô$nhớ$RAM$bắt$
đầu$tại$địa$chỉ$60h.
40
Example (looping)Example (looping)Write a program segment to clear 15 RAM locations starting at RAM address 60h.
CLR A ; A = 0MOV R1, #60h ; load pointer. R1 = 60hMOV R7, #15 ; load counter, R7 = 15 (0F in HEX)MOV R7, #15 ; load counter, R7 = 15 (0F in HEX)
AGAIN: MOV @R1, A ; clear RAM location R1 points toINC R1 ; increment R1 pointerDJNZ R7, AGAIN ; loop until counter = zero
; clear one ram location at address 60hCLR AMOV R1,#60hMOV @R1,A
Setup a loop using DJNZ and register R7 as counter77
GV:'Trương'Công'Dung'Nghi
Các'kiểu'định'địa'chỉ• Ví$dụ$:$Hãy$viết$đoạn$chương$trình$sao$chép$một$khối$dữ$liệu$
10$bytes$từ$vùng$nhớ$RAM$có$địa$chỉ$bắt$đầu$là$35h$đến$vùng$nhớ$RAM$có$địa$chỉ$bắt$đầu$là$60h.
41
Example (block transfer)Example (block transfer)Write a program segment to copy a block of 10 bytes of data from RAM locations starting at 35h to RAM locations starting at 60h.
MOV R0, #35h ; source pointerMOV R1, #60h ; destination pointerMOV R3, #10 ; counterMOV R3, #10 ; counter
BACK:MOV A, @R0 ; get a byte from sourceMOV @R1, A ; copy it to destinationINC R0 ; increment source pointerINC R1 ; increment destination pointerDJNZ R3, BACK ; keep doing it for all ten bytes
78