Mixing Fasm with C , need help ><

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
White-spirit
Member
Member
Posts: 89
Joined: Sun Mar 23, 2008 2:23 pm
Location: [0x8:0x1000]

Mixing Fasm with C , need help ><

Post 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
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post 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'
User avatar
Wave
Member
Member
Posts: 50
Joined: Sun Jan 20, 2008 5:51 am

Post 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.
Conway's Law: If you have four groups working on a compiler, you'll get a 4-pass compiler.
Melvin Conway
User avatar
t0xic
Member
Member
Posts: 216
Joined: Sat May 05, 2007 3:16 pm
Location: VA
Contact:

Post by t0xic »

Whoops, i guess global is just nasm :oops:
White-spirit
Member
Member
Posts: 89
Joined: Sun Mar 23, 2008 2:23 pm
Location: [0x8:0x1000]

Post by White-spirit »

So, how to link my fasm program with my C kernel ? ^^"

Thanks =)
White-spirit
Member
Member
Posts: 89
Joined: Sun Mar 23, 2008 2:23 pm
Location: [0x8:0x1000]

Post 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 : " =/
User avatar
Wave
Member
Member
Posts: 50
Joined: Sun Jan 20, 2008 5:51 am

Post 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.
Conway's Law: If you have four groups working on a compiler, you'll get a 4-pass compiler.
Melvin Conway
White-spirit
Member
Member
Posts: 89
Joined: Sun Mar 23, 2008 2:23 pm
Location: [0x8:0x1000]

Post 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 .
White-spirit
Member
Member
Posts: 89
Joined: Sun Mar 23, 2008 2:23 pm
Location: [0x8:0x1000]

Post 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 ) ^^"
Post Reply