MPSI Quiz1b Solution

You might also like

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

Quiz 1

Date: October 09, 2021


Q1. What is the value of the C and Z flags, upon last execution of DEC R16, for the program
given on the right.
LDI R17, 3
After the very last execution of DEC R16, LDI R18, 10
value of R16 will be zero. The value of LDI R16, 6 ;loop variable
the C flag will be zero, while the value of loop: ADD R18, R17
Z flag will be one. BRNE conditional DEC R16
BRNE loop
jump will not be taken since Z=1.

Q2 . Find the number of clock cycles needed, to execute the program given above. Also
find the duration when running on 2 MHz oscillator

The loop is executed 6 (six) times since the loop variable is loaded with value 6 and it
decrements each iteration until it reaches zero. The loop BRNE breaks when R16 = 0.

Instructions Clock Cycles Executed times Total Clock Cycles


LDI R17, 3 1 1 1×1= 1
LDI R18, 10 1 1 1×1= 1
LDI R16, 6 1 1 1×1= 1
loop: ADD R18, R17 1 6 1×6= 6
DEC R16 1 6 1×6= 6
BRNE loop 2 (true), or 5 (true), or (2 × 5) + (1 × 1) = 11
1 (false) 1 (false)
Sum of cycles 26 clock cycles

1
For 𝑓𝑜𝑠𝑐 = 2 𝑀𝐻𝑧, 𝑇𝑜𝑠𝑐 = 𝑓 = 0.5 μs = 500 ns
𝑜𝑠𝑐

Time taken for the program to complete: 𝑇𝑝𝑟𝑜𝑔. = 𝑇𝑜𝑠𝑐 × 26 = 13 μs

Q3. Write a simple assembly code to load value 20 in R20.

• Increment R20 NNN times using a loop (where NNN is your reg#)
• Write incremented value to PORTD in each loop iteration
• PORTD must be declared output, in the start
• It is suggested to use BRNE conditional branching instruction

LDI R20, 20 ; load value 20 in R20


LDI R16, 0xFF ; For setting PortD as output
OUT DDRD, R16 ; Set PortD as output
LDI R16, NNN ; loop variable, initialized with NNN = reg. number
loop: INC R20 ; Increment R20
OUT PORTD, R20 ; Write increment value of R20 to PortD
DEC R16 ; Decrement loop variable
BRNE loop ; continue loop until R16 goes to zero

You might also like