Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Assembly code that includes header files containing C-definitions

I'm trying to compile assembly and C code together (not C to assembly), but can't get it done.

For example:

file common.h

#ifndef __COMMON_H__
#define __COMMON_H__

struct tree{
        tree* left;
        tree* right;

        void* elem;
};

void foo(int c);
#endif

file common.S

#include "common.h"

    .text
    .globl foo
    .ent foo
foo:
     //foo implementation

    .end foo

When I try to compile this:

# gcc -c common.S
common.h: Assembler messages:
common.h:5: Error: unrecognized opcode `struct tree{'
common.h:7: Error: unrecognized opcode `tree* left'
common.h:8: Error: unrecognized opcode `tree* right'
common.h:10: Error: unrecognized opcode `void* elem'
common.h:12: Error: junk at end of line, first unrecognized character is `}'
common.h:14: Error: unrecognized opcode `void foo(int c)'

Is there any way to take C-definitions into assembly using gcc?

like image 694
Tom Avatar asked Mar 14 '26 14:03

Tom


1 Answers

No, you can't include C declarations in assembly language. The assembler has no idea what struct tree means.

If you want to write an assembly language function foo that makes use of your definition of struct tree, you're going to have to do it without making use of the C header file.

To get an idea of what this might look like, write the foo function in C, compile it with gcc -S to generate an assembly listing, and then take a look at the resulting compiler-generated common.s file. (You should probably do this in a separate directory so you don't clobber your existing common.s file.)

You probably won't see any references to struct tree or to the member names left, right, and elem; instead, you'll see assembly opcodes that refer to data at certain offsets.

like image 149
Keith Thompson Avatar answered Mar 17 '26 03:03

Keith Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!