Midterm1 Sol

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Solutions for Midterm Exam 1

OSU ECE473/573
Winter 2002
Dr. Shiue
Name: __________________________________

ID: _______________(last five digits of your SSN)

1. (a) Assume the register A has packed BCD (59H), write a program to convert packed
BCD to two ASCII numbers and place them in R2 and R6. (hint: using ANL, ORL,
SWAP) and (b) make a “list file” consisting of line number, ROM locations, Opcodes,
and Instructions. (The program starts at 0000H) (20 points)

Line ROM locations Opcodes Instructions


1 0000 ORG 0
2 0000 7459 MOV A, #59H
3 0002 FA MOV R2, A ; A=59H
4 0003 540F ANL A, #0FH ; A=09H
5 0005 4430 ORL A, #30H ; A=39H
6 0007 FE MOV R6, A ; R6=39H
7 0008 EA MOV A, R2 ; A=59H
8 0009 54F0 ANL A, #0F0H; A=50H
9 000B C4 SWAP A ; A=05H
10 000C 4430 ORL A, #30H ; A=35H
11 000E FA MOV R2, A ; R2=35H
12 000F END

2. Read and test P1 to see whether it has the value A3H. If it does, clear 5 RAM locations
starting at RAM address 78H; otherwise, convert 23H to decimal. (20 points)

MOV P1, #0FFH ; P1 is an INPUT


MOV A, P1 ; Read Data from P1
CJNE A, #A3H, Next ; if A!=A3H, then jump
CLR A ; clear 5 RAM locations
MOV R1, #78H
MOV R2, #5
AGAIN: MOV @R1, A
INC R1
DJNZ R2, AGAIN
Next: MOV A, #23H
MOV B, #10
DIV AB
MOV R3, B
MOV R4, A
2-1. Read and test P1 to see whether it has the value A3H. If it does NOT, clear 5 RAM
locations starting at RAM address 78H; otherwise, convert A3H to decimal. (20
points)

MOV P1, #0FFH ; P1 is an INPUT


MOV A, P1 ; Read Data from P1
CJNE A, #A3H, Next ; if A!=A3H, then jump
MOV B, #10
DIV AB
MOV R3, B
MOV B, #10
DIV AB
MOV R4, B
MOV R5, A
Next: CLR A ; clear 5 RAM locations
MOV R1, #78H
MOV R2, #5
AGAIN: MOV @R1, A
INC R1
DJNZ R2, AGAIN

3. (a) Write a program to get the x value (ranging from 0 to 9) from P1 and send x2+x+1
to ACC continuously using indexed addressing mode, and (b) what is the state of the P
bit in the PSW after execution of each iteration. (20 points)

ORG 0 ; ROM locations starts from 0000H


MOV DPTR, #300H ; DPTR is a pointer starting from
MOV P1, #0FFH ; Make P1 as an INPUT
Back: MOV A, P1 ; get x from P1
MOVC A, @A+DPTR ; get x2+x+1 from table
SJMP Back
ORG 300H
Table: DB 1, 3, 7, 13, 21, 31, 43, 57, 73, 91
END
ACC Binary P
1 0000 0001 1
3 0000 0011 0
7 0000 0111 1
13 0000 1101 1
21 0001 0101 1
31 0001 1111 1
43 0010 1011 0
57 0011 1001 0
73 0100 1001 1
91 0101 1011 1
4. An 8051 subroutine is shown below (20 points)

SUB: MOV R0, #20H


Loop: MOV @R0, #0
INC R0
CJNE R0, #80H, Loop
RET

(a) What does this subroutine do?

This subroutine is to clear the RAM locations from 20H to 7FH.


(b) In how many machine cycles does each instruction execute?

MC Bytes Opcodes
SUB: MOV R0, #20H 1 2 78H, 20H
Loop: MOV @R0, #0 1 2 76H, 00H
INC R0 1 1 08H
CJNE R0, #80H, Loop 2 3 B8H, 80H, FAH
RET 2 1 22H

Hint: related address= - 6 = FAH (Jump back)

(c) How many bytes long is each instruction

Shown as above.

(d) Convert the instructions to Opcodes

Shown as above.

(e) How long does this subroutine take to execute? (assume 12MHz operation)

12MHz/12=1 MHz  1/1MHz=1µs for each MC


80H-20H=60H = 96(10)
(4*96)+1+2=387 MCs  387µs=0.387ms

5. Find the results at points (1), (2), and (3) in the following code (5 points)

CJNE A, #50, not_equal


….. ; point (1)
not_equal: JC Next
….. ; point (2)
Next: ….. ; point (3)

Point (1): A=50; Point (2): A>50; Point (3): A<50


6. Observe the following, noting the role of the OV flag for R1, R2, and R3 (5 points)

MOV A, #-128
ADD A, #-2
MOV R1, A
MOV A, #-2
ADD A, #-5
MOV R2, A
MOV A, #7
ADD A, #18
MOV R3, A

MOV A, #-128 ; A=-128


ADD A, #-2 ; A=-130
MOV R1, A ; R1=-130  OV=1
MOV A, #-2 ; A=-2
ADD A, #-5 ; A=-7
MOV R2, A ; R2=-7  OV=0
MOV A, #7 ; A=7
ADD A, #18 ; A=25
MOV R3, A ; R3=25  OV=0

7. Find the time delay for the delay subroutine shown below, if the system frequency is
12MHz. (10 points)

Delay: MOV R1, #100


Back: MOV R2, #200
Again: MOV R3, #300
Here: NOP
NOP
DJNZ R3, Here
DJNZ R2, Again
DJNZ R1, Back
RET

[For Loop R3]


Delay: MOV R1, #100
Back: MOV R2, #200
Again: MOV R3, #300
Here: NOP (1+1+2)*300*200*100=24,000,000
NOP
DJNZ R3, Here
DJNZ R2, Again
DJNZ R1, Back
RET

[For Loop R2]


Delay: MOV R1, #100
Back: MOV R2, #200
Again: MOV R3, #300
Here: NOP (1+2)*200*100=60,000
NOP
DJNZ R3, Here
DJNZ R2, Again
DJNZ R1, Back
RET

[For Loop R1]


Delay: MOV R1, #100
Back: MOV R2, #200
Again: MOV R3, #300
Here: NOP (1+2)*100=300
NOP
DJNZ R3, Here
DJNZ R2, Again
DJNZ R1, Back
RET

[Others]
Delay: MOV R1, #100
Back: MOV R2, #200
Again: MOV R3, #300
Here: NOP 1+2=3
NOP
DJNZ R3, Here
DJNZ R2, Again
DJNZ R1, Back
RET

The total MCs are 24,060,303 24,060,303µs = 24.060303 seconds

You might also like