Connecting variable names in macros

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

Moderator: Moderators

Post Reply
User avatar
DRW
Posts: 1914
Joined: Sat Sep 07, 2013 2:59 pm

Connecting variable names in macros

Post by DRW » Sat Apr 29, 2017 3:10 pm

In C, there's a neat feature that lets you put together variable names.

For example, if you write:

Code: Select all

#define NumberA 1
#define NumberB 2

#define GetNum(name) (Number##name)
and you call the macro like this:

Code: Select all

int i = GetNum(A);
then the preprocessor will transform it into

Code: Select all

int i = NumberA;
Now, I want to do something similar in Assembly with cc65:

I'd like to create a macro to declare variables that have to be exported for Assembly and for C.

So, instead of this:

Code: Select all

SomeVariable: .res 1
.export SomeVariable
.export _SomeVariable = SomeVariable
I want to write this:

Code: Select all

DECLARE_GLOBAL SomeVariable, 1
And the macro shall look like this:

Code: Select all

.macro DECLARE_GLOBAL name, size
    name: .res size
    .export name
    .export _##name = name
.endmacro
Is this possible?
Available now: My game "City Trouble".
Sales website: https://megacatstudios.com/products/city-trouble
Gameplay: https://youtu.be/Eee0yurkIW4
Download website: http://www.denny-r-walter.de/city.htm

User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: Connecting variable names in macros

Post by thefox » Sat Apr 29, 2017 3:30 pm

DRW wrote:Is this possible?
Yes. This should work (not tested):

Code: Select all

.macro DECLARE_GLOBAL name, size
    name: .res size
    .export name
    .export .ident(.sprintf("_%s", .string(name))) = name
.endmacro
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi

User avatar
DRW
Posts: 1914
Joined: Sat Sep 07, 2013 2:59 pm

Re: Connecting variable names in macros

Post by DRW » Sat Apr 29, 2017 3:45 pm

Thanks. This works.
Available now: My game "City Trouble".
Sales website: https://megacatstudios.com/products/city-trouble
Gameplay: https://youtu.be/Eee0yurkIW4
Download website: http://www.denny-r-walter.de/city.htm

calima
Posts: 1047
Joined: Tue Oct 06, 2015 10:16 am

Re: Connecting variable names in macros

Post by calima » Sun Apr 30, 2017 2:34 am

You do know that asm can use the C vars fine?

_SomeVariable: .res 1

Sure, it's an extra _ when using it in asm.

User avatar
DRW
Posts: 1914
Joined: Sat Sep 07, 2013 2:59 pm

Re: Connecting variable names in macros

Post by DRW » Sun Apr 30, 2017 4:59 am

Yes, I know. But I prefer clean variable names. That's why I always declare them with a regular name and only export them with the underscore.
Also, when I notice that a certain variable doesn't need to be exported to C and therefore doesn't need an underscore, I don't have to rename every occurence of it.
Available now: My game "City Trouble".
Sales website: https://megacatstudios.com/products/city-trouble
Gameplay: https://youtu.be/Eee0yurkIW4
Download website: http://www.denny-r-walter.de/city.htm

Post Reply