## Time spent on the processor for a code snippet

11 posts / 0 new
Author
Message

LDI R16, 0x97

ANDI R16, 0x1F

EOR R17, R17

ORI R17, 0x1F

EOR R18, R18

L2:

NOP

INC R16

DEC R17

INC R18

CP R17,R16

BRCC L2

NOP

When running the given code snippet, how long does the processor spend time on the total, until the nop command in the last line (except nop) is executed, including the time it takes to execute the command ldi in the first line?

for sequence of operations:

R16:              R17:              R18:

0x97              0x00              0x00

0x17              0x1F              0x01

0x18              0x1E              0x02

0x19              0x1D             0x03

0x1A              0x1C             0x04

0x1B              0x1B             0x05

0x1C              0x1A

This topic has a solution.
Last Edited: Sat. Nov 10, 2018 - 11:50 PM

How can you say 2.44us? That depends entirely what F_CPU is.

The simulator in AS7 can count both cycles and, if you tell it an F_CPU value it will also calculate elapsed time.

How can I make a calculation for atmega 2560 device at 16 MHz? This is my exam question at university. I have no other data for question. Thanks for help.

Last Edited: Wed. Nov 7, 2018 - 08:32 PM
This reply has been marked as the solution.

Are you saying you have absolutely no idea how to approach this problem?

yep. I have to solve the question on paper. I have no idea how to start.

But nop, andi, eor, ori, eor, ınc, dec, cp are 1 cycle. brcc is 1/2 cycle. I need to use this method.

The existence of the simulator makes these type of problems too easy to solve.

The instruction set gives the number of cycles for each instruction. You just need to add them and to understand how that loop works.

Pretend your the cpu, and you have reg's 16, 17, and 18.

Now walk the code, and perform its operation on the three registers, count how many times you visit each instruction.

when you reach the end, multiply the number of times each instruction was visited and multiply by the cycle count (1 or 1/2)

done.

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
stack gold/silver https://www.onegold.com/join/713...

If you really want to know how to "solve" this then the book you need is:

http://ww1.microchip.com/downloa...

For each opcode it lists "cycles". So take the start of  your code:

```LDI R16, 0x97
ANDI R16, 0x1F```

If you look up "LDI" you find:

So that opcode will take one cycle. Then look up ANDI:

Keep doing this for all the code and add up all the cycles and you will then know how many total cycles it takes to execute.

The complication here is the "L2:" loop, because that contains a conditional branch that section will execute multiple times. So you have to multiply the sum of the cycles in that section by the number of times it will loop.

When you are done you will have a complete number of cycles.

The other part of this is that if the CPU runs at 16MHz then that means it executes 16 million cycles per second. So how long do you think one cycle is? It will be a value in nanoseconds. When you know that multiply it by the total number of cycles you already calculated and that will tell you the overall execution time in microseconds.

(or follow my earlier advice and "cheat" by using the cycle counter in the simulator - that's all very well and will give you an instant cycle count and duration HOWEVER I;m sure your course tutor wants to see evidence that you have actually looked all the opcodes up in the opcode manual as I just did and added up all the cycles separately. Having said that the simulator will be a useful check you got the right answer!)

EDIT: Just tried the simulator myself. Here's what I got:

I have redacted some of that as I don't want to make this too easy for you.

It's interesting the tutor put that NOP on the very end. It's almost like he was asking you to run this code to a breakpoint!

Last Edited: Thu. Nov 8, 2018 - 09:31 AM

hello folks found the solution. I think it's such a difficult question.
A total of 5 cycles are performed for the first 5 lines of the code block.

```LDI R16, 0x97      - 1 cycle

ANDI R16, 0x1F    - 1 cycle

EOR R17, R17      - 1 cycle

ORI R17, 0x1F      - 1 cycle

EOR R18, R18       - 1 cycle```

Total: 5 cycle

Recovery of r18 by reference from L2 drive. Here the cycle runs 5 times in total.

```ROP - 1 cycle
INC R16 - 1 cycle
ARALIK R17 - 1 cycle
INC R18 - 1 cycle
CP R17, R16 - 1 cycle
BRCC L2 - 2 cycle```

________ here 7 cycle but

Total:  7 * 5 = 35-1 = 34 cycle  (Why-1? Because last brcc is 1 cycle.)

(Why 7*5? Because The Loop runs 5 times.What's loop? There is brcc)

Grand total: 34 + 5 = 39 cycle. 2.44 how found?

processor 16 mhz; 1/16000000 * 39 = 2.44 us