Chapter 4 8051 Assembly Language Programming 1 4
Chapter 4 8051 Assembly Language Programming 1
4 8051 Assembly Language Programming 4. 1 Pseudo-instructions 4. 2 Programming steps 4. 3 Program types 2
Flow-process diagram of developing microcontroller system Programming: MOV R 2, #20 FF 2: MOV R 1, #50 FF 1: MOV R 0, #230 DJNZ R 0, $ DJNZ R 1, FF 1 DJNZ R 2, FF 2 programming Compile into machine code: 101001011000010111 11111010100101 011010110110 010110111………… burning software operate hardware Computer burner PCB
§ 4. 1 Pseudo-instructions § A given Assembly language program is a series of statements, or lines. It includes: Ø Assembly language instructions ¡Tell the CPU what to do Ø Directives (or pseudo-instructions) ¡Give directions to the assembler assembly language → machine code MOV A, R 1 → 11101001 B ¡pseudo-instructions generate no machine codes 4
1. ORG Specify the start address of following instruction. When they’re powered up, the first code should be at ROM address 0000 H We achieve this by the ORG statement in the source program for example: ORG 0000 H Ø AJMP MAIN ORG 0030 H Ø MAIN: MOV A, R 1 Ø ADD A, R 2 Ø …… ROM PC 0000 H 0001 H … 0030 H 0031 H … Machine codes 00000001 B(01 H) 01010000 B(50 H) … 11101001 B(E 1 H) 00101010 B(2 AH) … 5
2. END When compiling the source program, the assembly process will be ended, and the following instructions will no longer be compiled. for example: MOV LOOP: MOV END JZ XRL INC MOV INC DJNZ Here: R 7, R 0, A, #08 H #31 H @R 0 debugging program Here A, #01111 B A @R 0, A R 0 R 7, LOOP SJMP Here 6
3. DB Define the constant(Byte) table 4. DW Define the constant(Word) table for example: ORG 1000 H MOV A, R 1 …… ORG 2000 H TAB 1: DB 01 H, 04 H, 09 H, … TAB 2: ORG 3000 H DW 01 H, 0409 H, … 0001 H ROM PC 1000 H … 2000 H 2001 H 2002 H … 3000 H 3001 H 3002 H 3003 H … Machine codes (constant table) 11101001 B(E 1 H) … 00000001(01 H) 00000100(04 H) 00001001(09 H) … 0000(00 H) 00000001(01 H) 00000100(04 H) 00001001(09 H) … 7
5. BIT Define bit address for example: sign BIT 27 H big BIT 20 H MOV JB C, big sign, LOOP sign big 8
6. EQU Define byte or word address for example: average EQU 50 H TAB 1 EQU 3050 H MOV A, MOV DPTR, #TAB 1 #average ; (A)=50 H ; (DPTR)=3050 H 9
§ 4. 2 Programming steps § 1. First to design the hardware circuit according to the goal § 2. Then to write the program according to the designed hardware § Steps: 1. Analysis problem 2. Determine algorithm 3. Draw program flow chart 4. Programming 5. Debug program until success 10
§ 4. 3 Program types 1. Programming rules: ① simple ② Less memory ③ Short execution time 2. Program types: ① Sequence structure ② Branches structure ③ Loop structure ④ look - up table programming ⑤ Subroutine structure 11
① Sequence structure programming no jump, go on normally, continuous For example: P 60 4 -1 begin C←X0 0 BH←C C←X1 0 AH←C C←X2 09 H←C C←X3 08 H←C A← 00001111 B 0 0 X 0 X 1 X 2 X 3 A AND 21 H C X 7 X 6 X 5 X 4 X 3 X 2 X 1 X 0 21 H ← A end 12
0 0 X 0 X 1 X 2 X 3 C X 7 X 6 X 5 X 4 X 3 X 2 X 1 X 0 Write the program: MOV C, 00 H MOV 0 BH, C MOV C, 01 H MOV 0 AH, C MOV C, 02 H MOV 09 H, C MOV C, 03 H MOV 08 H, C MOV A, #0 FH(00001111 B) ANL A, 21 H MOV 21 H, A begin C←X0 0 BH←C C←X1 0 AH←C C←X2 09 H←C C←X3 08 H←C A← 00001111 B A AND 21 H ← A end 13
② Branches structure conditional jump, discontinuous for example: P 60 4 -2 sign R 1 R 0 R 3 R 2 begin no( Branch 1) the sign (R 1. 7)=1? yes( Branch 2) ROM PC Machine codes … … the sign (R 1. 7)=1? … Branch 1 positive … … Branch 2 negative … … end … … send to R 2 R 3 complement the low byte add 1 send to R 2 complement the high byte add the carry bit keep the sign send to R 3 end 14
for example: P 61 4 -3 External RAM begin DPTR … 2000 H 2001 H 2002 H … … DATA 1 DATA 2 DATA 3 … R 3 ←DATA 1 A←DATA 2 no DATA 1>DATA 2 A-R 3>0 ? Yes DATA 2>DATA 1 DATA 3←R 3 DATA 3←A end 15
③ Loop structure Decrement Jump Instructions DJNZ Rn, rel Decrement= change loop variable ; Rn – 1 if Rn ≠ 0, then PC + 2 + rel if Rn = 0, then PC + 2 DJNZ direct, rel ; (direct)- 1 if (direct) ≠ 0, then PC + 3 + rel if (direct) = 0, then PC + 3 16 Return
③ Loop structure for example: P 61 4 -4 begin R5 ←variable 2 of loop 2 R4 ←variable 1 of loop 1 loop 2 loop 1 yes NOP R4-1 ≠ 0? no yes R5-1 ≠ 0? no end 17
③ Loop structure for example: P 61 4 -4 begin R5 ←variable 2 of loop 2 R4 ←variable 1 of loop 1 loop 2 loop 1 yes NOP R4-1 ≠ 0? no yes R5-1 ≠ 0? no end 18
Exercise: If you have two four-byte hexadecimal data, stored in 20 H and 30 H as the starting address. Calculate the sum of them, and placed in 30 H as the starting address (first stored lower byte). Write the program: MOV R 0, #20 H MOV R 1 #30 H MOV R 2, #04 H CLR C JIA 2: MOV A, @R 1 ADDC A, @R 0 MOV @R 0, A INC R 0 INC R 1 DJNZ R 2, JIA 2 End loop begin get the first data add another data save the sum change the address yes R 2 -1 ≠ 0? no End 19
③ Loop structure for example: P 62 4 -5 begin R 0←address of the first data R 7←number of the data loop variable 8 data are in 20 H as start address, try to find the maximum and send it to 2 AH. A← the first data 2 AH←the next data (A)>=(2 AH)? no, (A)<(2 AH) exchange yes no finish Loop? yes (2 BH)←(A) end 20
④ look - up table programming Data Transfer from Program Memory MOVC A, @A+DPTR MOVC A, @A+PC • for reading look-up table in Program Memory For example: if (A)=01 H 2000 HBA: INC A 2001 MOVC A, @A+PC 2002 RET 2003 2004 2005 … DB 30 H DB 31 H DB 32 H … ; (A)=02 H ; (A)=? PC 2000 H 2001 H 2002 H 2003 H 2004 H 2005 H …… ROM 04 H 83 H 22 H 30 H 31 H 32 H …… (INC machine code) (MOVC machine code) (RET machine code) (table data) 21
Indexed Addressing Jump Instructions JMP @A+DPTR multiple-branch: …… JMP (A)=0, DPTR: …… (A)=1 …… (A)=2 …… (A)=FFH …… (A)=00 H~FFH (0~255) 256 branches 22 Return
for example: P 63 4 -7 begin Save the table DPTR ←the start address of the table get the lower 8 -bit address of key get the higher 8 -bit address of key DPTR← 16 -bit address jump to key program end 23
4 -7: if (A)=(key 1 code)=1 PC ORG 1000 H … 1000 H: 1001 H: 1002 H: 1003 H: 1004 H: 1005 H: … 2000 H: … 00 H(K 0 L) 20 H(K 0 H) 35 H(K 1 L) 20 H(K 1 H) 40 H(K 2 L) 21 H(K 2 H) … Key 0 program … 2035 H: Key 1 program … 2140 H: Key 2 program … DB 00 H, 20 H, 35 H, 20 H, 40 H, 21 H MOV DPTR, RL A MOV R 2, MOVC A, @A+DPTR ; (A )=( (A)+(DPTR))=(1002 H)=35 H PUSH Acc ; ((SP))=(A)=35 H MOV A, INC A ; (A)=(A)+1=03 H MOVC A, @A+DPTR ; (A )=( (A)+(DPTR))=(1003 H)=20 H MOV DPH, ; (DPH)=(A)=20 H POP DPL ; (DPL)=((SP))=35 H CLR A ; (A)=0 JMP @A+DPTR ; jump to 2035 H, start address of key 1 program #1000 H ; (DPTR)=1000 H ; (A)=00000001 → 00000010=02 H A R 2 A ; (R 2)=(A)=02 H ; (A)=(R 2)=02 H ROM 24
⑤ Subroutine structure for example: P 65 4 -9 Main program begin R 0← 20 H R 2← 3 Call subroutine R 0← 30 H R 2← 5 Call subroutine begin clear change the address R 2 - 1=0? no yes return R 0← 40 H R 2← 6 Call subroutine end 25
⑤ Subroutine structure for example: P 65 4 -10 subroutine Main program begin R 0← the start address of the first data R 1← the start address of another data get the first data add another data R 2←number of bytes save the sum Call subroutine change the address end yes R 2 -1 ≠ 0? no return 26
- Slides: 26