It is currently Sat Nov 18, 2017 11:02 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Sat Apr 29, 2017 3:10 pm 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1454
In C, there's a neat feature that lets you put together variable names.

For example, if you write:
Code:
#define NumberA 1
#define NumberB 2

#define GetNum(name) (Number##name)

and you call the macro like this:
Code:
int i = GetNum(A);
then the preprocessor will transform it into
Code:
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:
SomeVariable: .res 1
.export SomeVariable
.export _SomeVariable = SomeVariable

I want to write this:
Code:
DECLARE_GLOBAL SomeVariable, 1

And the macro shall look like this:
Code:
.macro DECLARE_GLOBAL name, size
    name: .res size
    .export name
    .export _##name = name
.endmacro

Is this possible?

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
PostPosted: Sat Apr 29, 2017 3:30 pm 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 2981
Location: Tampere, Finland
DRW wrote:
Is this possible?

Yes. This should work (not tested):

Code:
.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: kkfos.aspekt.fi


Top
 Profile  
 
PostPosted: Sat Apr 29, 2017 3:45 pm 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1454
Thanks. This works.

_________________
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
PostPosted: Sun Apr 30, 2017 2:34 am 
Offline

Joined: Tue Oct 06, 2015 10:16 am
Posts: 581
You do know that asm can use the C vars fine?

_SomeVariable: .res 1

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


Top
 Profile  
 
PostPosted: Sun Apr 30, 2017 4:59 am 
Offline
User avatar

Joined: Sat Sep 07, 2013 2:59 pm
Posts: 1454
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".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: NovaSquirrel and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group