In: Computer Science
The following program is to have a time delay. Calculate and tell how much the time delay is. The CPU frequency is 24-MHZ.
org $1000 ; starting address of the program
ldx #80000 ; 2 E cycles
loop psha ; 2 E cycles
pula ; 3 E cycles
psha ;
pula ;
psha ;
pula ;
psha ;
pula ;
psha ;
pula ;
psha ;
pula ;
nop ; 1 E cycle
nop ;
dbne x,loop; 3 E cycles
swi
end
ldx #80000
loop psha ; 2 E cycles
pula ; 3 E cycles
psha ; 2 E cycles
pula ; 3 E cycles
psha ; 2 E cycles
pula ; 3 E cycles
psha ; 2 E cycles
pula ; 3 E cycles
psha ; 2 E cycles
pula ; 3 E cycles
psha ; 2 E cycles
pula ; 3 E cycles
psha ; 2 E cycles
pula ; 3 E cycles
nop ; 1 E cycle
nop ; 1 E cycle
dbne x,loop ; 3 E cycles
dbne x,loop
To delay by 100 millisec, that is 24,000,000 * 100 / 1,000 EClocks, namely 2,400,000 instruction cycles.
The maximum register size available is 16-bits, so a loop counter value is chosen that is <= 65535.
Conveniently 60,000 is a factor of 2,400,000 being 60,000 * 40. So the inner loop is contrived to take 40 cycles.
Giving the required 40 cycles execution time.if you have interrupts, other processes, this hard coded method is not very accurate, and a timer interrupt would be better.
Note: If you satisfy with answer then give a upvote to answer and for any query please mention in comment.