Parsing Error

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
friend

Parsing Error

Post by friend »

Hi,

I have a problem with my c code, I have an asm file with this code in it

; === ASM CODE =====
[GLOBAL _KillFloppy]

; Reset and Kill Floppy Motor
_KillFloppy:
   push   eax
   mov      dx, 0x03f2
   mov      al, 0x00
   out      dx, al
   pop      eax
   ret

then I have a c file with this code in it

; === C CODE =====
// Linked ASM Function
void KillFloppy();

// Jump to the entry point
asm("jmp _kernel_start");

// kernel entry point
void kernel_start()
{
   KillFloppy();

   char* video = (char*)0xb8000;
   char* message = "F r i e n d O S E x a m p l e K e r n e l ";
   int i;

   // put the message into video memory
   for ( i = 0; i < 46; i++ )
      video = message;

   // Hang
   while( 1 )
   {}
}

I am compiling and linking like this

nasm -f elf floppy.asm
gcc -c kernel.c
ld -Ttext=0x20000 kernel.o floppy.o -o kernel.exe
objcopy -O binary kernel.exe kernel.bin

but I keep getting an error from gcc saying

kernel.c: In function `kernel_start':
kernel.c:14: parse error before `char'
kernel.c:19: `i' undeclared (first use in this function)
kernel.c:19: (Each undeclared identifier is reported only once
kernel.c:19: for each function it appears in.)
kernel.c:20: `video' undeclared (first use in this function)
kernel.c:20: `message' undeclared (first use in this function)

any ideas what is wrong?

thanks.
dronkit

Re:Parsing Error

Post by dronkit »

in kernel_start() you're calling a function *before* declaring your variables. Any code should starte right *after* any variable/constants declarations.

Be careful with that first line of inline asm. you have to be SURE this is the first code in the binary object. You should call the C main entry point right from your asm code.

regards.
Friend

Re:Parsing Error

Post by Friend »

Thanks dronkit, that is something that I was not aware of about variable declaration, I am used to C++ programming with BCC55 in which this was not an issue.

Also, I should be OK if I always link my .o files with the main kernel file first, this should make certain that this line of code is always first in the binary file, shouldn't it.

thanks again.
dronkit

Re:Parsing Error

Post by dronkit »

Thanks dronkit, that is something that I was not aware of about variable declaration, I am used to C++ programming with BCC55 in which this was not an issue.
Right, in C++ you can declare variables wherever you want ;)
Also, I should be OK if I always link my .o files with the main kernel file first, this should make certain that this line of code is always first in the binary file, shouldn't it.
Not always, it really depends on your linker and its options, so for portability's sake it is better to handle this kind of stuff right from your code without worrying about the order your code gets linked.
Friend

Re:Parsing Error

Post by Friend »

So how should I do this straight from my code?
dronkit

Re:Parsing Error

Post by dronkit »

Oh, I just saw your linking command. I'm sorry, this is a valid approach too. Still, you may want to do something like this:

Suppose you want to call main() (c code) from your bootstrapper code (asm code):

You have:

Code: Select all

int 
main(void)
{
   ...
}
Then, there's your bootstrapper:

Code: Select all

.extern main

.org 0 // or whatever
...
...
...
call main
then you link everything together, passing the asm file first to the linker.
dronkit

Re:Parsing Error

Post by dronkit »

What I meant before, is that the first line of your code may or may not be the first executable code in the resulting binary file.

The last post shows you how to correctly call your code from a bootstrapper without worrying about how the linker wants to link your c code.
Friend

Re:Parsing Error

Post by Friend »

OK, thanks for your help.
Post Reply