Calculating Execution Time for Sequential Code

Introduction


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



Question

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


Solution

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


Calculating Execution Time for Code with Single Loop

Introduction


This AVR tutorial will explain the procedure to follow in calculating the execution time for a piece of AVR assembly code which contains only single loops.


It is recommended to complete AVR tutorial on Calculating Execution Time for Code without Loops before going through this tutorial.

Procedure in Calculating Execution Time


The procedure to calculate the execution time (ET) for a piece of AVR assembly code containing single loops require a bit more. You still 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 calculate 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 code given. This is done by determining the number of cycle for each instruct to execute in the code and adding them. The number of cycle each instruction takes to execute is found in the datasheet for the micro-controller. This require abit more work than in tutorial ET1. (see example below)
  3. Calculate the execution time for the piece of assembly code using the the following equation:
    ET = T * C

Example Question & Solution



Question

Calculate the execution time for the following piece of AVR 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, 5 
Again: DEC R16
NOP
BRNE Again
NOP
NOP


Solution

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, 5 ; 1
Again: DEC R16 ;-----|
NOP ; | A
BRNE Again ;-----|
NOP ; 1
NOP ; 1
; _______________________________________________________
; Total # of cycles for execution (C) = 3 + A
 
 
The total number of cycles for the execution of the code is given as (3 + A). In calculating the total number of cycles for execution, in this case, it must be taken into consideration that the block of code labeled A is executed more than once because it forms a loop.
What is done here is that we look at the block of code which construct the loop as a single instruction for which we will determine the number of cycle for execution next.
Calculating the number of cycles for block A
;                   Instruction                         # cycles to execute
Again: DEC R16 ;-----| 1
NOP ; | 1
BRNE Again ;-----| 2
; _______________________________________________________
; Total # of cycles for execution of A = 5*(1+1+2) - 1
 
 
In this code the loop is controlled by the value in R16 which is 5. So the code in block A is done 5 times. This result in C = 3 + A = 3 + [5(1+1+2) -1] = 22cycles.
One important point to note here is that the BRNE instruction takes 2 cycle to execute when it is true and 1 cycle when the condition is false. So for the 5 times the BRNE instruction is executed 1 time the condition is false which is why we subtract 1.

Step 3 - Calculate Execution Time (ET) using information from steps 1 & 2
ET = C * T = 22 * 0.250 = 5.5µs

Calculating Execution Time for Code with Double Loop

Introduction


This AVR tutorial will explain the procedure to follow in calculating the execution time for a piece of AVR assembly code which contains double loops.

Procedure in Calculating Execution Time


The procedure to calculate the execution time (ET) for a piece of AVR assembly code containing double loops is simillar to that of calculating for single loops. As such it is recommeded that you first complete the tutorial onCalculating Execution Time for Code with Single Loops.

Example Question & Solution



Question

Calculate the execution time for the following piece of AVR assembly code given that the code will be running on an Atmel AVR 8-bit microcontroller being clocked by a 4MHz oscillator.
                 LDI   R16, 5 
Again: LDI R17, 4
Here: DEC R17
NOP
BRNE Here
DEC R16
NOP
BRNE Again
NOP
NOP


Solution

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, 5 ; 1
Again: LDI R17, 4 ;---------|
Here: DEC R17 ;---| |
NOP ; |B |
BRNE Here ;---| | A
DEC R16 ; |
NOP ; |
BRNE Again ;---------|
NOP ; 1
NOP ; 1
; _______________________________________________________
; Total # of cycles for execution (C) = 3 + A
The total number of cycles for the execution of the code is given as (3 + A). In calculating the total number of cycles for execution, in this case, it must be taken into consideration that the blocks of code labeled A and B are executed more than once because they form loops.
What is done here is that we look at the block of code which construct the loops as a single instruction for which we will determine the number of cycle for execution next.
Calculating the number of cycles for block A
;               Instruction                         # cycles to execute
Again: LDI R17, 4 ; 1
Here: DEC R17 ;---|
NOP ; | B
BRNE Here ;---|
DEC R16 ; 1
NOP ; 1
BRNE Again ; 2
; _______________________________________________________
; Total # of cycles for execution (C) = 5*(1+B+1+1+2) - 1
In this code the loop is controlled by the value in R16 which is 5. So the code in block A is done 5 times. This result in the number of cycles to execute code block A = 5(1+B+1+1+2) - 1= (5B + 24)cycles.
One important point to note here is that the BRNE instruction takes 2 cycle to execute when it is true and 1 cycle when the condition is false. So for the 5 times the BRNE instruction is executed 1 time the condition is false which is why we subtract 1.
Calculating the number of cycles for block B
;               Instruction                         # cycles to execute
Here: DEC R17 ; 1
NOP ; 1
BRNE Here ; 2
; _______________________________________________________
; Total # of cycles for execution (C) = 4*(1+1+2) - 1
In this code the loop is controlled by the value in R17 which is 4. So the code in block B is done 4 times. This result in number of cycle to execute code block B = 4*(1+1+2) - 1 = 15cycles . This futher implies that C = 3 + [5(15) + 24] = 102cycles.
Step 3 - Calculate Execution Time (ET) using information from steps 1 & 2
ET = C * T = 102 * 0.250 = 25.5µs

Verifying the Calculated Execution Time

Introduction


This AVR tutorial will explain how to use the simulation capability of AVR Studio 4 to verify the calculated execution time.
For this AVR tutorial we will be using the AVR assembly code from the Calculating Execution Time for Code with Double Loops tutorial.

Steps to verify your calculation with AVR Studio


  1. Open AVR Studio 4 and create a new AVR assembly project.
  2. Type the code in the editor and add the following line at the end of the code:
    End:             RJMP    End
     
    The entire code in the AVR Studio editor should be similar to that below:
                     LDI   R16, 5 
    Again: LDI R17, 4
    Here: DEC R17
    NOP
    BRNE Here
    DEC R16
    NOP
    BRNE Again
    NOP
    NOP
     
    End: RJMP End
  3. Go to the Build menu in the AVR Studio 4 and select Build and Run. You should get a screen looking like the one below:
    AVR Studio is now in simulation mode notice the yellow arrow, which indicates the microcontroller is about to execute this line of code.
    Also notice the 'Processor' window pane to the left, which indicates the value of the Cycle Counter, Frequency, Stop Watch, etc. At this point the Cycle Counter and Stop Watch is at 0, indicating that no instruction as been executed. The Frequency for this simulation is set to 4MHz.
  4. Position the cursor at the beginning of the line "End: RJMP End". Then go to the Debug menu and selectRun to Cursor.
    The simulator will now execute all the instruction up to where the cursor is located. You should now see an update 'Processor' window pane showing the number of cycles that has passed to execute up to this point in the code, given by Cycle Counter.
    We are interested in the Stop Watch value which gives the time taken to execute the instructions up to this point in the code. That is the Execution Time for our code.

STAY CONNECTED

projectbandya

No comments:

Post a Comment