Put a label into a Macro.

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
User avatar
kikutano
Posts: 114
Joined: Sat May 26, 2018 6:14 am
Location: Italy

Put a label into a Macro.

Post by kikutano » Fri Oct 19, 2018 8:29 am

Hello to everyone,
I'm implementing a Macro that put an enemy on the screen in this way:

Code: Select all

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: Select all

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: Select all

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.

tepples
Posts: 22044
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Put a label into a Macro.

Post by tepples » Fri Oct 19, 2018 8:35 am

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.

User avatar
kikutano
Posts: 114
Joined: Sat May 26, 2018 6:14 am
Location: Italy

Re: Put a label into a Macro.

Post by kikutano » Fri Oct 19, 2018 8:36 am

tepples wrote:Which assembler is this for? The answer might be in the assembler's man page.
NesAsm 3.1 .

User avatar
kikutano
Posts: 114
Joined: Sat May 26, 2018 6:14 am
Location: Italy

Re: Put a label into a Macro.

Post by kikutano » Fri Oct 19, 2018 8:38 am

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.

User avatar
kikutano
Posts: 114
Joined: Sat May 26, 2018 6:14 am
Location: Italy

Re: Put a label into a Macro.

Post by kikutano » Fri Oct 19, 2018 8:40 am

If I put a dot before the label, it seems working:

Code: Select all

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

tepples
Posts: 22044
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Put a label into a Macro.

Post by tepples » Fri Oct 19, 2018 8:43 am

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: Select all

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: Select all

LoopyMacro: MACRO 
  xor a,a 
.loop\@
  ld [hl+],a 
  dec c 
  jr nz,.loop\@ 
  ENDM

darkhog
Posts: 192
Joined: Tue Jun 28, 2011 2:39 pm

Re: Put a label into a Macro.

Post by darkhog » Fri Oct 19, 2018 3:55 pm

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).

User avatar
kikutano
Posts: 114
Joined: Sat May 26, 2018 6:14 am
Location: Italy

Re: Put a label into a Macro.

Post by kikutano » Sun Oct 21, 2018 6:34 am

Ok, where I can find some documentation about ASM6 and Nes?

User avatar
pubby
Posts: 557
Joined: Thu Mar 31, 2016 11:15 am

Re: Put a label into a Macro.

Post by pubby » Sun Oct 21, 2018 10:59 am

https://github.com/freem/asm6f

Checkout readme-original.txt

User avatar
tokumaru
Posts: 11858
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Put a label into a Macro.

Post by tokumaru » Sun Oct 21, 2018 1:18 pm

I think he meant ASM6 applied specially to NES programming.

Post Reply