Getting rid of "Relative branch out of reach" errors

Jump to: navigation, search

While I'm modifying existing assembler code, the AVR Assembler 2 tends to spit out the infamous "error: Relative branch out of reach" at unexpected places. This is annoyingly distracting, as I'm usually focused somewhere else at the time those errors arise.

I could of course fix them by reorganising the code or by rewriting the affected jump conditions, but to do so, I would have to mentally "switch context" and modify some unrelated old code which has proved to work well, and then I need to retest it to check I haven't made any new mistakes.

Most of the time my personal productivity is far more important than code size or speed, and, in order to avoid such distractions, I have written the following assembler macros. Every time I get such pesky compilation errors, I just mindlessly substitute the offending brXX instruction with their jXX counterpart, and the assembler will be happy again. The macros leave little room for errors. After I'm done with the main changes, I can optimise the jXX away, or leave them in place if neither size nor speed are important in that particular code region.

<c> .macro jeq

   brne pc+2
   rjmp @0

.endm

.macro jne

   breq pc+2
   rjmp @0

.endm

.macro jlo

   brsh pc+2
   rjmp @0

.endm

.macro jsh

   brlo pc+2
   rjmp @0

.endm </c>