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.
Jump instructions do two things :
It takes one cycle to test the condition. In your case,
brnetests 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 :
brnewill need 2 cycles the 5 first iterations becauser27is not equal to 5, and only one on the 6th, whenr27is equal to 5