nesdev.com
http://forums.nesdev.com/

using structs from C in Assembly
http://forums.nesdev.com/viewtopic.php?f=2&t=16012
Page 2 of 2

Author:  rainwarrior [ Thu Jun 01, 2017 8:01 pm ]
Post subject:  Re: using structs from C in Assembly

Yeah, that builds.

Author:  gauauu [ Thu Jun 01, 2017 10:12 pm ]
Post subject:  Re: using structs from C in Assembly

rainwarrior wrote:

You could also make a separate structure definitions header file with just the C or assembly structures in it and write a little parser utility to convert it to the other. You don't need a very complicated parser just to translate a struct definition.


That would be my recommendation. Have your makefile (or whatever build system) run a script to auto-create the assembly struct file from the C.

Author:  DRW [ Fri Jun 02, 2017 1:09 am ]
Post subject:  Re: using structs from C in Assembly

A parser would be a bit overkill for me. But maybe I'll give it another try to create a code that can be parsed on the source level for both languages.

I'm already using this for constant values that I need in Assembly and in C:

Code:
#ifdef COMPILE_FOR_ASSEMBLY
#define DeclareConstant(name, value) name = value
#else
#define DeclareConstant(name, value) enum { name = value };
#endif


Maybe I can do a similar thing for the struct:
Code:
#ifdef COMPILE_FOR_ASSEMBLY
#define TypeByte byte
#define TypeUint word
#define DeclareStructStart(name) .struct name
#define DeclareStructEnd() .endstruct
#define DeclareMember(type, name) name: .type
#else
#define TypeByte unsigned char
#define TypeUint unsigned int
#define DeclareStructStart(name) struct name {
#define DeclareStructEnd() };
#define DeclareMember(type, name) type name;
#endif

DeclareStructStart(Character)
    DeclareMember(TypeByte, Energy)
DeclareStructEnd()

etc.

And then you call cc65 with that one specific command line parameter that only preprocesses the file instead of compiling it, so that it outputs a valid Assembly file if you define COMPILE_FOR_ASSEMBLY.
This file can be used in your code then. (The C file itself can be used anyway since it is already a valid C file if you don't define COMPILE_FOR_ASSEMBLY.)

Page 2 of 2 All times are UTC - 7 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/