AVR Tutorials
The Best AVR Microcontroller Tutorials on the Web

Calculating Execution Time for Sequential Code


This AVR tutorial will explain the procedure to follow in calculating the execution time for a piece of AVR assembly code which does not contain any loops.

Procedure in Calculating Execution Time

To calculate the execution time (ET) for a piece of AVR assembly code you will need the following information:
  • The clock frequency of the AVR microcontroller the code is running on.
  • The information sheet that tells the number of cycle each instruction requires to execute.

Once you have the above information follow the steps below to calculate the execution time:

Steps to caculate ET

  1. Determine the period of the microcontroller clock using the equation:

    Period (T) = 1/Frequency(f)

  2. Determine the total number of cycle (C) to execute the piece of AVR assembly code given. This is done by determining the number of cycle for each instruction to execute in the code and adding them. The number of cycle each instruction takes to execute is found in the datasheet for the microcontroller. See AVR Instruction Set Summary.
  3. Calculate the execution time for the piece of assembly code using the the following equation:

    ET = T * C

Example Question & Solution


Calculate the execution time for the following piece of assembly code given that the code will be running on an Atmel AVR 8-bit micro-controller being clocked by a 4MHz oscillator.

                  LDI   R16, 0xFF 
                  LDI   R17, 0xEE
                  ADD   R17, R16	
                  MOV   R20, R17


Step 1 - Determine the Period (T)

T = 1/f = 1/4*106 = 0.250µs

Step 2 - Determine Total Number of Cycles for execution (C)

;                 Instruction                         # cycles to execute
                   LDI    R16, 0xFF                    ;            1
                   LDI    R17, 0xEE                    ;            1
                   ADD    R17, R16                     ;            1
                   MOV    R20, R17                     ;            1 
;                  _______________________________________________________
;                  Total # of cycles for execution (C) =            4 

Step 3 - Calculate Execution Time (ET) using information from steps 1 & 2

ET = C * T = 4 * 0.250 = 1µs

AVR Tutorials hopes this AVR assembly tutorial on calculating execution time was beneficial to you and looks forward to your continued visits for all your microcontroller tutorial needs.