Variables in Assembler

I am in the process of separating a C program into separate files to make it easier to read.

I have therefore collected up all the variable declarations and definitions and put them in a separate file.

If I just keep the standard declarations, the compiler gives a “multiple definition of” error for each one whenever it is used.

if I declare them all as EXTERN then the C compiler is happy, but AS can no longer see them, so the assembler generates errors.

How can I declare and define the variables so that both GCC and GAS can see them, and also have my program divided up into separate files for ease of understanding?

  • I am a little confused about what you are doing here, are you wanting to divide C-files into both ASM and C-files?  Or are you saying that the assembly stage produces an error from the generated C-code?

    Could you provide a simple example of the original code, and the attempted division of that code?

  • C isn't my first language - I've been programming in assembler since before anyone even thought of writing a C compiler for a microcontroller,

    I have a long program in C, all 2000 lines of it inserted in hal_entry.c where it says "write your code here", and I have some functions that are written in assembler that are called by the C program (generally I write something in assembler when I can't work out how to write it in C).

    There are a lot of global variables. Some of the Assembler routines access the global variables,

    Now I want to tidy it all up, and make it into separate files, but I don't really know how.

    I'm  tempted to put the new files in a separate directory where the compiler doesn't see them and #include them where they need to go, but I know that's not the proper way to do it (although I bet it will work).

  • That part that is confusing me is that if the assembly code worked before, what changed and why wouldn't it work now?

    Using the GNU tools, the easiest way to share variables between C & ASM is to use the inline assembler, this provides support for variable access and calling convention type checking, and allows the compiler to allocate the registers needed.

    If you use separate assembly files, you would have to include an ASM-header file with the referenced variables, and maintain a separate C-header with same variables in C-format.

  • What changed? As soon as I declared variables as EXTERN so that all the different C files could share them, AS no longer knew what they were. If I didn't declare them as EXTERN it gives 862 "multiple definition" errors.

    Prior to that, with separate .s and .c files AS knew the address of all the global variables, including the start addresses of arrays and structures, without the need of any assembler header files.

    The only things it didn't know were the offsets inside a structure.

  • Sorry, but I am not making the connection between C-file extern variable references and how that affects the assembler.  As you are using the GNU assembler, I thought it treated all unknown symbols as "extern" by default.

    This is why I requested a simple example to demonstrate what you are seeing.

  • I was struggling to make the connection as well. I'll post some code tomorrow.

    It is definitely GNU AS, and it was happily finding all external references until I told the C compiler that they were external!

  • I’m making progress. Further reading on what EXTERN does (or doesn’t). With EXTERN I need to define as well as declare. . .

Reply Children
No Data