## Help with counting the machine cycles for this assembly program?

19 posts / 0 new
Author
Message

Here's the program.

LDI R17,20

L1: LDI R16,50

L2: NOP

NOP

DEC R16

BRNE L2

DEC R17

BRNE L1

Here's my calculation

I first counted the cycles here

L2: NOP

NOP

DEC R16

BRNE L2

[(1+1+1+2)*50]-1 = 249 Cycles ( -1 is because; In the last iteration BRNE cost is 1)

Then this part;

LDI R17,20     = 1 cycle

L1: LDI R16,50

L2: 249 cycles

DEC R17

BRNE L1

The bold part is another loop.

1+ [(1+249+1+2)*20]-1 = 5060 cycles

Please be kind enough to point out any mistakes.

Thank you very much :)

Last Edited: Mon. Nov 4, 2019 - 02:06 PM

Bung it into the Simulator.   The PC will give you the correct answer.

You seem to know what you are doing.

In real life,   you evaluate the "inner loop".   i.e. just as you have.

And then count the number of inner loops.

Don't worry about the "outside" housekeeping.

If you want a cycle-accurate figure,   use the Simulator.

In real life,   you often do the "inner loop".    You seldom worry about the rest.

David.

Thank you very much for the quick reply sir.

Last Edited: Tue. Oct 22, 2019 - 04:30 PM

if you want to make the code readable, specially if you want to know the cycle counts, then in the avr instruction set overview ( which you for sure have found ) there are the needed cycles mentioned.

If you put these as a comment and the loop time calculation below that you in the future will have a reference to what you have done. Then you can also quickly see if you did anything wrong ( and were you went wrong) by using the simulator.

Seems correct to me. The last loop iteration takes one cycle less, but normally this is the extra cycle that was used to initialize the loop counter.

So, in many cases, you can just count the init as part of the loop, knowing that it will "cancel out" with the last iteration, so:

```    LDI R17,20                      |
|
L1: LDI R16,50      |               |
L2: NOP         1   |               |
NOP         1   |---> 50*5      |
DEC R16     1   |               |---->20*(50*5+3) = 5060
BRNE L2     2   |               |
|
DEC R17                     1   |
BRNE L1                     2   |```

Life just seems so much simpler if you...

Cliff, I think he has to show the teachers how he came up with that number, I do not think they will be happy if he just showed them your output as he will not have learned anything from that( Guess the learning experience here is how to read an instruction manual and identify the important parts in it...... )

My result seems to suggest an "out by 1" error. I imagine this might be because of BRNE being 1 not 2 on the last iteration.

I don't see the bit where this is defined as an educational exercise - why should there be "teachers" involved in this anyway?

Anyway, if I were a teacher I would give additional merit from those who discovered that the professional engineering solution to this was to get a computer to do the leg work for you - it is, after all, why we all program computers in the first place!

My result seems to suggest an "out by 1" error. I imagine this might be because of BRNE being 1 not 2 on the last iteration.

I actually paid attention to the -1 that happens because of the because of the last BRNE iteration (In both inner loop and outer loop). I still can't figure out why the answer is 5059 and not 5060. If you clear that a bit more it would be really helpful.

This isn't a homework  :)

Thank you that helped sir :)

5059 is how many cycles the simulator executed (I'm too lazy to count when there's a computer will do it for me). If you repeat what I did you could single step every last opcode and see how it adds up to 5059.

esavrperson wrote:
If you clear that a bit more it would be really helpful.

You can use breakpoints and step in the simulator just as well as we can.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

esavrperson wrote:
This isn't a homework  :)

Re-inventing delay loops?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

I wonder how much time has been wasted writing delay loops and routines?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

esavrperson wrote:

My result seems to suggest an "out by 1" error. I imagine this might be because of BRNE being 1 not 2 on the last iteration.

I actually paid attention to the -1 that happens because of the because of the last BRNE iteration (In both inner loop and outer loop). I still can't figure out why the answer is 5059 and not 5060. If you clear that a bit more it would be really helpful.

This isn't a homework  :)

Have you gotten hold of the AVR instruction set?

if not, then go and look for that, it will be your best friend here.

have a look at what the instruction DEC R17 does on the last itteration,

And also what the instruction BRNE does specially the change with the possible outcome of the instruction.

It is all there in the instruction set, you just have to have a good look and truly understand what instructions are doing.

meslomp wrote:
have a look at what the instruction DEC R17 does on the last itteration,
OK, give in, I just went and checked a copy and I am having a little difficulty spotting how one DEC is ever any different from any other DEC. Care to explain? In terms of cycles it's the same even on the occasion that 0x01 DEC's to 0x00.

Last Edited: Thu. Oct 24, 2019 - 09:21 AM

I also wondered what meslomp is on.

```outer:
LDI R17,20
inner:
LDI R16,50
loop: NOP
NOP
DEC R16
BRNE loop

DEC R17
BRNE inner

done: NOP```

The loop block takes 5 cycles    (the controlling LDI and last BRNE cancel each other)

The inner block takes 50 * loop cycles

The outer block takes 20 * (inner + 3)

I make the total 20 * ((50 * 5) + 3) = 5060 cycles.

Having suggested "Trust the Simulator" I will have to load up AS7.0 and try it for myself.

David.

Last Edited: Thu. Oct 24, 2019 - 09:18 AM

The instruction set tells you that the BRNE instruction looks at the Z flag in the SREG register.

Now the DEC instruction, that is right in front of the BRNE, sets the Z flag only when the decrement reaches 0. It will always take 1 cycle, so not directly involved but as a side effect one should see and understand.

The link between BRNE and DEC is here thus is the Z flag.

The BRNE instruction takes only 1 clock cycle if the condition is false (Z flag is cleared) and 2 clock cycles if the condition is true ( Z flag is set ).

So for all but the last loop you can do the math as given, only the last loop will have 1 cycle less as the BRNE instruction has 1 clock cycle less.

that is the link here between the DEC / BRNE and 1 clock cycle difference I had hope the OP would come up with.

I think understanding things like this is part of what you should be knowing when doing assembly programming, and perhaps even more specific when you are working on counting clock cycles.