0
\$\begingroup\$

For the below mentioned 8051 assembly code:
Time elapse: MOV R0,#100
Part 1 \$ \quad \, \, \,\,\$ : MOV R1,#50
Part 2 \$ \quad \, \, \,\,\$ : MOV R2,#248
Part 3 \$ \quad \, \, \,\,\$ : DJNZ R2,Part3
\$ \quad \quad \quad \, \, \,\,\,\, \$ : DJNZ R1,Part2
\$ \quad \quad \quad \, \, \,\,\, \, \$ : DJNZ R0,Part1

Assumptions:
1) Microcontroller is running at \$12 MHz \$ frequency and \$ 1 \$ Machine cycle is having \$ 12 \$ clock cycles
2)MOV instruction takes \$ 1 \$ Machine cycle
3)DJNZ instruction takes \$ 2 \$ machine cycle
Calculate time required for execution of part1:
a) \$ 2495600 \mu \$s \$ \quad \$ b) \$ 2496300 \mu \$s \$ \quad \$ c) \$ 2495300 \mu \$s \$ \quad \$ d) \$ 2496600 \mu \$s

My Approach:
Here \$ \# 100 \to \$ Decimal
$$\because \text{In 8051 : MOV Rn,#Data} \implies \text{#Data } \to 8 \text{bit}$$ We Know:
DJNZ Rn,addr: $$\text{Decrement register 'Rn' and Jump to 'addr' if Rn} \ne (00)_H$$

Now,time required for execution of Part1:
Part 1 \$ \quad \, \, \,\,\$ : MOV R1,#50
Part 2 \$ \quad \, \, \,\,\$ : MOV R2,#248
Part 3 \$ \quad \, \, \,\,\$ : DJNZ R2,Part3
\$ \quad \quad \quad \, \, \,\,\,\, \$ : DJNZ R1,Part2
\$ \quad \quad \quad \, \, \,\,\, \, \$ : DJNZ R0,Part1

So,time required for execution of Part1, \$ T = N * 12*T_{CLK}\$
where, \$ N= \$ total number of machine cycles required for execution of Part1 $$\implies T = N * 12* \frac{1}{f_{CLK}}$$ $$= N * 12* \frac{1}{12*10^6}$$ $$\therefore T=N \mu s \quad \dots (i)$$ For \$ N :\$
\$N= 1+1+\{ 247*2 + 1*2\} \quad \color{red}{[\because \text{leftmost } 1 \text{ is for 'MOV R1,#50' ;}}\$
\$ \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \color{red}{\text{2nd 1 is for 'MOV R2,#248' ;}} \$
\$ \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \color{red}{\text{ 247 times 'DJNZ R2,Part3' True Condition &}} \$
\$ \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \quad \color{red}{\text{1 time 'DJNZ R2,Part3' False Condition}]} \$
\$ \quad \quad \quad + 49*2 +49 \{ 1 +248*2\} + 1*2 \quad \color{red}{[\because \text{49 times 'DJNZ R1,Part2 ' True Condition & }}\$
\$ \color{red}{1 \text{ time 'DJNZ R1,Part2 ' False Condition}]}\$
\$ \quad \quad \quad \quad \quad + \underbrace{98*2 +98 \{ 1 +1 +248*2 +49 (2 + 1 +248*2 )+1*2 \} }_{\color{blue}{\text{Calculation for 98 times}}} \$
\$ \quad \quad \quad \quad \quad \quad \quad + \color{green}{\{}1*2+ 1*1 \color{green}{\} \to \text{calculation for 99th time}}\$ \$ \quad \color{red}{[\because \text{'DJNZ R0,Part1' is True for 99 times} \implies \text{Part1 is executed 99 times}]}\$
$$\implies N= 2470348$$ $$\therefore T= 2470348 \mu s$$ ,which is not there in the option, Key Provided:c)\$2495300 \mu \$s
so, please anyone help me to find my error...

\$\endgroup\$
1
\$\begingroup\$

You don't need to split up the DJNZ True Condition and DJNZ False Condition states in your calculations. Since the DJNZ loop test control is at the end of the loop, all the operations for the loop happen on the zeroth time when the loop exits as they do all the previous times. This would be different if the loop test control was at the top (or head) of the loop. Thus, Huisman's answer is correct. Counting from Part1, the loop ends after:

100 * (1 + (50 * (1 + (248 * (2)) + 2)) + 2) cycles 

and thus the total number of cycles is 2495300.

\$\endgroup\$
1
\$\begingroup\$

I think you should approach it considering from the most inner loop to the most outer loop.
First label the instructions:

Time elapse: MOV R0,#100 \$ \quad \, \, \, A\$
Part 1 \$ \quad \, \, \, \, \, \$ : MOV R1,#50 \$ \quad \, \, \, \, \, \, B \$
Part 2 \$ \quad \, \, \, \, \, \$ : MOV R2,#248 \$ \quad \, \, \, C\$
Part 3 \$ \quad \, \, \, \, \, \$ : DJNZ R2,Part3 \$ \quad \, D\$
\$ \quad \quad \quad \, \, \, \, \, \, \, \, \$ : DJNZ R1,Part2 \$ \quad \, E\$
\$ \quad \quad \quad \, \, \, \, \, \, \, \, \$ : DJNZ R0,Part1 \$ \quad \, F\$

The inner loop consist of C and D. Only looking at this most inner loop, C is executed once and D is executed 248 times. So, $$ 1*C + 248*D $$ The loop nesting loop [C and D] consist of B and E, of which (only considering [B [CD] E]) B is executed once and E is executed 50 times. E causes loop [C and D] to be executed 50 times as well. So,

$$ 1*B + 50*E + 50*(1*C + 248*D) $$

The outer loop nesting loop [B and E] consist of A and F, of which A is executed once (but should not be taken into account) and F is executed 100 times. E causes loop [B and E] to be executed 100 times as well. So,

$$ 100*F + 100*(1*B + 50*E + 50*(1*C + 248*D)) $$

Considering instructions B and C cost 1 machine cycle, and D, E and F cost 2 machine cycles, the total number of cycles is 2495300.

Approach of OP
I tried to substitute my defined labels in your approach (and hope I did it right):

For \$ N :\$
\$N= B+C+\{ 247*D_{true} + 1*D_{false}\}\$

\$ + 49*E_{true} +49 \{ C +248*D\} + 1*E_{false} \$

\$ + \color{red}{98}*F_{true} + \color{red}{98} \{ B + C +248*D + 49 (E + C +248*2 )+1*E_{false} \} \$

\$ 1*F_{false}+ \color{red}{1*1}\$

I think I understand your approach: you execute the it the way a machine reads the code!
I think there are 2 errors in it:

  • part 1 is executed 100 times, so the red 98 should be 99.
  • the last 1*1 is unclear to me and shouldn't be added
\$\endgroup\$
  • \$\begingroup\$ +\$ 1 \$ for your approach ,but could you please help me to find the mistake in my approach \$\endgroup\$ – Suresh Mar 9 at 5:17
  • \$\begingroup\$ Please accept the answer if it is correct \$\endgroup\$ – Huisman Mar 10 at 13:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.