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

2 Comments

  1. Stefan Arentz
    Posted August 5, 2008 at 1:40 am | Permalink | Reply

    This is not correct. The last two examples produce exactly the same code and have exactly the same timing.

  2. thandle
    Posted August 5, 2008 at 2:07 pm | Permalink | Reply

    I know. I never said anything about timing. What I meant is that it’s better to use ‘brne PC-0x01’ if you only need to step one instruction back, instead of putting in another label. In the latter case, you might wonder later: “Am I jumping/branching here from any other place, too?”

    In other words, it’s about clearer coding, style. I do know what a disassembler is. ;D

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: