Page 1 of 1

Problem loading the kernel (Without the Pmode enabled )

Posted: Thu Aug 16, 2012 8:54 am
by richi18007
I was trying to load a small piece of code with the bootloader .
Now , the problem is everything works fine when I load a flat binary prepared by nasm to the memory address 0x2000:0000 (note that PMode is still not enabled , so if i'm wrong please do correct me while I read more about this at osdev forums)

The sequence of operations

Bootloader Up -> Loading module from sector 2 gets printed

Now , instead of the "Loading module from sector 2 gets printed" , i want to print an alphabet 'C' via a Compiled C file. This 'C' is printed with the help of asm directives in the C file (And BIOS interrupt 0x13) . But it doesn seem to work , I tried to different methods to just load the C file correctly , but it doesn work .And I am pretty sure that the issue is with the loading and linking of C code only because I can load and print an nasm prepared flat binary very easily . These are the two scripts I used for linking

link.ld

Code: Select all

OUTPUT_FORMAT("BINARY")
ENTRY(main)
phys = 0x2000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .;
}


Then , i just added them up into a floppy disk image

Code: Select all

ld -T link.ld -o kernel.bin main.o
dd if=kernel.bin of=floppy.img seek=1 bs=512 count=1 conv=notrunc (seeking  1 because bootloader resides at sector 1 )
qemu -fda floppy.img 
Doesn work
if I replace kernel.bin with main.bin (the second stage in assembly , flat binary) ... it works

And the second script

Code: Select all

ld -e _main -Ttext 0x20000 -o kernel.o main.o 
ld -i -e _main -Ttext 0x20000 -o kernel.o main.o 
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
dd if=kernel.bin of=floppy.img seek=1 bs=512 count=1 conv=notrunc
qemu -fda floppy.img 
Once again , this doesn work either .
Can anybody please tell me what am I doing wrong ? Please excuse me for my english .

Re: Problem loading the kernel (Without the Pmode enabled )

Posted: Thu Aug 16, 2012 9:10 am
by Owen
GCC doesn't produce real mode code

Re: Problem loading the kernel (Without the Pmode enabled )

Posted: Thu Aug 16, 2012 9:29 am
by eino
Check out open watcom c compiler for real mode c compiler http://www.openwatcom.org/index.php/Main_Page

Re: Problem loading the kernel (Without the Pmode enabled )

Posted: Thu Aug 16, 2012 10:03 am
by Combuster
richi18007 wrote:ld -e _main -Ttext 0x20000 -o kernel.o main.o
ld -i -e _main -Ttext 0x20000 -o kernel.o main.o
objcopy -R .note -R .comment -S -O binary kernel.o kernel.bin
Will someone DDOS osdever.net until that tutorial from hell is gone forever?

Re: Problem loading the kernel (Without the Pmode enabled )

Posted: Thu Aug 16, 2012 10:09 am
by richi18007
So , i need to switch to Pmode before writing something fancy C code :-/ .. Wattcomm compiler , here we go.
Thanks for the quick reply btw =D>

Re: Problem loading the kernel (Without the Pmode enabled )

Posted: Wed Aug 22, 2012 1:03 am
by richi18007
Owen wrote:GCC doesn't produce real mode code
Ironically it does ..

And i figured out how to get the code up and working finally. But now, the problem is in C code , I cannot make a function call. I implemented this little piece of code as a test.

Code: Select all

#ifndef _CODE16GCC_H_
#define _CODE16GCC_H_
__asm__(".code16gcc\n");
#endif
/*
 * A simple bootloader skeleton for x86, using gcc.
 *
 * Prashant Borole (boroleprashant at Google mail)
 * */
#include "code16gcc.h" 
/* XXX these must be at top */
__asm__ ("jmp main");
 
 
#define __NOINLINE  __attribute__((noinline))
#define __REGPARM   __attribute__ ((regparm(3)))
#define __NORETURN  __attribute__((noreturn))
 
/* BIOS interrupts must be done with inline assembly */
void    __NOINLINE __REGPARM print(const char   *s){
	int i ;
        for(i=0;s[i];i++)
        {
        	char ch = s[i];
        	__asm__("mov $0x0E ,  %ah");
                __asm__("xor %bh ,  %bh");
                __asm__("mov $0xEF ,  %bl");
		__asm__("mov %0 , %%al ;": : "r"(ch):"%eax");
                __asm__("or %al , %al");
                __asm__("int $0x10");

        }
        return ;
}
/* and for everything else you can use C! Be it traversing the filesystem, or verifying the kernel image etc.*/
 
void __NORETURN main(){
   print("woo hoo!\n");
  		char ch='A';
                __asm__("mov $0x0E ,  %ah");
                __asm__("xor %bh ,  %bh");
                __asm__("mov $0xEF ,  %bl");
		__asm__("mov %0 , %%al ;": : "r"(ch):"%eax");
                __asm__("or %al , %al");
                __asm__("int $0x10");
                
    while(1);
}
While it has no problem printing the char ch in main , the print function fails repeatedly. And the problem actually is

Code: Select all

it inserts a call like this mov byte ptr ds[edi] , al ; And this results in making a call which is far into the memory than what real mode can refer.
I am trying though :) ..Will be back when I have completely given up .

Re: Problem loading the kernel (Without the Pmode enabled )

Posted: Wed Aug 22, 2012 1:29 am
by gerryg400
richi18007 wrote:Ironically it does ..
How is that irony ? Do you know what irony is ?

Re: Problem loading the kernel (Without the Pmode enabled )

Posted: Wed Aug 22, 2012 1:40 am
by bluemoon
do not use code16gcc
http://www.delorie.com/gnu/docs/binutils/as_270.html

it is also considered unstable

Re: Problem loading the kernel (Without the Pmode enabled )

Posted: Wed Aug 22, 2012 4:13 am
by JamesM
Ah the standard "I've come here for help but I know better than you" newbie attitude.

I look forward to your fall from your pedestal when you realise that GCC can not, in fact, reliably generate real mode code.

Re: Problem loading the kernel (Without the Pmode enabled )

Posted: Thu Aug 23, 2012 2:33 am
by richi18007
JamesM wrote:Ah the standard "I've come here for help but I know better than you" newbie attitude.

I look forward to your fall from your pedestal when you realise that GCC can not, in fact, reliably generate real mode code.
Sorry but I did not mean offense to anyone. I only use gcc16asm till I have Pmode enabled , after that , I will use the standard 32 bit set up. Hope gcc16asm works till then. And if it doesn't work. I will switch back to assembly. My sole intention was to learn. Whether , we can make real mode 16 bit code or not. And I did learn.