Several of my ca65 projects have relied on the ability of macros to define global labels in order to allow a macro to assign a name to which other data tables can refer. For example:In [url=https://github.com/mplamann/NESEmulator/blob/master/ROMs/asm6/README.TXT]ASM6 README.txt[/url], loopy wrote:Labels defined inside macros are local (visible only to that macro).
Code: Select all
; ca65 code
.macro entry nameofthing, part1, part2, part3
nameofthing = (* - tablestart) / 4
.word part1
.byte part2, part3
.endmacro
tablestart:
hello THING_ONE, $1111,$01,$01
hello THING_TWO, $2222,$02,$02
hello THING_THREE, $3333,$03,$03
; becomes this:
tablestart:
THING_ONE = (* - tablestart) / 4 ; that is, 0
.word $1111
.byte $01,$01
THING_TWO = (* - tablestart) / 4 ; that is, 1
.word $2222
.byte $02,$02
THING_THREE = (* - tablestart) / 4 ; that is, 2
.word $3333
.byte $03,$03
Code: Select all
; ASM6 code
macro entry nameofthing, part1, part2, part3
nameofthing = ($ - tablestart) / 4
word part1
byte part2, part3
endmacro
tablestart:
hello THING_ONE, $1111,$01,$01
hello THING_TWO, $2222,$02,$02
hello THING_THREE, $3333,$03,$03
; becomes this, where the rept 1 blocks make labels defined within
; invisible to the rest of the program
tablestart:
rept 1
THING_ONE = ($ - tablestart) / 4 ; that is, 0
word $1111
byte $01,$01
endr ; symbol THING_ONE unusable outside
rept 1
THING_TWO = ($ - tablestart) / 4 ; that is, 1
word $2222
byte $02,$02
endr ; symbol THING_TWO unusable outside
rept 1
THING_THREE = ($ - tablestart) / 4 ; that is, 2
word $3333
byte $03,$03
endr ; symbol THING_THREE unusable outside
Code: Select all
; ASM6 code
macro entry nameofthing, part1, part2, part3
word part1
byte part2, part3
endmacro
tablestart:
THING_ONE = ($ - tablestart) / 4
hello THING_ONE, $1111,$01,$01
THING_TWO = ($ - tablestart) / 4
hello THING_TWO, $2222,$02,$02
THING_THREE = ($ - tablestart) / 4
hello THING_THREE, $3333,$03,$03