Tag Archives: assembly

Atmel’s assembly: branch to address, not label

Say you had this code to wait for n iterations, where n is stored in register arg. You could write:

wait:
dec arg
brne wait ; step back to dec
ret

Which would be OK. But if you didn’t have that much free registers, you would probably pass n using the stack. In which case your procedure would grow intensely fucked up, like this:

wait:
; (line/s to get argument from stack)
; ...
wait_dec:
dec arg
brne wait_dec ; step back to dec
ret

This ain’t good. Instead, branch to a relative adress:

wait:
; (line/s to get argument from stack)
; ...
dec arg
brne PC-0x01 ; step back to dec
ret

Advertisements