This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Sharing defines with sizeof() for assembler???

Hi all!

I want to use some sizes of C-structures in assembler. I know that it is possible to use #define statements also in the assembler language, but not something like that:

#define STRUCT_SZ sizeof(TStruct)

Does anybody known an workaround for this simple problem?

Thanks.
Benny

  • The inability to use sizeof() is really only a symptom of a considerably harder problem, I suspect. That real problem is that you cannot use C structs at all from assembly. The assembler not only lacks understanding of their size, it doesn't know anything about structured data types, period. It doesn't understand C struct layout, padding or anything else about structs.

    What all this means is that you have to choose between essentially these two options:

    1) Don't use structs if you want to interface to assembly.

    2) Hardcode not just the sizeof(), but also the offsetof() values for elements of the struct in the C-to-ASM interface header files.

    For case 2), prudence dictates you must check back that the C compiler agress with what you hardcoded. To do that, the usual method would be to have lots of statements like this, in the home C module of that struct:

    #include <assert.h>
    #include <stddef.h>
    #include "c_to_asm_interface.h"
    
    /*...*/
    assert(sizeof(struct foo)==SIZEOF_STRUCT_FOO);
    assert(offsetof(element1, struct foo)==OFFSET_OF_STRUCT_FOO_ELEMENT1);
    assert(offsetof(element2, struct foo)==OFFSET_OF_STRUCT_FOO_ELEMENT2);
    

  • I s'pose another option would be for the assembler to only ever touch the structures in C-called functions, so that 'C' could provide the appropriate address & length at runtime?