Writing my own OS

If you are new to OS Development, plan on spending some time here first before going into the other forums.

Moderator: Moderators

fadiababneh
Posts: 1
Joined: Sun Aug 10, 2008 4:08 pm

Writing my own OS

Post by fadiababneh » Sun Aug 10, 2008 4:18 pm

Hi all;
I have been trying to write a small operating system by myself. I have compiled a small C kernel for myself. However the only boot loader that I can write in ASM is one that says hello world. I would like my boot loader to load my kernel into the memory and then execute it. I want to boot from a floppy disk. I can write a boot loader onto the first 512 bytes of my floppy disk.

The two questions are:


1. How can I write the kernel and the boot loader onto the FD, and make sure that the boot loader still occupies the first 512 bytes (needed for it to boot)?
2. How can i get my boot loader to load the kernel from the FD into the memory and then execute it. What asm coding do I need?

Any one can help me ???

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Post by Mike » Mon Aug 11, 2008 5:25 pm

Hi and Welcome! :)

I would like my boot loader to load my kernel into the memory and then execute it. I want to boot from a floppy disk. I can write a boot loader onto the first 512 bytes of my floppy disk.
Is your C program 32 or 16 bit? If it is 32 bit, you may need to load a 2nd stage boot program first to set up 32 bit protected mode. (Most boot loaders do this from GRUB, LILO, and Windows NTLDR)

1. How can I write the kernel and the boot loader onto the FD, and make sure that the boot loader still occupies the first 512 bytes (needed for it to boot)?
The boot sector will only get touched if you reformat the disk. So, simply copying the kernel to the floppy disk (copy+paste) will work.

2. How can i get my boot loader to load the kernel from the FD into the memory and then execute it. What asm coding do I need?

1st stage boot loaders need to rely on the Bios (More specifically, use Bios interrupt 0x13) to read sectors off disk.

Here is a nice tutorial on reading sectors:
<a href="http://www.brokenthorn.com/Resources/OSDev5.html">Clicky</a>

After being able to read sectors off disk, you will need to be able to parse the file system. (The tutorial I linked you to describes parsing FAT12)
<a href="http://www.brokenthorn.com/Resources/OSDev6.html">Parsing FAT12</a>

Hope this helps ;)

roboman
Posts: 12
Joined: Fri Nov 28, 2008 10:02 pm
Location: usa
Contact:

bootloader

Post by roboman » Fri Nov 28, 2008 10:21 pm

There is also a nice boot loader at: http://alexfru.chat.ru/epm.html
that will load a com or exe and start it. The code is even reasonably clear.

fadi_jor
Posts: 11
Joined: Sat Nov 15, 2008 5:33 pm

Post by fadi_jor » Wed Dec 03, 2008 2:37 pm

Hi All;
Thank u for ur help.

But I have a problem in "Kernel Setup: MSVC++ 2005" demo (Demo 5), that is I didn't know which files I have to copy to disk and how to copy it?!

Plz help me.
Thanks again.

Andyhhp
Moderator
Posts: 387
Joined: Tue Oct 23, 2007 10:05 am
Location: 127.0.0.1
Contact:

Post by Andyhhp » Wed Dec 03, 2008 4:42 pm

The single file you need to copy is Kernel.exe (for .dll if you used the old tutorial).

And you copy it in the same way you would copy any other file. The bootloader itself is a valid FAT12 signature so your OS will know how to copy the file correctly.

~Andrew
Image

fadi_jor
Posts: 11
Joined: Sat Nov 15, 2008 5:33 pm

Post by fadi_jor » Fri Dec 05, 2008 12:20 pm

Hi;
I have a code that is read a charectar from the keyboard; in this code there is an assemply instructions like(inb,outb,sti,cli) that access the ports and deal with interrupt but the windows prevent such instruction.
as:
_asm volatile("cli"); // Disable ints

therefore, when I try to compile my kernel the MSVC++ gives me this error in every place I had used these instructions:
" error C2400: inline assembler syntax error in 'opcode'; found 'data type' ".

may any one help me to solve this problem and grant the .exe kernel file.


Thx.

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Post by Mike » Fri Dec 05, 2008 11:24 pm

That syntax looks odd. MSVC++ uses a form of MASM32 syntax:

Either of the following forms should work:

Code: Select all

_asm cli //single instruction only
_asm {cli} //code block contains multiple asm instructions

fadi_jor
Posts: 11
Joined: Sat Nov 15, 2008 5:33 pm

Post by fadi_jor » Sat Dec 06, 2008 1:31 am

thx for ur help; but unfortunatly non of them work.
the same error still appear!!!!!!

any other solutions;plz.

fadi_jor
Posts: 11
Joined: Sat Nov 15, 2008 5:33 pm

Post by fadi_jor » Sat Dec 06, 2008 1:35 am

and what about this:

_asm volatile ("outb %b0,%w1"
:
: "a"(val), "d"(port));

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Post by Mike » Sat Dec 06, 2008 3:08 am

Code: Select all

_asm volatile ("outb %b0,%w1"
:
: "a"(val), "d"(port));

This is AT&T syntax not Intel syntax. You must use the Intel assembly language syntax not AT&Ts when using MSVC++ inline assembler.

The above would be this in Intel syntax:

Code: Select all

_asm {
   mov   al, [val]
   mov   dx, [port]
   out   dx, al
}


thx for ur help; but unfortunatly non of them work.
the same error still appear!!!!!!

I suspect you are mixing AT&T syntax with Intel assembly language syntax here. You will need to update all of the AT&T syntax to match that of Intel's syntax or rewrite it using Intel's syntax in order for it to work.

I can post a tutorial on Intel syntax if you need it. Or, if it is not much code, post it here and I might be able to convert it for you.

Andyhhp
Moderator
Posts: 387
Joined: Tue Oct 23, 2007 10:05 am
Location: 127.0.0.1
Contact:

Post by Andyhhp » Sat Dec 06, 2008 6:22 am

Another thing to note is the different uses of the volatile keyword.

For MSVC, volatile can only be used as a modifier for variables and it tells the compiler not cache the variable (e.g. in registers) because the value itself is likely to change without the current program changing it. This is mostly useful for concurrency but also if you have a variable that an interrupt is likely to change.

For the GNU chain (which is the only chain I know that uses AT&T syntax), volatile tells the compiler to optimize the ASM itself. In this case, it is more of a 'I know what I am doing' instruction to the compiler.

~Andrew
Image

fadi_jor
Posts: 11
Joined: Sat Nov 15, 2008 5:33 pm

Post by fadi_jor » Sat Dec 06, 2008 5:53 pm

Hi All;
Thank u very much for ur attention.

this is the x86.h file that contains the errors:

Code: Select all

// defines:

#ifndef X86_H
#define X86_H


// voids:

// inb: get a byte from the selected port
inline unsigned inportb(unsigned port);

// outb: send the selected byte to the selected port
inline  void outportb(unsigned port, unsigned val);

// Enable Interrupts:
inline  void sti();

// Disable Interrupts:
inline  void cli();

// A reboot function for rebooting the computer:
void reboot();

// data:
int checkints;      // for checking if interrupts are disabled or enabled

//***********************************************************************************************************
//

inline  unsigned inportb(unsigned port)
{
   unsigned ret_val;

    _asm volatile ("inb %w1,%b0"
       : "=a"(ret_val)
       : "d"(port));

   return ret_val;
}

//***********************************************************************************************************
//

inline  void outportb(unsigned port, unsigned val)
{
    _asm volatile  ("outb %b0,%w1"
      :
      : "a"(val), "d"(port));
}

//***********************************************************************************************************
//

void reboot()
{
   int temp;      // A temporary int for storing keyboard info. The keyboard can be use to
            //  reboot the PC

   // Get ready for reboot...flush the keyboard controller
        do
        {
                temp = inportb( 0x64 );

                if ( temp & 1 )
                        inportb( 0x60 );
        }
   while ( temp & 2 );

        // Reboot the computer...
        outportb(0x64, 0xFE);

   // If this didn't reboot the computer...let the code that calles this handle that.
}

//***********************************************************************************************************
//

inline  void sti()
{
   _asm volatile( "sti" );   // Enable ints

   // Set checkints to 1 - on
   checkints = 1;
}

//***********************************************************************************************************
//

inline  void cli()
{
   _asm volatile("cli");   // Disable ints

   // Set checkints to 0 - off
   checkints = 0;
}

#endif

// end of x86.h

User avatar
Mike
Site Admin
Posts: 465
Joined: Sat Oct 20, 2007 7:58 pm
Contact:

Post by Mike » Sun Dec 07, 2008 1:30 am

Try these routines instead (cli and sti not tested but should work) :

Code: Select all

inline   unsigned _cdecl inportb (unsigned portid) {

   unsigned res=0;

   _asm {
      mov dx, [portid]
      in   al, dx
      mov [res], al
   }

   return res;
}

inline   void _cdecl outportb (unsigned portid, unsigned value) {

   _asm {
      mov   al, [value]
      mov   dx, [portid]
      out   dx, al
   }
}

inline void sti () {

   _asm sti

   // Set checkints to 1 - on
   checkints = 1;
}

inline  void cli() {

   _asm cli

   // Set checkints to 0 - off
   checkints = 0;
}

fadi_jor
Posts: 11
Joined: Sat Nov 15, 2008 5:33 pm

Post by fadi_jor » Sun Dec 07, 2008 12:38 pm

Thx;
but I got this error on every line that contains "mov" instruction:

" error C2443: operand size conflict "

Andyhhp
Moderator
Posts: 387
Joined: Tue Oct 23, 2007 10:05 am
Location: 127.0.0.1
Contact:

Post by Andyhhp » Sun Dec 07, 2008 3:55 pm

it should be something like:

Code: Select all

inline   unsigned _cdecl inportb (unsigned portid) {

   unsigned res=0;

   _asm {
      mov dx,WORD PTR [portid]
      in   al, dx
      mov BYTE PTR [res], al
   }

   return res;
}

inline   void _cdecl outportb (unsigned portid, unsigned value) {

   _asm {
      mov   al,BYTE PTR [value]
      mov   dx,WORD PTR [portid]
      out   dx, al
   }
}

You have to do that so the compiler knows what size of operand you are using.
Image

Post Reply