Ch 03 8051 的組合語言與系統發展
3.1 Instruction Format Instruction content: opcode + operand Label: opcode destination operand , source oprand Example: MOV A, R1 Effect: (A) (R1) Source operand: R1 Destination operand: A Operand type: memory location, register, constant, Number of operands: NOP 1 INC A 2 MOV R1, A 3 CJNE A,30H,NEXT [Jump to NEXT if (A) ≠ (30H)]
Assembly language vs machine code MOV A, #60H “MOV” is a mnemonic, 給人看的。 74H 60H 74H is the opcode, 給 processsor 看的。
Label opcode operands comment ORG 043H ; Program starts from 0043H START: MOV DPTR,#TABLE MOV R0,#6 N_BYTE: MOV A,#0 MOVC A,@A+DPTR MOV P1,A CALL DELAY INC DPTR DJNZ R0,N_BYTE JMP START DELAY: MOV R7,#0 DL1: MOV R6,#0 DJNZ R6,$ DJNZ R7,DL1 RET TABLE: DB 00H,33H,66H,99H,0CCH,0FFH DS 3 DW 1234H,0ABCDH END
1 ;Label opcode operands comment 2 3 0043 ORG 043H ; Program starts from 0043H 4 0043 START: 5 0043 90 00 5E MOV DPTR,#TABLE 6 0046 78 06 MOV R0,#6 7 0048 N_BYTE: 8 0048 74 00 MOV A,#0 9 004A 93 MOVC A,@A+DPTR 10 004B F5 90 MOV P1,A 11 004D 12 00 55 CALL DELAY 12 0050 A3 INC DPTR 13 14 0051 D8 F5 DJNZ R0,N_BYTE 15 0053 01 43 JMP START 16 0055 DELAY: 17 0055 7F 00 MOV R7,#0 18 0057 DL1: 19 0057 7E 00 MOV R6,#0 20 0059 DE FE DJNZ R6,$ 21 005B DF FA DJNZ R7,DL1 22 005D 22 RET 23 24 005E TABLE: 25 005E 00 33 66 99 CC DB 00H,33H,66H,99H,0CCH,0FFH 0063 FF 26 0064 DS 3 27 0067 1234 ABCD DW 1234H,0ABCDH 28 006B END
3.2 Addressing Modes Example: 中壢市中大路300號 中央大學 Zip code = 32001
Addressing Modes Immediate data: MOV A, #60H (A) #60H #: constant “Immediate” means the data is in the instruction and can be accessed by the CPU immediately. The processor: Reads the opcode into the CPU Acts according to the opcode
Addressing Modes Direct addressing: MOV A,60H [ (A) (60H) ] 60H = memory address E5H opcode 60H Address of data A CPU “Direct” means the data address is contained in the instruction and the CPU can access the data directly with the data address.
Addressing Modes Register addressing: MOV A,R3 [ (A) (R3) ] MOV A,13H [ (A) (13H) ] R3 = register name 13H = register R3’s address (when RS1=1, RS0=0) (Register addressing 是direct addressing的一種) R2 R3 R4 12H 13H 14H
Address of R0 ~ R7 RS1, RS0 0, 0 0, 1 1, 0 1, 1 Register bank, selected RB0 RB1 RB2 RB3 R0 00H 08H 10H 18H R1 01H 09H 11H 19H R2 02H 0AH 12H 1AH R3 03H 0BH 13H 1BH R4 04H 0CH 14H 1CH R5 05H 0DH 15H 1DH R6 06H 0EH 16H 1EH R7 07H 0FH 17H 1FH PSW (Program Status Word) CY AC F0 RS1 RS0 OV P
Addressing Modes Indirect addressing: MOV A,@R0 @: pointer @R0 = an address whose value is stored at R0 Possible pointers: @R0, @R1, @SP, @DPTR MOV DPTR, #1234H MOV A, @DPTR MOV SP,#30H PUSH 60H 5FH 28H 60H 3FH Data to be accessed 61H 41H R0 60H (3) (2) E6H opcode (1) A “Indirect” means the data address is not contained in the instruction CPU
Addressing Modes 30H 31H SP before & after pushing Indirect addressing: MOV A,@R0 @: pointer @R0 = an address whose value is stored at R0 Possible pointers: @R0, @R1, @SP, @DPTR 類似: 托人帶來支票 MOV DPTR, #1234H MOV A, @DPTR MOV SP,#30H PUSH 60H 30H 25H Top of stack before pushing 31H 38H Top of stack after pushing 60H Data to be pushed (3) 75H Opcode of mov SP,#30H 81H 30H C0H Opcode of push 60H 60H “Indirect” means the data address is not contained in the instruction (1) A (2) CPU
Addressing Modes Index addressing (只能用於程式記憶體) MOVC A , @A + DPTR Possible base registers: DPTR, PC Possible index register: A
Addressing Modes 定址法 資料所在 Immediate 程式記憶體 Direct 資料記憶體 Register Indirect Index
(SP) = the content of SP ((SP)) = the content of (SP) 30H SP 2FH 1AH 30H 85H 31H 69H
3-3 8051 指令集 49 one-operand 111 instructions 46 two-operand 3-3 8051 指令集 49 one-operand 111 instructions 46 two-operand 16 three-operand
Instructions that can modify flags
3.4 Program Development 標記 Label 運算碼Opcode 運算元Operand(s) 註解 Comment 32 letters max. Label 加 : Label 不加 (定義data時); 被組譯成記憶體的位址值。 Opcode A mnemonic Operand Comment ;(分號) 之後的文字,給人看,組譯器不看。
5_1_1.asm Pseudo instruction,指示 assembler 的動作。 ORG 000H MOV A,#10000000B NEXT: MOV P2,A CALL DELAY RR A JMP NEXT ;============================== ; DELAY 0.1S DELAY: MOV R6,#200 DL1: MOV R7,#249 DJNZ R7,$ DJNZ R6,DL1 RET END Instruction, 給 processor 執行的。 #10000000B 可寫成 #80H或 #128 A = 10000000B, 01000000B, 00100000B, 00010000B … Pseudo instruction,指示 assembler 不必組譯”END”之後的文字。
MOV A,1H MOV A,2H MOV A,3H . MOV A,99H END “END” 可以讓組譯程式停止組譯 “END”之後的程式。
Number Systems Number System Symbol Example Binary B Octal O Decimal D, (Default) Hex H ASCII ‘ ’ or “ ”
虛擬指令 給組譯程式看的,指示如何組譯。 不是給 processor 執行的。 ORG n END EQU To define a constant. e.g. LF EQU 0AH DB DW DS n REG
End of Lecture, 20091007
進入 DOS 視窗 打開 DOS 視窗的步驟: 所有程式 附屬應用程式 命令提示字元
DOS Commands Command >cd Change directory >dir Show directory
1 ;Label opcode operands comment 2 3 0043 ORG 043H ; Program starts from 0043H 4 0043 START: 5 0043 90 00 5E MOV DPTR,#TABLE 6 0046 78 06 MOV R0,#6 7 0048 N_BYTE: 8 0048 74 00 MOV A,#0 9 004A 93 MOVC A,@A+DPTR 10 004B F5 90 MOV P1,A 11 004D 12 00 55 CALL DELAY 12 0050 A3 INC DPTR 13 14 0051 D8 F5 DJNZ R0,N_BYTE 15 0053 01 43 JMP START 16 0055 DELAY: 17 0055 7F 00 MOV R7,#0 18 0057 DL1: 19 0057 7E 00 MOV R6,#0 20 0059 DE FE DJNZ R6,$ 21 005B DF FA DJNZ R7,DL1 22 005D 22 RET 23 24 005E TABLE: 25 005E 00 33 66 99 CC DB 00H,33H,66H,99H,0CCH,0FFH 0063 FF 26 0064 DS 3 27 0067 1234 ABCD DW 1234H,0ABCDH 28 006B END Thu Oct 8 2009 09:23 Page 2 Why, jumping to F5H? Ans: PC = 0053H Destination = N_BYTE = 0048H 0048H -0053H ---------------------- FFF5H = F5H (以後再詳述。)
:10 0043 00 90005E7806740093F590120055A3D8F5 DE :10 0053 00 01437F007E00DEFEDFFA2200336699CC 87 :01 0063 00 FF 9D :04 0067 00 1234ABCD D7 :00 0000 01 FF
Format of Intel HEX file
3-5 系統發展的流程 圖3-5-1 (a) 非 ISP 之8051系統 將程式燒錄到 8051 較麻煩與耗時;所以最好先用ICE來驗證程式已經是正確的,才進行燒錄到 8051 的動作。 圖3-5-1 (b) 具 ISP 之8051系統 將程式燒錄到 8051 很簡易與快速,所以直接將程式燒錄到 8051以進行驗證;經過多次燒錄、除錯、修改以確定程式完全正確。 ICE (In-Circuit Emulator): price > 10000 NT$ Emulation 用另一套硬體去驗證目標硬體。 Simulation 用軟體模擬去驗證目標硬體。