This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Put a label into a Macro.

Put a label into a Macro.
by on (#227788)
Hello to everyone,
I'm implementing a Macro that put an enemy on the screen in this way:

Code:
MACROPutEnemyOnScene .macro
  lda $2002             
  lda #$20
  sta $2006             
  lda #$00
  sta $2006

  ldy #$00
LoadTargetSpriteLoop:
  lda \1, y
  sta \2, y
  iny
  cpy #$08
  bne LoadTargetSpriteLoop
  .endm


And i use it like this:

Code:
lda #LOW( target_up )
  sta TARGET_TO_LOAD + 0
  lda #HIGH( target_up )
  sta TARGET_TO_LOAD + 1

  MACROPutEnemyOnScene [TARGET_TO_LOAD], RAM_TARGET_UP



The problem, if I try to use the macro again, for example:

Code:
lda #LOW( target_down )
  sta TARGET_TO_LOAD + 0
  lda #HIGH( target_down )
  sta TARGET_TO_LOAD + 1

  MACROPutEnemyOnScene [TARGET_TO_LOAD], RAM_TARGET_DOWN


The compiler give me an error of duplicate label, it seems it "copy" the label everytime the macro is called. There is a way to put "generic" label or something like that to use the macro with a label inside? Because macros allow me to reduce a lot of duplicate code.

Thanks.
Re: Put a label into a Macro.
by on (#227790)
Which assembler is this for? The answer might be in the assembler's man page. In ca65, you can use .scope or .local to create macro-local labels, but this doesn't look like ca65.
Re: Put a label into a Macro.
by on (#227791)
tepples wrote:
Which assembler is this for? The answer might be in the assembler's man page.


NesAsm 3.1 .
Re: Put a label into a Macro.
by on (#227792)
The documentation ( https://github.com/thentenaar/nesasm/bl ... /usage.txt ) say:

Symbols
-------

Two types of symbol are supported, global symbols and local
symbols. Local symbols are preceded by a dot '.' and are valid
only between two global symbols. A symbol can be followed by
a colon ':' but this is not necessary.
Re: Put a label into a Macro.
by on (#227793)
If I put a dot before the label, it seems working:

Code:
MACROPutEnemyOnScene .macro
  lda $2002             
  lda #$20
  sta $2006             
  lda #$00
  sta $2006

  ldy #$00
.LoadTargetSpriteLoop:
  lda \1, y
  sta \2, y
  iny
  cpy #$08
  bne .LoadTargetSpriteLoop
  .endm
Re: Put a label into a Macro.
by on (#227794)
From usage.txt in camsaul/nesasm:
\@
Special parameter that returns a different number for each macro; can be used to define local symbols inside macros:
Code:
abs .macro
  lda \1
  bpl .x\@
  eor #$FF
  clc
  adc #1
  sta \1
.x\@:
  .endm


It turns out to be the same syntax used in RGBASM, which targets the Game Boy CPU. From man 5 rgbasm:
To get around this problem there is a special label string equate called \@ that you can append to your labels and it will then expand to a unique string. \@ also works in REPT-blocks should you have any loops there.
Code:
LoopyMacro: MACRO
  xor a,a
.loop\@
  ld [hl+],a
  dec c
  jr nz,.loop\@
  ENDM
Re: Put a label into a Macro.
by on (#227825)
I'd strongly advise switching to ASM6 (or ASM6f if you need Mesen labels for debugging), syntax is much simpler (NESASM does several things very weirdly compared to most assemblers).
Re: Put a label into a Macro.
by on (#227891)
Ok, where I can find some documentation about ASM6 and Nes?
Re: Put a label into a Macro.
by on (#227895)
https://github.com/freem/asm6f

Checkout readme-original.txt
Re: Put a label into a Macro.
by on (#227900)
I think he meant ASM6 applied specially to NES programming.