SlithyMatt / snes-hello

Hello, World for the SNES
GNU General Public License v3.0
22 stars 9 forks source link

[Question] why do some labels begin with @ #3

Closed jeffythedragonslayer closed 2 years ago

jeffythedragonslayer commented 2 years ago

I notice that some labels in hello.asm begin with @ and some don't. What does this mean?

jeffythedragonslayer commented 2 years ago

This likely indicates relative addressing which is probably why there are two different kinds of labels used, a BRA (near) is 2 bytes and a JMP (long) takes 4 bytes, thanks to Emil Dotchevski

SlithyMatt commented 2 years ago

That is a syntax specifically for ca65, which is called a "cheap local". It's a way to provide scoping for labels so that they don't have to be globally unique. Cheap locals are only addressable between pairs of global labels (without the @).

jeffythedragonslayer commented 2 years ago

Ah, because the instructions to jump to them are cheaper

SlithyMatt commented 2 years ago

Not necessarily, it could still require an absolute jump to them, but it's "cheaper" in terms of the namespace, meaning a cheap local label doesn't use up another label from the global namespace. So, you can have only one "loop", but you can have an "@loop" inside any scope:

foo:
   ldx #5
@loop:
   inc counter
   dex
   bne @loop
   rts

bar:
   ldx #6
@loop:
   dec counter
   dex
   bne @loop
   rts