Calling Assembly (nasm) from C (GCC on linux)
Posted: Wed Jan 14, 2004 12:00 am
I'm new to this whole lark (OS dev), and I've hit on a little snag.
I want to be able to call an assembly function (done in nasm) from my C kernel code (compiled with GCC on linux).
As I understand it, I can do something like this:
//------ main.c
#include <stdio.h>
extern int Add(int,int);
int main()
{
int x;
x = Add(2,2); // should be 4...
printf("2+2=%d\n",x);
}
//--------Add.asm
[BITS 32]
GLOBAL _Add
SECTION .text
_Add:
push ebp
mov ebp,esp
mov eax,[ebp+8]
mov ecx,[ebp+12]
add eax,ecx
pop ebp
ret
And I compile this with:
nasm -f aout Add.asm
gcc -c main.c
gcc Add.o main.o
But I get an 'undefined reference in function main' error when linking.
I know it's probably something simple, but can anyone help me out or point me the right direction? It'd be much appreciated.
I want to be able to call an assembly function (done in nasm) from my C kernel code (compiled with GCC on linux).
As I understand it, I can do something like this:
//------ main.c
#include <stdio.h>
extern int Add(int,int);
int main()
{
int x;
x = Add(2,2); // should be 4...
printf("2+2=%d\n",x);
}
//--------Add.asm
[BITS 32]
GLOBAL _Add
SECTION .text
_Add:
push ebp
mov ebp,esp
mov eax,[ebp+8]
mov ecx,[ebp+12]
add eax,ecx
pop ebp
ret
And I compile this with:
nasm -f aout Add.asm
gcc -c main.c
gcc Add.o main.o
But I get an 'undefined reference in function main' error when linking.
I know it's probably something simple, but can anyone help me out or point me the right direction? It'd be much appreciated.