The C compilation doesn't appear to use .struct in its generated code, so there's no automatic pass-through of the definition, but you could maybe make macros for C and assembly that let you include the same header in C and assembly at the same time?

Duplicating a struct would require manual synchronization between the two definitions just as well.I would have to declare a struct in C and the same struct in Assembly again and pay attention that every little change is carried over from one struct to the other one.

I'm talking about a way of importing the separate members of a C struct into Assembly.

If I have a struct called Character and the character has a member named Energy and I have a global variable called Player, then I want to be able to use something like Player.Energy inside Assembly and let the compiler/linker find out what actual address this is.

Alternately, I would also be content with building something with the help of C preprocessors etc.For example, if I could do something like this in C:

Code:

#define PlayerEnergy (&Player.Energy)

and then import this in Assembly:

Code:

.import _PlayerEnergyLDA _PlayerEnergy

this would be fine as well.

However, something like this:

Code:

byte *const PlayerEnergy = &Player.Energy;

(or however a pointer has to be declared whose address is fixed, but whose value can be written)would not be good.Because not only does the pointer occupy actual space in ROM, but in Assembly, I would have to use it this way:

Well, if you don't like the macro suggestion I made in my first post...

You can export some structure member location from your assembly file and use asserts to make sure they are equivalent to the redundant structure definition. You'd have two definitions to maintain but you'd know immediately when they don't match.

Well, I can try the macro version and try to build something with it. But everything else isn't really that elegant.

Actually, this would have been a task for the compiler: Exporting the offsets for struct members.But the inline assembly doesn't even allow statements like ".export".

Is there some command in C that forces the compiler to include a literal statement which is ignored when checking the C syntax, but that gets written into the Assembly file as-is?Similar to inline-assembly, only that it isn't checked for correct syntax by the C compiler and only gets checked when the assembly code gets handled.Something like this:

I don't think C has the capability to export a constant like that. Anything that's not static is automatically exported, but I think all exports are either addresses of variables or functions, so I can't think of a way to pass a particular constant directly to the linker for use via assembly.

Yeah, I try to avoid these kinds of things: Storing values into the DATA or RAM segment that could usually be used directly. Or using the X or Y register for values that are known at compile time.

After all, my problem is really just a code style issue. And I don't like to complicate the behavior of the program, just so that I can enforce a certain style on the source code level.(I'd rather abolish the struct altogether and simply use a bunch of separate variables. At least this would be a workaround purely on the code level while the ROM would still work exactly the same.

Also, some of my structs are not just structs, but structs with arrays inside (as a quicker alternative to an array of structs). So, the X or Y register is often already needed for the actual array index. Therefore, using it for an offset that's known at compile-time is not only ugly, but pretty much impossible.

I solved my current problem by writing the function inside C, but with 100 % inline assembly. This way, I can use the member names to turn their offsets into the literal values, like in the example from the compiler documentation:

About using the same header file: This could be quite a hassle, right?

I mean, how do you create a file that looks like this:

(example omitted)

Thinking about it some more, I believe it's doable but the fact that ca65 macros have no () enclosing parameters would make any attempted solution a little hairy.

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.

Who is online

Users browsing this forum: No registered users and 4 guests

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