Exercise 02-02: 8051 Assembly Language
QUESTION 22 The program in listing 6-1 clears c lears the content in internal data memory location 25H to32H. Modify the program to populate the content in location 25H to 32H as shown in figure 6-1. Your solution should demonstrate efficiency in programming.
M1:
HG:
MOV R0,#25H CLR A MOV @R0,A INC R0 CJNE R0,#33H,M1 SJMP HG Listing 6-1
32H 31H 30H 2FH 2EH 2DH 2CH 2BH 2AH 29H 28H 27H 26H 25H
0DH 0CH 0BH 0AH 09H 08H 07H 06H 05H 04H 03H 02H 01H 00H
19H 18H
2DH 20H
02H 12H 01H 0DH 00H 25H Figure 6-1
QUESTION 23 Find out the results of the operation shown in the table below. Instruction
Result
MOV A, #0F5H ORL A,#0FH ANL A,#0FH SWAP A
QUESTION 24 Determine the contents of the destination operands and flags after executing the following instructions.
Instruction Instruct ion MOV A,#0E5H SUBB A,#16H SUBB A,#2BH
23-03-2008
After executing executing the instru instruction ctions s Destination Destinati on operand CY AC (A) = 0 0 (A) = (A) =
1
OV 0
Exercise 02-02: 8051 Assembly Language
QUESTION 25 Determine the changes in the internal data memory after executing the following instructions in sequence.
48H 47H 46H 45H
13H 3FH F5H 51H
02H 12H 01H 62H 00H 45H Figure 2-1: Internal Data Memory
INC @R0 INC R0 INC @R0 INC R0
QUESTION 26
Find out the results of the operation shown in the table below. Instruction
Result
MOV 12H, #78H CLR A MOV R1,#12H ADD A,@R1 MOV 12H,A INC R1 MOV @R1,A
QUESTION 27 With reference to the assembly listing, show how the machine codes of the program fill up the program memory space up to code memory location C:005CH.
0000 ORG 0000H 0000 020050 LJMP MAIN 0050 ORG 0050H 0050 MAIN: 0050 7581A0 MOV SP,#0AH 0053 900050 MOV DPTR,#0050H 0056 7410 MOV A,#10H 0058 93 MOVC A,@A+DPTR 0059 23 RL A 005A 12005F LCALL WAIT 005D 80F1 SJMP MAIN 005F 7EF0 WAIT: MOV R6,#0F0H 0061 7F10 D1: MOV R7,#010H 0063 DFFE D2: DJNZ R7,D2 0065 DEFA DJNZ R6,D1 0067 22 RET END
What is the size of the program?
23-03-2008
2
Exercise 02-02: 8051 Assembly Language
QUESTION 28 The figure shows some of the contents in the 8051 Internal Data Memory. Which memory location is affected by the following instruction:
48H 47H 46H 45H
13H 3FH F5H 51H
SETB 1FH 23H 12H 22H 62H 21H 5AH Internal Data Memory
What is the new content?
(You may need reference to the internal data memory map. This information is provided during your test or exam ) QUESTION 29 The content of some of the location of the Internal Data Memory is shown below. How are the contents affected when the following program is executed:
7FH 7EH
MAIN: JB 1EH,M1 MOV 20H,#28H M1: SETB 05H
23H 22H 21H 20H 18H
23H 47H 22H 12H 21H 62H 20H 8AH Internal Data Memory
10H 08H 00H
1FH
1EH
1DH
1CH
1BH
1AH
19H
18H
17H
16H
15H
14H
13H
12H
11H
10H
0FH
0EH
0DH
0CH
0BH
0AH
09H
08H
07H
06H
05H
04H
03H
02H
01H
00H
Bank 3 Bank 2 Bank 1 Bank 0
Internal Data Memory Map
23-03-2008
3
Exercise 02-02: 8051 Assembly Language
QUESTION 30 What are the changes in the internal data memory after executing the program showed?
28H 27H 26H 25H 24H 23H 22H 21H 20H
CSEG AT 00H LJMP MAIN CSEG AT 0100H MAIN: MOV DPTR,#0100H MOV R1,#0H MOV R0,#20H M1: MOV A,R1 MOVC A,@A+DPTR MOV @R0,A INC R0 INC R1 CJNE A,#E9H,M1 HANG: SJMP HANG
020100 900100 7900 7820 E9 93 F6 08 09 B4E9F8 80FE
30H 7EH F6H 13H 20H 1EH 1FH 05H 43H
02H 12H 01H 62H 00H 45H Internal Data Memory
QUESTION 31 The contents in the program and data memory are shown below. For the given program, update the changes in the memory after execution. G1:
HG:
0033H 0032H 0031H 0030H
1EH 1FH 05H 43H
48H 47H 46H 45H
0002H 12H 0001H 62H 0000H 45H Internal Program Memory
23-03-2008
MOV DPTR,#0030H MOV R0,#45H CLR A PUSH A MOVC A,@A+DPTR MOV @R0,A INC R0 POP A INC A CJNE A,#4H,G1 SJMP HG
13H 3FH F5H 51H
02H 12H 01H 62H 00H 45H Internal Data Memory
4
Exercise 02-02: 8051 Assembly Language
QUESTION 32
Fill in the expected results after executing each of the instruction in sequence. Instruction MOV A,#45H SETB C RRC A MOV B,#10D DIV AB ANL A,#0FH DEC A MOV 40H,#10H MOV R0,#40H ADD A,@R0 MOV @R0,A
Results
QUESTION 33 The figure 4-1 shows the content in some location of the 8051 Internal Data Memory. Show the changes in the contents after executing the program shown in listing 4-1. MAIN: MOV R0,#20H MOV R7,#0AH M1: MOV A,@R0 XRL A,#0FFH MOV @R0,A INC R0 DJNZ R7,M1 HANG:SJMP HANG Listing 4-1
2DH 2CH 2BH 2AH 29H 28H 27H 26H 25H 24H 23H 22H 21H 20H
13H 3FH F5H 51H F6H 44H 12H 62H 5AH 4FH 13H 33H 6FH 12H
Figure 4-1
QUESTION 34 a) Determine the addressing modes of the destination operand in the following instructions:
(i) (ii) (iii) (iv)
23-03-2008
Instruction MOV 77H,A MOV 21H,#45H MOV @R1,45H MOV R7,#7EH
5
Addressing Mode
Exercise 02-02: 8051 Assembly Language
b)
Determine the contents of the destination operands and flags after executing the following instructions.
Instruction MOV A,#0A5H SUBB A,#0A6H ADDC A,#02H
After executing the instructions Destination operand CY AC (A) = 0 0 (A) = (A) =
OV 0
QUESTION 35 Write an 8051 assembly language program to fulfill the following requirements: Compare the contents in internal memory location (D:50H) and (D:51H), and move the larger number to internal data location (D:61H) and the smaller number to (D:60H).
QUESTION 36 Write an 8051 assembly language program to add all the contents in internal data memory from (D:50H) to (D:60H) and store the results in internal data memory (D:70H).
QUESTION 37 Write an 8051 assembly language program to divide the number in internal data memory at (D:40H) by the content in (D:41H) and store the Quotient of the result in (D:42H) and remainder in (D:43H).
23-03-2008
6
Exercise 02-02: 8051 Assembly Language
QUESTION 38
The content in the internal data memory is shown. Indicate the changes in the content after the following sequence of instructions are executed.
CLR RS0 CLR RS1 MOV R0,#30H SETB RS0 MOV R0,#13H SETB RS1 CLR RS0 MOV R1,00H
CY
AC
F0
RS1
RS0
OV
P
0
0
0
0
0
0
0
PSW
13H 12H 11H 10H
30H 7EH F6H 13H
09H 08H
05H 43H
02H 12H 01H 62H 00H 45H Internal Data Memory
23-03-2008
7
Exercise 02-02: 8051 Assembly Language
SOLUTION 22
M1:
HG:
MOV R0,#25H CLR A MOV @R0,A INC R0 INC A CJNE R0,#33H,M1 SJMP HG
SOLUTION 23 Instruction
Result
MOV A, #0F5H ORL A,#0FH ANL A,#0FH SWAP A
(A) = F5H (A) = FFH (A) = 0FH (A) = F0H
SOLUTION 24
Instruction MOV A,#0E5H SUBB A,#16H SUBB A,#2BH
After executing the instructions Destination operand CY AC (A) = E5H 0 0 (A) = CFH 0 1 (A) = A4H 0 0
OV 0 0 0
SOLUTION 25
Where is R0? By default, R0 is located in direct address 00H. Therefore (R0) = 45H. INC @R0 causes the content in memory location 45H to be incremented from 51H to 52H. INC R0 causes (R0) = 46H. The next INC @R0 causes the content in memory location 46H to be incremented from F5H to F6H. INC R0 causes (R0) = 47H. SOLUTION 26 Instruction
Result
MOV 12H, #78H CLR A MOV R1,#12H ADD A,@R1 MOV 12H,A INC R1 MOV @R1,A
23-03-2008
(D:12H) = 78H (A) = 0 (R1) = 12H (A) = 78H (D:12H) = 78H (R1) = 13H (D:13H) = 78H
8
48H 47H 46H 45H
13H 3FH F5H F6H 51H 52H
02H 01H 00H
12H 62H 45H 47H
Exercise 02-02: 8051 Assembly Language
SOLUTION 27
005CH 005BH 005AH 0059H 0058H 0057H 0056H 0055H 0054H 0053H 0052H 0051H 0050H
00H 12H 0BH 23H 93H 10H 74H 50H 00H 90H A0H 81H 75H
0002H 50H 0001H 00H 0000H 02H Code Memory
The program is 27 bytes.
SOLUTION 28
(D:23H) changed to 92H.
SOLUTION 29
Explanation: 23H 22H 21H 20H
47H 12H 62H 8AH AAH
The JB 1EH,M1 instruction checks for the status of bit location 1EH and jump if it is zero. Bit location 1EH is part of the byte data at byte location 23H. Data in location 23H is 47H or 01001111 in binary. The bit data at location 1EH (indicated by the underscore) is 1. Therefore, the instruction causes a jump to M1and skipped the instruction MOV 20H,#28H. The SETB 05H changes the data in byte location AAH.
23-03-2008
9
Exercise 02-02: 8051 Assembly Language
SOLUTION 30
Internal data memory location D:00H and D:01H represents R0 and R1 respectively. 28H 27H 26H 25H 24H 23H 22H 21H 20H
Code from location starting at 0100H is copied to the internal data memory until the code E9H is encountered.
30H 7EH E9H F6H 20H 13H 78H 20H 00H 1EH 79H 1FH 00H 05H 01H 43H 90H
02H 12H 01H 62H 08H 00H 45H 27H Internal Data Memory
SOLUTION 31
There is no change in the program memory. Internal data memory location D:00H is R0. 48H 47H 46H 45H
13H 3FH F5H 51H
1EH 1FH 05H 43H
02H 12H 01H 62H 00H 45H 49H Internal Data Memory
SOLUTION 32 Instruction MOV A,#45H SETB C RRC A MOV B,#10D DIV AB ANL A,#0FH DEC A MOV 40H,#10H MOV R0,#40H ADD A,@R0 MOV @R0,A
23-03-2008
Results (A) = 45H (C) = 1 (A) = A2H (B) = 0AH (A) = 10H (B) = 02H (A) = 0H (A) = FFH (D:40H) = 10H (R0) = 40H (A) =0FH (D:40H) = 0FH
10
Exercise 02-02: 8051 Assembly Language
SOLUTION 33
The program simply take the value pointed by the R0 register and compliment its bits ,and then stored back in the same location. R0 is then incremented to point to the next location and the operation is repeated. This goes round for 10 times as controlled by the R7 register.
2DH 2CH 2BH 2AH 29H 28H 27H 26H 25H 24H 23H 22H 21H 20H
13H 3FH F5H 51H F6H 44H 12H 62H 5AH 4FH 13H 33H 6FH 12H
09H BBH EDH 9DH A5H B0H ECH CCH 90H EDH
SOLUTION 34
(i) (ii) (iii) (iv)
Instruction MOV A,#0A5H SUBB A,#0A6H ADDC A,#02H
Instruction MOV 77H,A MOV 21H,#45H MOV @R1,45H MOV R7,#7EH
Addressing Mode Direct Direct Register Indirect Register
After executing the instructions Destination operand CY AC (A) = A5H 0 0 (A) = FFH 1 1 (A) = 02H 1 1
OV 0 0 0
SOLUTION 35 Notes: The SUBB (subtraction) instruction can be used to determine the smaller or larger number between two numbers by observing the carry (or borrow) flag. If a smaller number is subtracted by a larger number, the carry (or borrow) flag will be set. The JC (Jump if Carry is Set) instruction can then be used to decide the course of action after determining which number is smaller or larger.
23-03-2008
11
Exercise 02-02: 8051 Assembly Language
ORG 0000H LJMP MAIN ORG 0100H MAIN: CLR C MOV A,50H SUBB A,51H
ABC:
; ; ; ;
Logic: Subtract (D:50H) by (D:51H). If (D:50H) is smaller, then the Carry Flag will be set indicating a borrow is necessary.
JC ABC
; Jump if (D:50H) is smaller.
MOV 60H,51H MOV 61H,50H SJMP HANG
; (D:51H) contains the smaller number.
MOV 60H,50H MOV 61H,51H
; (D:50H) contains the smaller number.
HANG: SJMP HANG
SOLUTION 36 ORG 0000H LJMP MAIN ORG 0200H MAIN: MOV R1,#50H CLR C CLR A M1: ADD A,@R1 INC R1 CJNE R1,#61H,M1 MOV 70H,A HANG: SJMP HANG
SOLUTION 37 ORG 0000H LJMP MAIN ORG 0090H MAIN: MOV A,40H MOV B,41H DIV AB MOV 42H,A MOV 43H,B HANG: SJMP HANG
23-03-2008
12
Exercise 02-02: 8051 Assembly Language
SOLUTION 38
13H 12H 11H 10H
30H 7EH F6H 13H 30H
09H 08H
05H 43H 13H
02H 12H 01H 62H 00H 45H 30H Internal Data Memory
23-03-2008
13