AVR Assembly Clock Cycle

36 views Asked by At

I will calculate the time delay so I need clock cycle number. What is the clock cycle in this code total?

.ORG 0
LDI R20, 200   ; Load immediate value 200 into register R20
BACK:
  LDI R25, 120 ; Load immediate value 120 into register R25
  NOP          ; No operation
  NOP          ; No operation
  NOP          ; No operation
HERE:
  DEC R25      ; Decrement R25
  BRNE HERE    ; Branch to HERE if R25 is not zero
  DEC R20      ; Decrement R20
  BRNE BACK    ; Branch to BACK if R20 is not zero
LDI R22, 0xFF  ; Load immediate value 0xFF into register R22

I understand the LDI, NOP,DEC instructions have 1 clock cycle each time and depend on their loops, but I confused BRNE instructions.

1

There are 1 answers

0
Olivier On

Jump instructions do two things :

  1. Testing the condition
  2. Jumping to the branch if the condition is true.

It takes one cycle to test the condition. In your case, brne tests the 'Zero Flag'. Then, if the test is true, it takes one more cycle to jump to the label. Otherwise, the program continues to the next instruction.

Example from the AVR Instruction Set Manual :

eor r27,r27 ; Clear r27
loop: inc r27 ; Increase r27
...
cpi r27,5 ; Compare r27 to 5
brne loop ; Branch if r27<>5
nop ; Loop exit (do nothing)

brne will need 2 cycles the 5 first iterations because r27 is not equal to 5, and only one on the 6th, when r27 is equal to 5