Continue to Site

# Any way to find out the machine cycles taken each instruction in Keil written in C

Status
Not open for further replies.

#### thannara123

Hello ,
Any way to find out the machine cycles taken each instruction in written in C (Keil) ?

thanks

#### sky_123

Hi,

You can probably enable assembler listing generation (I don't use Keil, but I'm sure it must be possible).
Interspersed with code, it will look like something like this:
Code:
      9            unsigned int i;
10            for (i=0; i<30000; i++)
\                     mydelay:
\   00000000   0x....             LDR      R0,??DataTable1  ;; 0x7530
11            {
12              __no_operation();
\                     ??mydelay_0:
\   00000002   0x46C0             Nop
13            }
\   00000004   0x1E40             SUBS     R0,R0,#+1
\   00000006   0xD1FC             BNE      ??mydelay_0
14          }
\   00000008   0x4770             BX       LR               ;; return
Then, you can directly look up the machine instructions in the specific microcontroller data sheet, to
see how long they take to execute.

#### Jigar 4 Electronics

##### Full Member level 5
after you click on debug in keil, a sub window will be there in background of the main program window.
main program Tab has program in C, & other Tab will be of ASM language... that will be equivalent to the C program & so we can
judge the no. of cycles for 1 instruction of C by step execution.
But I've experienced that if we have used 3 for loop (even if all of them are same !) all of them will occupy diff. no. of Machine Cycles. (Don't know why !)

thannara123

### thannara123

Points: 2

#### thannara123

thanks for replay ?
one doubt How to calculate how much cycles take each instruction ?
is it according with bit operation ? please can you explain how it ?

for example take from the picture

what is the relation between bit operation and cycles ?

Last edited:

#### sky_123

You can see from your screenshot that the "P2=0x00" command was translated into two instructions:
CLR A
MOV PPAGE_SFR(0x00),A

Then, you can see that the "P3=0x00" command was translated to one instruction:
MOV P3(0xB0),A

You can check the microcontroller data sheet to see how many clock cycles these instructions take. Maybe it is 1 clock cycle each for a PIC (I don't know).
Then, you can check the microcontroller clock cycle period (depends on your crystal) and multiply by the number of clock cycles.
So, the P2=0x00 command took twice as long as the P3=0x00 command, because the "CLR A" instruction was used for both (it basically sets register A to zero).
A total of 3 machine instructions for the two source code lines. So, for those two source code lines, the amount of time was 1.5 machine instructions each.

thannara123

### thannara123

Points: 2

#### Tahmid

Isn't there a stopwatch in Keil that you can use to move through the code while debugging and check the time taken for each instruction written? I haven't used Keil, so I don't know. But a stopwatch is present in many other IDEs.

Points: 2

### sky_123

Points: 2

#### thannara123

Yes right that is am searching ? thanks Thamid for the information .

#### js

##### Full Member level 5
yes!
"Registers" window have all timings:
cycles, 0.12345678us, .....

p.s.
only if some of micro isn't supported 100% at peripheral level this data can't be viewed!

thannara123

### thannara123

Points: 2

#### alexan_e

There is a stopwatch at the lower right corner of uvision

There is also a cycle counter in the left side panel

Points: 2

Points: 2

### Jigar 4 Electronics

Points: 2

#### sky_123

Ah, that would be the easier way! I didn't realise this existed.

#### kodi.sudar

##### Member level 5
There is a stopwatch at the lower right corner of uvision
View attachment 84155

There is also a cycle counter in the left side panel
View attachment 84156

I heard that it is possible to use logic analyzer to measure the execution time in Uvision4 . but i don't know to verify that any idea to achieve that ?

Thanks

- - - Updated - - -

There is a stopwatch at the lower right corner of uvision
View attachment 84155

There is also a cycle counter in the left side panel
View attachment 84156

I heard that it is possible to use logic analyzer to measure the execution time in Uvision4 . but i don't know to verify that any idea to achieve that ?

Thanks