Skip to main content
added 632 characters in body
Source Link
Huisman
  • 11.1k
  • 2
  • 25
  • 42

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

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.

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
added 7 characters in body
Source Link
Huisman
  • 11.1k
  • 2
  • 25
  • 42

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 E and F cost 2 machine cycles, the total number of cycles is 2495300.

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 2 machine cycles, the total number of cycles is 2495300.

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.

Source Link
Huisman
  • 11.1k
  • 2
  • 25
  • 42

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 2 machine cycles, the total number of cycles is 2495300.