The number of cycles it takes to execute some code can be calculated by adding together the number of cycles it takes to execute each instruction times the number of times that instruction is executed. This is probably clearest using an example.
In the following example, the comments try to act like a table showing
how to calculate the cycle count. Cycles
is the number
of cycles it takes to execute the instruction, while
Times
is the number of times the instruction is executed,
and Total
is Cycles * Times
.
* Code Cycles Times Total
*---------------------------------------------
ldab #9 2 1 2
loop decb 2 9 18
bne loop 3 9 27
* --
* 47
so the code takes a total of 47 cycles to execute.
It's a bit harder when the loops are nested. Let's take that example, and nest it inside another example.
* Code Cycles Times Total
*---------------------------------------------
ldaa #8 2 1 2
oloop
ldab #9 }
loop decb }47 8 376
bne loop }
deca 2 8 16
bne oloop 3 8 24
* --
* 418
Notice that our original loop is being considered here as if it were an instruction that takes three lines in the listing, and 47 cycles to execute. This "instruction" is executed 8 times, for a total of 376 cycles. There are three more instructions in the code; one that's executed one time for two cycles, and two that are executed eight times each (one takes two cycles, and one takes three). The total of all that is 418 cycles.