Page 1 of 1

how to share a C include file between C and Gas

Posted: Mon Feb 18, 2013 5:01 pm
by lcjuanpa
Hi everybody
firstly apologies for this question but I need your help one more time.
I want to share a C header file with Gas but I can't do it.
As a simple example I have 3 files with code below and I get link errors, wich is right because the constants (macros) HOLA_MUNDO and TAM_HOLA_MUNDO is not present in the assembly file.

----------------------------------------------------------
main.c
----------------------------------------------------------
#include <stdio.h>

int main()
{
saludar();

return 0;
}

----------------------------------------------------------
saludos.h
----------------------------------------------------------
#define HOLA_MUNDO "Hola mundo\n"
#define TAM_HOLA_MUNDO 11
#define COMO_ESTAS "Como estas?\n"

----------------------------------------------------------
saludar.s
----------------------------------------------------------
.include "saludos.inc"

.code32
.section .text

.global saludar
saludar:
pushl %ebp
movl %esp, %ebp
movl $4, %eax
movl $1, %ebx
movl HOLA_MUNDO, %ecx
movl TAM_HOLA_MUNDO, %edx
int $0x80
leave
ret

fin:
xorl %eax, %eax
incl %eax
xorl %ebx, %ebx
int $0x80

.end



I tried some options to compile but I failed, also I was reading in google that it's not possible, is it truth?. Do you know some hack that can help me.
Thanks.

Re: how to share a C include file between C and Gas

Posted: Mon Feb 18, 2013 6:31 pm
by thomasloven
I found a way that does what I think you want.

I wrote a bit about it at http://thomasloven.com/blog/2012/06/C-Headers-In-Asm/ last summer.

The trick is to use a c preprocessor for evaluating the include macros and insert the necessary parts into a new source file.
Then you assemble the new file, and you're good.

Re: how to share a C include file between C and Gas

Posted: Mon Feb 18, 2013 7:41 pm
by Owen
If you're using the GNU Assembler, then there are only three things you need to do:
  1. Change the filename of your files to myfile.S (note the capital S - it's important)
  2. Use GCC to build them, not as
  3. Use #include to include the header file
GCC has builtin support for this.

To detect the header being included from assembly, use #ifdef __ASSEMBLER__

Re: how to share a C include file between C and Gas

Posted: Tue Feb 19, 2013 2:15 pm
by lcjuanpa
Hi men
thanks a lot for your help, but now let me feedback you.
The .include directive is util to include files related with assembly but not with Preprocessor's files.
So is really necessary put the #include instruction into assembly file and compile it with the two options provided by: thomasloven and Owen.
In my case I just changed saludar.s code:

----------------------------------------------------------
saludar.s
----------------------------------------------------------
#include "saludos.h"

.code32
.section .text

.global saludar
saludar:
pushl %ebp
movl %esp, %ebp
movl $4, %eax
movl $1, %ebx
movl $HOLA_MUNDO, %ecx
movl $TAM_HOLA_MUNDO, %edx
int $0x80
leave
ret
...

and use the next command to preprocessing:
$ cpp -x assembler-with-cpp saludar.s -o saludar.cpp
or
$ gcc -E -x assembler-with-cpp saludar.s -o saludar.cpp

and generating next code:
# 1 "saludar.s"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "saludar.s"
# 1 "saludos.h" 1
# 2 "saludar.s" 2

.code32
.section .text

.global saludar
saludar:
pushl %ebp
movl %esp, %ebp
movl $4, %eax
movl $1, %ebx
movl $"hola mundo\n", %ecx
movl $11, %edx
int $0x80
leave
ret
As you can see, the '$' symbol will works but just with numerical data but not to manipulate strings. Even if someone would want to use memory address is responsability of programmer the use of '$' symbol or not. So take care.

to compile I use:
$ as saludar.cpp -o saludar.o
and generate next error:
saludar.s: Assembler messages:
saludar.s:12: Error: invalid character '"' in operand 1
wich is right by line: movl $"hola mundo\n", %ecx


As the code above just was an example I'm fine. I'm just planning to use numerical values at momment, so thanks one more time.