ASM6 macro with indexer

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

Moderator: Moderators

Post Reply
User avatar
bleubleu
Posts: 99
Joined: Wed Apr 04, 2018 7:29 pm
Location: Montreal, Canada

ASM6 macro with indexer

Post by bleubleu » Mon Aug 03, 2020 8:03 am

Hi!

I'm cleaning up the FamiStudio sound engine and porting it to NESASM + ASM6.

Like most of us, I tend to abuse CA65 macros and there is one thing i have not been able to do in ASM6 : have an optional indexer passed as a parameter.

Code: Select all

.macro test offset, indexer
    lda var + offset indexer
.endmacro
Which i can then call like this:

Code: Select all

test 10
Or like this

Code: Select all

test 5, {,y}
To get different addressing mode.

Surprisingly I can get something similar to work with NESASM too, but ASM6 is the only one I have not been able to figure out.

Is it possible to get something like this to work?

-Mat

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

Re: ASM6 macro with indexer

Post by tokumaru » Mon Aug 03, 2020 8:39 am

Despite all the shortcomings, NESASM does have more features than ASM6 in general.

I don't think there's any way to pass arguments containing commas to a macro in ASM6, but seeing as there are only 2 index registers, you could probably write multiple versions of the macro.

Another solution is to specify only the index register (without the comma) and explicitly check for its presence:

Code: Select all

.macro test _offset, _indexer
  .ifndef _indexer
    lda var + _offset
  .else
    lda var + _offset, _indexer
  .endif
.endm

Code: Select all

test 10
test 5, y
I always name macro parameters with an underscore as the first character, to avoid .ifdef/.ifndef picking up symbols defined outside of the macro.

User avatar
bleubleu
Posts: 99
Joined: Wed Apr 04, 2018 7:29 pm
Location: Montreal, Canada

Re: ASM6 macro with indexer

Post by bleubleu » Wed Aug 05, 2020 7:58 pm

Thanks man. That was also my conclusion :(

In the end, I reworked my code to avoid having an indexer as a macro parameter and its probably for the best.

-Mat

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

Re: ASM6 macro with indexer

Post by tokumaru » Wed Aug 05, 2020 8:19 pm

Doesn't that macro do what you wanted, though? It's just larger due to the explicit check for the argument, but it works pretty much like the one you posted...

frantik
Posts: 332
Joined: Tue Mar 03, 2009 3:56 pm

Re: ASM6 macro with indexer

Post by frantik » Thu Aug 13, 2020 2:11 pm

i found myself wishing for this feature too.. even making a simple "set variable" macro is complicated by this. I wish we could do something like

Code: Select all

macro set _memloc, _value

	lda _value
	sta _memloc

endm

set $00, #$01
set [$00,x], #$01
set [($00,x)], [($01),y]
or some other syntax so that the comma in indexed values didn't throw off the macro interpreter



I also noticed if you try to use IF on undefined parameters inside of ifdef blocks, it doesn't work

Code: Select all

macro test _a, _b      
   ifdef _b
      if _b == #$00
         nop
      endif
   endif      
endm

test $00
gives

Code: Select all

08000                           test $00
08000                              ifdef _b
08000                                 if _b == #$00
*** Unknown label.
08000                                    nop
08000                                 endif
08000                              endif   
seems like it shouldn't be processing the if statement at all if _b isn't defined

Post Reply