Exercise 02-01: 8051 Assembly Language
QUESTION 1
Determine the contents of the destination operands and flags after executing the
following instructions.
After executing the instructions
Destination operand
CY
AC
(A) = 55H
0
0
(A) =
(A) =
Instruction
MOV A,#55H
ADD A,#E6H
SUBB A,#7BH
OV
0
QUESTION 2
PSW
CY
0
AC
0
F0
0
RS1
0
RS0
1
OV
0
P
0
New Value
10H
0FH
0EH
0DH
0CH
0BH
0AH
09H
08H
07H
05H
04H
03H
02H
01H
00H
77H
54H
0BH
45H
9DH
17H
1AH
20H
10H
E4H
12H
23H
34H
F5H
34H
0FH
The initial values of the PSW
(Program Status Word) and Internal
Data Memory (from 00H to 10H are
shown).
The codes below are executed in
sequence. Update the value in the
Internal Data Memory space at the
end of the execution.
ABC:
LP:
MOV R0,#10H
MOV A,#1H
MOV @R0,A
INC A
DEC R0
CJNE R0,#0BH,ABC
SJMP LP
QUESTION 3
a)
Determine the addressing modes of the source operand in the following instructions:
(i)
(ii)
(iii)
(iv)
23-03-2008
Instruction
MOV A,@R1
MOV R0,#45H
MOV A,R7
MOV 75H,50H
Addressing Mode
Exercise 02-01: 8051 Assembly Language
b)
Determine the validity of the following 8051 instructions:
Instruction
SUBB A,#45H
MOV A,@R3
MOV R7,50H
MOV #45H,A
(i)
(ii)
(iii)
(iv)
Valid or Invalid?
QUESTION 4
(a)
Identify whether the instructions shown in table 1 are valid or invalid.
Instruction
MOV A,@R2
MOV R4,56H
XRL @R1,A
MOV @R7,A
Valid/Invalid
Table 1
(b)
Identify the destination operand addressing mode of the instructions shown in
table 2.
Destination Operand
Addressing Mode
Instruction
MOV @R1,45H
XRL 45H,#75H
MOV R7,A
MOV 45H,@R0
Table 2
QUESTION 5
Find out the results of the operation shown in the table below.
Instruction
MOV A, #F5H
ADD A,#F2H
CLR C
ADDC A,#24H
CY
0
-
AC
0
-
OV
0
-
Remark
Initial Value
-
QUESTION 6
Identify the addressing mode of the source operands in the following instructions:
Instruction
MOV A,#56H
MOV 56H,76H
MOV A,@R0
ANL A,#45H
MOV A,R7
ORL A,45H
MOV @R1,25H
ADD A,@R1
SUBB A,45H
23-03-2008
Addressing Mode
Exercise 02-01: 8051 Assembly Language
QUESTION 7
PSW
CY
0
AC
0
F0
0
RS1
1
RS0
1
OV
0
P
0
New Value
1FH
1EH
1DH
1CH
1BH
1AH
19H
18H
17H
16H
15H
14H
13H
12H
11H
10H
77H
54H
0BH
45H
9DH
17H
1AH
20H
10H
E4H
12H
23H
34H
F5H
34H
0FH
The initial values of the PSW
(Program Status Word) and Internal
Data Memory (from 00H to 10H are
shown).
The codes below are executed in
sequence. Update the value in the
Internal Data Memory space at the
end of the execution.
K2:
LP:
MOV R0,#10H
MOV A,#20H
MOV @R0,A
DEC A
INC R0
CJNE R0,#18H,K2
SJMP LP
QUESTION 8
Identify the addressing mode of the destination operands in the following instructions:
Instruction
MOV 56H,A
MOV 56H,76H
MOV @R0,A
ANL 45H,#35H
MOV R7,A
ORL 55H,A
MOV @R1,25H
MOV R7,#45H
MOV @R1,23H
Addressing Mode
QUESTION 9
Figure 4-1 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 0CH
22H
12H
21H
62H
20H
5AH
Figure 4-1: Internal Data
Memory
What is the new content?
(Use the memory address shown in figure 31).
23-03-2008
Exercise 02-01: 8051 Assembly Language
QUESTION 10
Figure 3-1 shows the Internal Data
Memory of the 8051.
a.
What is the size (in bytes) of the
Internal Data Memory?
b.
Some memory locations in the
Internal Data Memory are bit
addressable. What is the range of
this bit addressable address?
c.
What is the byte address of the R0
register in Register Bank 3?
d.
What is the instruction to set bit
location 04H to a logic 1?
7FH
7EH
2FH
2EH
7FH
77H
7EH
76H
7DH
75H
7CH
74H
7BH
73H
7AH
72H
79H
71H
78H
70H
23H
22H
21H
20H
1FH
17H
0FH
07H
1EH
16H
0EH
06H
1DH
15H
0DH
05H
1CH
14H
0CH
04H
1BH
13H
0BH
03H
1AH
12H
0AH
02H
19H
11H
09H
01H
18H
10H
08H
00H
Bank 3
18H
Bank 2
10H
Bank 1
08H
07H
06H
05H
04H
03H
02H
01H
00H
R7
R6
R5
R4
R3
R2
R1
R0
Figure 3-1: Internal Data Memory
QUESTION 11
Figure 5-1 shows the content in some
location of the 8051 Internal Data
Memory.
2DH
2CH
2BH
2AH
29H
28H
27H
26H
25H
24H
23H
22H
21H
20H
Show the changes in the contents after
executing the program shown in listing 51.
MAIN: MOV R0,#20H
MOV R1,#2DH
MOV R7,#06H
M1:
MOV A,@R0
ADD A,@R1
MOV @R0,A
INC R0
DEC R1
DJNZ R7,M1
HANG:SJMP HANG
Listing 5-1
23-03-2008
13H
3FH
F5H
51H
F6H
44H
12H
62H
5AH
4FH
13H
33H
6FH
12H
Figure 5-1: Internal Data Memory
Exercise 02-01: 8051 Assembly Language
QUESTION 12
Determine the contents of the destination operands and flags after executing the following
instructions.
Instruction
MOV A,#0F5H
ADD A,#16H
SUBB A,#2BH
After executing the instructions
Destination operand
CY
AC
(A) = F5H
0
0
(A) =
(A) =
OV
0
QUESTION 13
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: JNB 1BH,M1
MOV 20H,#28H
M1:
SETB 04H
23H
22H
21H
20H
1FH
17H
0FH
07H
1EH
16H
0EH
06H
1DH
15H
0DH
05H
1CH
14H
0CH
04H
19H
11H
09H
01H
Bank 2
10H
Bank 1
08H
Bank 0
00H
Figure 2-1: Internal Data Memory
QUESTION 14
Identify the invalid instructions:
Instruction
MOV A,#56H
MOV #56H,76H
MOV A,@R0
ANL A,#45H
MOV A,R7
ORL @R1,45H
MOV @R1,25H
ADD A,@R7
SUBB A,45H
23-03-2008
1AH
12H
0AH
02H
Bank 3
18H
23H
F7H
22H
12H
21H
62H
20H
EAH
Internal Data Memory
1BH
13H
0BH
03H
Valid/Invalid
18H
10H
08H
00H
Exercise 02-01: 8051 Assembly Language
QUESTION 15
The content of the PSW is shown below.
The content of some locations in the
Internal Data Memory is shown in figure
4-1.
PSW
CY
0
AC
0
F0
0
RS1
1
RS0
1
OV
0
P
0
Show the changes in the contents after
executing the program shown in listing 41.
MOV R7,#06H
MOV A,@R0
ADD A,@R1
MOV @R0,A
INC R0
DEC R1
DJNZ R7,M1
HANG:SJMP HANG
Listing 4-1
M1:
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
19H
18H
2DH
20H
02H
01H
00H
12H
20H
2DH
Figure 4-1: Internal Data Memory
QUESTION 16
Determine the contents of the destination operands and flags after executing the following
instructions.
Instruction
MOV A,#0E5H
ADD A,#0FH
SUBB A,#3BH
ORL A,#0FH
23-03-2008
After executing the instructions
Destination operand
CY
AC
(A) = E5H
0
0
(A) =
(A) =
(A) =
OV
0
Exercise 02-01: 8051 Assembly Language
QUESTION 17
The content of the PSW is shown below.
The content of some locations in the
Internal Data Memory is shown in figure
1-1.
PSW
CY
0
AC
0
F0
0
RS1
0
RS0
0
OV
0
P
0
Show the changes in the contents after
executing the program shown in listing 11.
MOV R7,#06H
MOV A,@R0
ADD A,@R1
MOV @R0,A
INC R0
DEC R1
DJNZ R7,M1
HANG:SJMP HANG
Listing 1-1
M1:
32H
31H
30H
2FH
2EH
2DH
2CH
2BH
2AH
29H
28H
27H
26H
25H
17H
4FH
F5H
51H
16H
44H
12H
62H
52H
4FH
63H
23H
6FH
22H
19H
18H
2DH
20H
02H
01H
00H
Figure
12H
32H
25H
1-1
QUESTION 18
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 0AH,K1
MOV 21H,#28H
K1:
SETB 07H
23H
22H
21H
20H
18H
23H
F7H
22H
12H
21H
62H
20H
0AH
Internal Data Memory
23-03-2008
10H
08H
00H
1FH
17H
0FH
07H
1EH
16H
0EH
06H
1DH
15H
0DH
05H
1CH
14H
0CH
04H
1BH
13H
0BH
03H
1AH
12H
0AH
02H
19H
11H
09H
01H
Bank 3
Bank 2
Bank 1
Bank 0
Figure 2-1: Internal Data Memory
18H
10H
08H
00H
Exercise 02-01: 8051 Assembly Language
QUESTION 19
Determine the contents of the destination operands and flags after executing the following
instructions.
Instruction
MOV A,#033H
SUBB A,#0FFH
ANL A,#0FH
ORL A,#0AAH
After executing the instructions
Destination operand
CY
AC
(A) =
0
0
(A) =
(A) =
(A) =
OV
0
QUESTION 20
Figure 5-1 shows the content in some location of
the 8051 Internal Data Memory.
2DH
2CH
2BH
2AH
29H
28H
27H
26H
25H
24H
23H
22H
21H
20H
Show the changes in the contents after executing
the program shown in listing 5-1.
MAIN: MOV R0,#20H
MOV R7,#0AH
M1:
MOV A,@R0
ANL A,#0FH
MOV @R0,A
INC R0
DJNZ R7,M1
HANG:SJMP HANG
Listing 5-1
13H
3FH
F5H
51H
F6H
44H
12H
62H
5AH
4FH
13H
33H
6FH
12H
Figure 5-1
QUESTION 21
Listing 7-1 shows a simple assembly
language program.
(a)
What is the size of the program?
(b)
What is the machine code for the
instruction INC R0 and where is it
stored?
(c)
How many operands does the
instruction MOV A,@R0 have?
CSEG AT 00H
LJMP MAIN
CSEG AT 0100H
MAIN: MOV R0,#20H
MOV R7,#0AH
M1:
MOV A,@R0
ANL A,#0FH
MOV @R0,A
INC R0
DJNZ R7,M1
HANG:SJMP HANG
020100
7820
7F0A
E6
540F
F6
08
DFF9
80FE
Listing 7-1
23-03-2008
Exercise 02-01: 8051 Assembly Language
SOLUTION 1
Instruction
MOV A,#55H
ADD A,#E6H
SUBB A,#7BH
After executing the instructions
Destination operand
CY
AC
(A) = 55H
0
0
(A) = 3BH
1
0
(A) = BFH
1
1
SOLUTION 2
10H
0FH
0EH
0DH
0CH
0BH
0AH
09H
08H
07H
05H
04H
03H
02H
01H
00H
77H
54H
0BH
45H
9DH
17H
1AH
20H
10H
E4H
12H
23H
34H
F5H
34H
0FH
New Value
01H
02H
03H
04H
05H
0BH
SOLUTION 3
23-03-2008
(i)
(ii)
(iii)
(iv)
Instruction
MOV A,@R1
MOV R0,#45H
MOV A,R7
MOV 75H,50H
Addressing Mode
Register Indirect
Immediate
Register
Direct
(i)
(ii)
(iii)
(iv)
Instruction
SUBB A,#45H
MOV A,@R3
MOV R7,50H
MOV #45H,A
Valid or Invalid?
Valid
Invalid
Valid
Invalid
OV
0
0
0
Exercise 02-01: 8051 Assembly Language
SOLUTION 4
(a)
Instruction
MOV A,@R2
MOV R4,56H
XRL @R1,A
MOV @R7,A
Valid/Invalid
Invalid
Valid
Invalid
Invalid
Table 1
(b)
Destination Operand
Addressing Mode
Indirect
Direct
Register
Direct
Table 2
Instruction
MOV @R1,45H
XRL 45H,#75H
MOV R7,A
MOV 45H,@R0
SOLUTION 5
Instruction
MOV A, #F5H
ADD A,#F2H
CLR C
ADDC A,#24H
CY
0
1
0
1
AC
0
0
0
OV
0
0
0
Remark
Initial Value
-
SOLUTION 6
Instruction
MOV A,#56H
MOV 56H,76H
MOV A,@R0
ANL A,#45H
MOV A,R7
ORL A,45H
MOV @R1,25H
ADD A,@R1
SUBB A,45H
23-03-2008
Addressing Mode
Immediate
Direct
Register Indirect
Immediate
Register
Direct
Direct
Register Indirect
Direct
10
Exercise 02-01: 8051 Assembly Language
SOLUTION 7
New Value
1FH
1EH
1DH
1CH
1BH
1AH
19H
18H
17H
16H
15H
14H
13H
12H
11H
10H
77H
54H
0BH
45H
9DH
17H
1AH
20H
10H
E4H
12H
23H
34H
F5H
34H
0FH
18H
19H
1AH
1BH
1CH
1DH
1EH
1FH
20H
SOLUTION 8
Instruction
MOV 56H,A
MOV 56H,76H
MOV @R0,A
ANL 45H,#35H
MOV R7,A
ORL 55H,A
MOV @R1,25H
MOV R7,#45H
MOV @R1,23H
Addressing Mode
Direct
Direct
Register Indirect
Direct
Register
Direct
Register Indirect
Register
Register Indirect
SOLUTION 9
Byte location 21H is affected. The new value of this location became 72H.
SOLUTION 10
(a)
(b)
(c)
(d)
128 Bytes
00H to 7FH
18H
SETB 04H
23-03-2008
11
Exercise 02-01: 8051 Assembly Language
SOLUTION 11
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
13H
3FH
F5H
51H
F6H
44H
12H
62H
9EH
45H
64H
28H
AEH
25H
Figure 5-1: Internal Data Memory
SOLUTION 12
Instruction
MOV A,#0F5H
ADD A,#16H
SUBB A,#2BH
After executing the instructions
Destination operand
CY
AC
(A) = F5H
0
0
(A) = 0BH
1
0
(A) = DFH
1
1
OV
0
0
0
SOLUTION 13
Explanation:
23H
22H
21H
20H
F7H
12H
62H
EAH FAH
The JNB 1BH,M1 instruction checks for the status of bit
location 1BH and jump if it is zero. Bit location 1BH is part of
the byte data at byte location 23H. Data in location 23H is F7H
or 11110111 in binary. The bit data at location 1BH (indicated
by the underscore) is zero. Therefore, the instruction causes a
jump to M1and skipped the instruction MOV 20H,#28H.
The SETB 04H changes the data in byte location 20H.
23-03-2008
12
Exercise 02-01: 8051 Assembly Language
SOLUTION 14
Instruction
MOV A,#56H
MOV #56H,76H
MOV A,@R0
ANL A,#45H
MOV A,R7
ORL @R1,45H
MOV @R1,25H
ADD A,@R7
SUBB A,45H
Valid/Invalid
Valid
Invalid
Valid
Valid
Valid
Invalid
Valid
Invalid
Valid
SOLUTION 15
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
13H
3FH
F5H
51H
F6H
44H
12H
62H
9EH
45H
64H
28H
AEH
25H
19H
18H
2DH
20H
27H
26H
02H
01H
00H
12H
20H
2DH
12H
20H
2DH
Figure 4-1: Internal Data Memory
23-03-2008
13
Exercise 02-01: 8051 Assembly Language
SOLUTION 16
Instruction
MOV A,#0E5H
ADD A,#0FH
SUBB A,#3BH
ORL A,#0FH
After executing the instructions
Destination operand
CY
AC
(A) = E5H
0
0
(A) = F4H
0
1
(A) = B9H
0
1
(A) = BFH
0
1
OV
0
0
0
0
SOLUTION 17
Analyzing the program, you will noticed that
the program basically ADD two numbers
pointed by R0 and R1, and then stored them
in the location pointed by R0.
Example: Data in location 25H is added
with data in location in location 32H and the
result 39H is stored back in location 25H.
Basically:
(25H) + (32H) (25H) = 39H
(26H) + (31H) (26H) = BEH
(27H) + (2FH) (27H) = 18H
(28H) + (2EH) (28H) = B4H
(29H) + (2DH) (29H) = 65H
(2AH) + (2CH) (2AH) = 96H
32H
31H
30H
2FH
2EH
2DH
2CH
2BH
2AH
29H
28H
27H
26H
25H
96H
65H
B4H
18H
BEH
39H
07H
00H
01H
00H
2DH
2AH
Content indicates changes.
SOLUTION 18
JB 0AH,K1 >> Jump if bit address 0AH contains 1.
(21H) = 62H (or 01100010 in binary). Bit data
(0AH)=0. Therefore, there is no jump.
SETB 07H changes the value on byte location 20H from
0AH (00001010) to 8AH (10001010)
23-03-2008
14
23H
22H
21H
20H
F7H
12H
62H 28H
0AH 8AH
Exercise 02-01: 8051 Assembly Language
SOLUTION 19
Instruction
MOV A,#033H
SUBB A,#0FFH
ANL A,#0FH
ORL A,#0AAH
After executing the instructions
Destination operand
CY
AC
(A) = 33H
0
0
1
1
(A) = 34H
1
1
(A) = 04H
1
1
(A) = AEH
OV
0
0
0
0
SOLUTION 20
The program simply take the value pointed
by the R0 register and then mask off the
higher order nibble (4-bit),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
06H
04H
02H
02H
0AH
0FH
03H
03H
0FH
02H
SOLUTION 21
(a)
The size of the program can be determined by counting the number of bytes of the
machine codes. The answer is 16 bytes.
(b)
The machine code for instruction INC R0 is 08H. It is stored in program memory
location 0108H.
(c)
This is a two-operand instruction.
23-03-2008
15