Page 1 of 1

Mixing Fasm with C , need help ><

Posted: Tue Mar 25, 2008 11:40 am
by White-spirit
Hello,

i don't know if this issue is due to a bad linking, or a mistake in my fasm code .

I tryed to link cpuid.asm with kernel.c but it doesn't work .

cpuid.asm :

Code: Select all

format ELF
public get_vendor

get_vendor:
xor eax,eax ;	eax <== 0 : get vendor ID
cpuid
mov [buffer], ebx
mov [buffer+4], edx
mov [buffer+8], ecx
mov eax, dword ptr buffer
ret

buffer: db " ", 0Dh, 0Ah, 0
kernel.c :

Code: Select all

extern char* get_vendor ();
...
int kernel_main(void)
{
	clrscr();
	print(">CPU Vendor : ");
	get_vendor(); // just for testing
	print("\n");
	while(1);
	return(0);
}
...
I use this to compile the two programs :

Code: Select all

fasm cpuid.asm cpuid.o
gcc -ffreestanding -c kernel.c -o kernel.o
ld -e kernel_main -Ttext 0x1000 -o kernel kernel.o cpuid.o
objcopy -R .note -R .comment -S -O binary kernel kernel.bin
Who can help me please =) ?

Thanks .

PS : i'm a beginner on mixing fasm with C

Posted: Tue Mar 25, 2008 12:44 pm
by t0xic
Well it looks like you're trying to link an ELF file as a normal binary. Look in the wiki, or just assemble the fasm file as a binary. And use 'global', not 'public'

Posted: Tue Mar 25, 2008 4:41 pm
by Wave
What do you mean it doesn't work?

Your program doesn't print anything useful because you don't use the return value of the get_vendor() function.

Also, "public" is correct, "global" is wrong. And no, you can't assemble as binary and then link it with a C program. If you're ld for Windows you'd probably want to assemble it as MS COFF instead of elf, though.

Posted: Tue Mar 25, 2008 5:05 pm
by t0xic
Whoops, i guess global is just nasm :oops:

Posted: Wed Mar 26, 2008 5:35 am
by White-spirit
So, how to link my fasm program with my C kernel ? ^^"

Thanks =)

Posted: Wed Mar 26, 2008 6:28 am
by White-spirit
I changed the fasm code :

Code: Select all

format coff
public get_vendor

get_vendor:
pusha
xor eax,eax ;	eax <== 0 : get vendor ID
cpuid
mov eax, [esp+36]
mov [eax], ebx
mov [eax+4], edx
mov [eax+8], ecx
mov byte [eax+12],0
popa
xor eax,eax
ret
but my kernel reboots after printing "> CPU Vendor : " =/

Posted: Wed Mar 26, 2008 10:57 am
by Wave
About the first code: Your buffer is WAY to small. It's only 4 bytes. You write past the end of it. Fix that and then start stripping out parts from the function until it doesn't crash.

Posted: Thu Mar 27, 2008 11:09 am
by White-spirit
If i use "ld -i -e kernel_main -Ttext 0x1000 -o kernel cpuid.o kernel.o" , ld shows me the following error message :

Code: Select all

ld: Relocatable linking with relocations from format coff-i386 (cpuid.o) to form at elf32-i386 (kernel) is not supported
If i use "format elf" instead of "format coff ; or ms coff" , get_vendor is loaded instead of kernel_main by my bootsector :s .

Posted: Thu Mar 27, 2008 11:20 am
by White-spirit
Sorry, it works fine with MS COFF ( and COFF ) , the only problem is that the bootloader loads only 2 sectors but my kernel is too big ( 10 sectors ) ^^"