Académique Documents
Professionnel Documents
Culture Documents
HCM
L Ch Thng www.tinyurl.com/thongchile
Instruction Set
255 instructions, mnemonic code 1-3 byte instructions Opcode(operation code) + 0,1,2 bytes address, data 1-byte instructions 139 2-byte instructions 92 3-byte instructions 24
Ref. I. Scott Mackenzie L Ch Thng 2
L Ch Thng www.tinyurl.com/thongchile
FIGURE B1
Opcode map
MOV
L Ch Thng
Coding format
Opcode (operation code) + operands
L Ch Thng www.tinyurl.com/thongchile
FIGURE 31 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
L Ch Thng
Addressing Modes
Register Addressing
L Ch Thng
L Ch Thng www.tinyurl.com/thongchile
Register banks o Bank 0 (default), Bank 1, Bank 2, and Bank 3 o Change register bank by selecting bits RS1 and RS0 the program status word (discussed next slide) o One bank includes 8 registers: R0 through R7 Ex: Read the contents of location 05H into the accumulator MOV A, R5 or MOV A, 05H Write the contents of the accumulator into location 00H MOV R0, A
Ref. I. Scott Mackenzie L Ch Thng 7
L Ch Thng
L Ch Thng www.tinyurl.com/thongchile
Addressing Modes
Register Addressing - 8 registers: R0 ~ R7 Ex: MOV A,R6 ADD A,R7 - 4 banks of registers: Bank 0 (default), Bank 1, Bank 2, and Bank 3 - The active bank is determined by bit PSW.3 & PSW.4 (reviewed in the next 2 slides) Ex: MOV PSW,#00011000B; activates bank 3 Ex: INC DPTR
L Ch Thng
Addressing Modes
Direct Addressing
L Ch Thng
10
L Ch Thng www.tinyurl.com/thongchile
DPTR
L Ch Thng
11
Addressing Modes
Direct Addressing - Access internal memory Ex: MOV A,30H ADD A,31H - Access special function registers: can use the mnemonic abbreviation instead of the addresses Ex: MOV P1,A; Accumulator Port 1 MOV 90H,A; same Opcode: F5 90 Ex: MOV R1,ACC MOV R1,0E0H MOV R1,A Same function, but 0000 A9E0 1 MOV R1,ACC
Ref. I. Scott Mackenzie
2 3
12
L Ch Thng www.tinyurl.com/thongchile
Addressing Modes
Immediate Addressing - Source operand is constant - Use # sign Ex: MOV A,#12 MOV A,#0CH MOV R1,#0 ADD A,#11110001B MOV DPTR,#2000H
L Ch Thng
13
Addressing Modes
Indirect Addressing - Address of a memory location is stored in R0 or R1 - Use @sign to access the content of this memory location: @R0, @R1 Ex: MOV R0,#30H MOV A,@R0 Ex: Implementation of array MOV R0, #60H LOOP: MOV @R0, #0 INC R0 CJNE R0, #80H, LOOP
L Ch Thng
14
L Ch Thng www.tinyurl.com/thongchile
Addressing Modes
Relative Addressing - Used with 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 - 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 Thng
15
Addressing Modes
Relative Addressing
FIGURE 32 Calculating the offset for relative addressing. (a) Short jump ahead in memory (b) Short jump back in memory
Ref. I. Scott Mackenzie L Ch Thng 16
L Ch Thng www.tinyurl.com/thongchile
Addressing Modes
Absolute Addressing
FIGURE 33 Instruction encoding for absolute addressing. (a) Memory map showing 2K pages (b) 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
Ref. I. Scott Mackenzie L Ch Thng 17
Addressing Modes
Long Addressing - Used with LCALL and LJMP instruction - Use full 16-bit address - Usually used with label Ex: LCALL SUBPROGRAM1 LJMP LABEL2 Indexed Addressing - Base address (PC or DPTR) + Offset (A) - Used with JMP or MOVC Ex: MOVC A, @A+DPTR JMP @A+DPTR
Effective Address
L Ch Thng
18
L Ch Thng www.tinyurl.com/thongchile
Instruction Types
Data transfer Arithmetic Logical Boolean variable Program branching
L Ch Thng
19
L Ch Thng
20
10
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
21
L Ch Thng
22
11
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
23
L Ch Thng
24
12
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
25
L Ch Thng
26
13
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
27
14
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
29
L Ch Thng
30
15
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
31
L Ch Thng
32
16
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
33
L Ch Thng
34
17
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
35
L Ch Thng
36
18
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
37
L Ch Thng
38
19
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
39
L Ch Thng
40
20
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
41
Arithmetic
Status in PSW : CY (carry/borrow), PSW.7 OV (overflow), PSW.2 AC (Auxiliary carry), PSW.6 Flag 0 (zero), PSW.5 P (Even parity), PSW.0 RS1,0 PSW.4,3 CY for unsigned integer OV for signed integer, (+)+(+)(-), (-)+(-)(+) ADDC, SUBB multi-byte operations DA (decimal adjustment) for BCD
L Ch Thng
42
21
L Ch Thng www.tinyurl.com/thongchile
; MUL/DIV MUL AB DIV AB SUBB A, R6 ; BCD addition MOV A, 43H ADD A, 41H DA A MOV 41H, A MOV A, 42H ADD A, 40H DA A MOV 40H, A
;PSW status of A
L Ch Thng
44
22
L Ch Thng www.tinyurl.com/thongchile
Logic
ANL, ORL, XRL CLR A, CPL A (complement) RL, RLC RR, RRC SWAP
MOV LOOP: RLC XCH RRC XCH DJNZ XCH
Ref. I. Scott Mackenzie
R7, #8 A A, 0F0H ; register B : 0F0H A A, 0F0H R7, LOOP; dec/jump, R7- counter A, 0F0H
L Ch Thng 45
Data transfer
MOV (internal memory), MOVX (external memory) XCH A, <source> XCHD A,<source> ; exchange digits (lower nibble) PUSH, POP PUSH : sp++; @sp data POP : Ri @sp ; sp--; sp (stack pointer) : 08H-7FH if sp>7FH; no operation
L Ch Thng
46
23
L Ch Thng www.tinyurl.com/thongchile
; LOOK-UP TABLE, TRICKED; usually PC can ; not be accessed directly MOV A, #ENTRY_NUMBER; n+size (RET) CALL LOOK_UP . ; subroutine LOOK_UP: MOVC A, @A+PC ; move constant RET TABLE: ; define byte-data (array for highlevel language) DB data1, data2, data3,
L Ch Thng
47
Time-space consideration ; TABLE LOOKUP - 13 bytes, 5 clocks SQUARE: INC A ; sizeof (RET), 1 clocks MOVC A, @A+PC ; move constant, 2 clocks RET ; 2 clocks TABLE: ; define data (array for high-level language DB 0,1,4,9,16, 25, 36, 49, 64, 81 ; Use multiplication - 8 bytes, 13 clocks SQUARE: PUSH 0F0H ; register B <bit operand>, LABEL, 2 clocks MOV 0F0H, A ; 1 clocks MUL AB ; 4 cycles POP 0F0H ; 2 clocks RET ; 2 clocks
L Ch Thng
48
24
L Ch Thng www.tinyurl.com/thongchile
Boolean
Bit operand, address 0-127
SETB <bit operand> CLR <bit operand> MOV <bit operand>, <bit operand> CLR C (single instruction) compare with CLR CY JC LABEL (jump if CY=1) JB <bit operand>, LABEL (jump if bit==1) JBC <bit operand>, LABEL (jump if bit==1 and clear bit) JNC LABEL JNB <bit operand>, LABEL
L Ch Thng
49
FIGURE 34
L Ch Thng
50
25
L Ch Thng www.tinyurl.com/thongchile
FIGURE 35
L Ch Thng
51
Program branching
SJMP : 8-bit offset LJMP : 11-bit address (2KB segment) AJMP : 16-bit LCALL, ACALL
; index*2-byte
L Ch Thng
52
26
L Ch Thng www.tinyurl.com/thongchile
Example : ; DJNZ : Dec and jump non-zero ; use as counter MOV R7, #10 LOOP: DJNZ R7, LOOP ; CJNZ : compare and jump non-zero CJNZ A, #03H, SKIP SJMP TERMINATE SKIP:
; 03H : control-C
L Ch Thng
53
Example : bit-wise XOR XRB: MOV ANL MOV MOV ANL ORL
Other ways (1) Use byte-wise XOR (2) Use JB, JNB
L Ch Thng
54
27
L Ch Thng www.tinyurl.com/thongchile
FIGURE 38
L Ch Thng
55
; dest = source
;A=72H ;R4=62H ;B=the content of F9th byte of RAM
Note 1:
MOV A,#72H MOV A,72H After instruction MOV A,72H the content of 72th byte of RAM will replace in Accumulator.
Note 2:
MOV A,R3
Ref. I. Scott Mackenzie
MOV A,3
L Ch Thng 56
28
L Ch Thng www.tinyurl.com/thongchile
ADDA, Source
ADDA,#6 ADDA,R6 ADD ADD A,6 A,0F3H ;A=A+6
;A=A+SOURCE
SUBB
SUBB SUBB
A, Source
A,#6 A,R6 ;A=A-6
;A=A-SOURCE-C
;A=A+R6
L Ch Thng
57
58
29
L Ch Thng www.tinyurl.com/thongchile
; bit=1 ; bit=0
; CY=1 ;bit 0 from port 0 =1 ;bit 7 from port 3 =1 ;bit 2 from ACCUMULATOR =1 ;set high D5 of RAM loc. 20h
CLR instruction is similar to SETB i.e.: CLR C ;CY=0 But following instruction is only for CLR: CLR A ;A=0
Ref. I. Scott Mackenzie L Ch Thng 59
DEC INC
INC DEC DEC
byte byte
R7 A 40H
;byte=byte-1 ;byte=byte+1
; [40]=[40]-1
L Ch Thng
60
30
L Ch Thng www.tinyurl.com/thongchile
C
61
CPL
;1s complement
A,#55H ;A=01010101 B A P1,A DELAY L01
L Ch Thng 62
31
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
63
SP=08H
SP=09H
SP=08H
L Ch Thng
64
32
L Ch Thng www.tinyurl.com/thongchile
L Ch Thng
65
DJNZ:
Write a program to clear ACC, then add 3 to the accumulator ten time Solution: MOV MOV ADD DJNZ MOV A,#0 R2,#10 A,#03 R2,AGAIN ;repeat until R2=0 (10 times) R5,A
AGAIN:
L Ch Thng
66
33
L Ch Thng www.tinyurl.com/thongchile
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 location within the 2k block of program memory. SJMP(short jump) In this 2-byte instruction. The relative address range of 00FFH is divided into forward and backward jumps, that is , within -128 to +127 bytes of memory relative to the address of the current PC.
L Ch Thng
67
CALL Instructions
Another control transfer instruction is the CALL instruction, which is used to call a subroutine.
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 the subroutine must be within 2K byte range.
Ref. I. Scott Mackenzie L Ch Thng 68
34
L Ch Thng www.tinyurl.com/thongchile
Example: Write a program to copy a block of 10 bytes from RAM location starting at 37h to RAM location starting at 59h. Solution: MOV R0,#37h MOV R1,#59h MOV R2,#10 L1: MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R2,L1
Ref. I. Scott Mackenzie
L Ch Thng
69
1.Add the low bytes R7 and R5, leave the answer in R3. 2.Add the high bytes R6 and R4, adding any carry from step 1, and leave the answer in R2. 3.Put any carry from step 2 in the final byte, R1.
L Ch Thng
70
35
L Ch Thng www.tinyurl.com/thongchile
Steps 1, 2, 3
MOV A,R7 ;Move the low-byte into the accumulator ADD A,R5 ;Add the second low-byte to the accumulator MOV R3,A ;Move the answer to the low-byte of the result
MOV A,R6 ;Move the high-byte into the accumulator ADDC A,R4 ;Add the second high-byte to the accumulator, plus carry. MOV R2,A ;Move the answer to the high-byte of the result MOV A,#00h ;By default, the highest byte will be zero. ADDC A,#00h ;Add zero, plus carry from step 2. MOV R1,A ;Move the answer to the highest byte of the result
Ref. I. Scott Mackenzie L Ch Thng 71
References
I. Scott Mackenzie, The 8051 Microcontroller Cc ti liu trn Internet khng trch dn hoc khng ghi tc gi
L Ch Thng
72
36