EEE3410 Microcontroller Applications LABORATORY Experiment 2 Assembly Language programming (2) Name Class Date Class No. Marks Arithmetic, Logic and Jump instructions Objectives To learn and practice the following basic types of 8051 instructions: - Arithmetic instructions - Logical instructions - Jump instructions To practice the use of 8051 simulation and debugging software in program design Equipment/Components Required Personal computer with Windows XP operating system installed MedWin 8051 simulation software Ver Author(s) Date Remark 1.0 KK SIN 9/2006 Laboratory Experiment 2 1
A. Background Teaching lecturer should go through the topics on Data Movement instructions and Arithmetic and Logic operation instructions with students before carrying out the following laboratory exercise. B. Programming exercise B.1 Arithmetic instructions B.1.1 Use the 8051 simulation software, create and carry out simulated execution of the assembly program in table B.1.1. Table B.1.1 1. ORG 0H 2. MOV DPTR, #040H ; load pointer 3. MOV R2, #3 ; load counter 4. MOV R1, #0H ; set R1 equal to sum 5. AGAIN: CLR A ; A = 0 6. MOVC A, @A+DPTR ; load data to A 7. ADDC A, R1 ; add data to sum 8. MOV R1, A ; store result to sum 9. INC DPTR ; increment pointer 10. DJNZ R2, AGAIN ; repeat until R2 is zero 11. MOV B, #03H ; load B 12. DIV AB ; divide A by B 13. ADD A, R1 ; add A to sum 14. LOOP: SJMP LOOP ; endless loop 15. ORG 40H ; start address of Data 16. DB ABC ; content of data 17. END (1)Execute the program steps by steps until end. Note the change of values in the registers by opening suitable monitoring windows, e.g. the Register window, SFR window, etc. (2) Write down the contents of the following at the end of execution Register Content Register Content A 08H R1 C6H B 00H R2 00H DPTR 0043H CY flag 1 B.1.2 Use the 8051 simulation software, create and carry out simulated execution of the assembly program of BCD calculation in table B.1.2. 2 Experiment 2 Laboratory
Table B.1.2 EEE3410 Microcontroller Applications 1. ORG 0H 2. CLR C ; clear Carry Flag 3. CLR PSW.6 ; clear Auxiliary Carry Flag 4. MOV 40h, #71H ; Initialization of data 5. MOV 41h, #11H ; 6. MOV 42h, #65H ; 7. MOV 43h, #59H ; 8. MOV R0, #40H ; load pointer 9. MOV R1, #3 10. MOV R2, #4 ; load counter 11 CLR A ; A = 0 12. MOV R7, A ;clear R7 13. AGAIN: ADD A, @R0 ; add data pointed by pointer 14. MOV @R1, A 15. DA A ; adjust for BCD 16. JNC NEXT ; if CY=0 don t accumulate carry 17. INC R7 ; keep track of carries 18. NEXT: INC R0 ; increment pointer 19. INC R1 20. DJNZ R2, AGAIN ; repeat until R2 is zero 21. LOOP: SJMP LOOP ; endless loop 22. END a) Execute the program steps by steps until end. Note the change of values in the registers. b) Write down the contents of the following at the end of execution Register Content Register Content A 06H R4 82H R0 44H R5 E7H R2 00H R6 A0H R3 71H R7 02H c) What are the registers storing the BCD result of calculation and what is the result? R7 & A, BCD result after addition of the 4 BCD number = 206 d) Write down the values of the CY, OV & AC flags after the program line 13 is being executed at each time. Program line 13 being executed at CY flag OV flag AC flag 1 st time 0 0 0 2 nd time 0 1 0 3 rd time 0 0 0 4 th time 0 1 1 Laboratory Experiment 2 3
B.2 Logic instructions B.2.1 To perform the logic AND operation on two hexadecimal numbers 18h and 5Bh and keep the result in the Accumulator A. (1) Use the 8051 simulation software, create and carry out simulated execution of the assembly program in table B.2.1 (2) To record the result in Accumulator A after the logic AND operation: A = 18H or 0001 1000 B Table B.2.1 1. ORG 0030H 2. MOV A, #18H ;load A with the value 18h 3. ANL A, #5BH ;AND A with the value 5Bh 4. LOOP: SJMP LOOP ;endless loop 5. END ; B.2.2 To perform the logic OR operation on two hexadecimal numbers 18h and 5Bh and keep the result in the Accumulator A. (1) Use the 8051 simulation software, create and carry out simulated execution of the assembly program in table B.2.2 (2) To record the result in Accumulator A after the logic OR operation: A = 5BH or 0101 1011 B Table B.2.2 1. ORG 0030H 2. MOV A, #18H ;load A with the value 18h 3. ORL A, #5BH ;AND A with the value 5Bh 4. LOOP: SJMP LOOP ;endless loop 5. END ; B.2.3 Perform the logic Exclusive OR (XOR) operation on two hexadecimal numbers 18h and 5Bh, and keep the result in the Accumulator A. (1) Use the 8051 simulation software, create and carry out simulated execution of the assembly program in table B.2.3 (2) To record the result in Accumulator A after the logic XOR operation: A = 43H or 0100 0011 B 4 Experiment 2 Laboratory
EEE3410 Microcontroller Applications Table B.2.3 1. ORG 0030h 2. MOV A, #18h ;load A with the value 18h 3. XRL A, #5Bh ;AND A with the value 5Bh 4. LOOP: SJMP LOOP ;endless loop 5. END ; Note: The operations ANL (logic AND), ORL (logic OR) and XRL (logic Exclusive OR) may take the addressing modes of either Immediate, Direct, Indirect or Register Addressing, the choice of which addressing mode is chosen depends on individual applications. B.2.4 To perform the compare and jump operation (instruction: CJNE & DJNZ) (1) The program in table B.2.4 is to monitor Port 1 (P1) continuously for the value 63H. It should get out of the monitoring only if P1=63H. (2) Use the 8051 simulation software, create and carry out simulated execution of the assembly program in table B.2.4 Table B.2.4 1. ORG 0030H 2. MOV P1, #0FFH ; make P1 as an input port 3. HERE: MOV A, P1 ; get P1 4. CJNE A, #63H, HERE ; keep monitoring unless P1=63H 5. MOV R2, #13H 6. BACK: MOV P2, A 7. CPL A 8. DJNZ R2, BACK 9. LOOP: SJMP LOOP ; endless loop 10. END ; (3) During the simulated execution, use command Peripherals Ports to display Port Windows. By locating the mouse on any port bit, you can toggle the bit status by clicking the left hand button (Bit with white = LOW, bit with colour = HIGH). Use mouse to set the value of Port 1 to 63H. Observe the result. Value of Port 2 will toggle between 0110 0011 and 1001 1100 (4) How many times are the lines from 6 to 8 being executed? 19 times (5) What is the value of A at the end of execution? 9CH Laboratory Experiment 2 5
C. Exercise and Discussion 1. What is the stack pointer (SP) inside the CPU? What is the size of the SP register? SP is a 8-bit register used to hold an internal RAM address that is called the top of the stack. It is the last byte of data that was stored by a stack operation. 2. What is the instruction CJNE and DJNZ stand for? CJNE - Compare the source and destination operands first and then jump to the relative address if they are not equal DJNZ - Decrease operand by 1 first and then jump to the relative address if the result becomes zero 3. Find the content of register A after the following code in each case. a) MOV A, #37h ANL A, #0CAh Accumulator A : 02H b) MOV A, #37h ORL A, #0CAh Accumulator A : FFH c) MOV A, #37h XRL A, #0CAh Accumulator A : FDH 4. To mask certain bits of the accumulator we must ANL it with 0. 5. To set certain bits of the accumulator to 1 we must ORL it with 1. 6. XRL an operand with itself results in 0. 7. What value must R4 have in order for the following instruction not to jump? CJNE R4, #53, OVER R4 = 53 8. Find contents of register A after execution of the following code. CLR A ORL A, #99h CPL A Accumulator A : 66H 9. What is the value of register A after each of the following instruction? MOV A, #25h 25H RR A 92H RR A 49H RR A A4H RR A 52H Accumulator A : 10. What is the value of register A after each of the following instruction? MOV A, #A2h A2H RL A 45H RL A 8AH RL A 15H RL 2AH Accumulator A : 6 Experiment 2 Laboratory
11. What is the value of register A after each of the following instruction? CLR A 0H SETB C 0H RRC A 80H SETB C 80H RRC C0H Accumulator A : 12. What is the value of register A after each of the following instruction? MOV A, #85h 85H SWAP A 58H ANL A, #0F0h 50H Accumulator A : EEE3410 Microcontroller Applications 13. Assume the P1 is an input port connected to a temperature sensor. Write a 8051 program to read the temperature ns test it for the value 80. According to the test results, place the temperature value in to the registers indicated by the following. If T = 80 then A = 80 If T < 80 then R1 = T If T > 80 then R2 = T ORG 00H MOV P1,#0FFH ;set P1 as input port CLR A ;clear A MOV R1,A ;clear R1 MOV R2,A ;clear R2 MOV A,P1 ;input T from P1 to A CJNE A,#80,NOT_EQ ;jump if T 80 SJMP STOP ;end if T=80 NOT_EQ: JC LOW ;jump if T<80 HIGH: MOV R2,A ;save T to R2 CLR A ;clear A SJMP STOP LOW: MOV R1,A ;save T to R1 STOP: CLR A ;clear A NOP END - End - Laboratory Experiment 2 7