avr-rust / delay

arduino-like delay routines based on busy-wait loops
Apache License 2.0
15 stars 11 forks source link

Correct rjmp address (fix infinite loop) #37

Closed bombela closed 2 years ago

bombela commented 2 years ago

Until now I had not ran the code. I merely verified that the assembly output was what I expected.

Because rjmp on AVR always jump at the given address plus one instruction (ie: plus 2 bytes). My reasoning was that rjmp . means to jump at the address of rjmp, and the hardware will add two, therefore it will jump to the next instruction.

But of course this is a silly assumption. The assembler (gas) knows about this rule. And it will actually subtract two from any constant passed to rjmp during assembly. That is the point of the assembler. Hiding those little details.

It is interesting to note that disassembling the machine code would have showed the problem by representing the jump under a different form.

The updated code is being used on real hardware (atmega168 if that matters). It appears to work as intended, timing and all.

https://sourceware.org/binutils/docs/as/Dot.html.