Writing my own OS
Moderator:Moderators
-
- Posts:1
- Joined:Sun Aug 10, 2008 4:08 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 ???
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 ???
Hi and Welcome! 
Here is a nice tutorial on reading sectors:
<a href="http://www.brokenthorn.com/Resources/OS ... >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/OS ... l">Parsing FAT12</a>
Hope this helps

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)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 boot sector will only get touched if you reformat the disk. So, simply copying the kernel to the floppy disk (copy+paste) will work.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)?
1st stage boot loaders need to rely on the Bios (More specifically, use Bios interrupt 0x13) to read sectors off disk.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?
Here is a nice tutorial on reading sectors:
<a href="http://www.brokenthorn.com/Resources/OS ... >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/OS ... l">Parsing FAT12</a>
Hope this helps

bootloader
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.
that will load a com or exe and start it. The code is even reasonably clear.
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.
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.
That syntax looks odd. MSVC++ uses a form of MASM32 syntax:
Either of the following forms should work:
Either of the following forms should work:
Code: Select all
_asm cli //single instruction only
_asm {cli} //code block contains multiple asm instructions
Code: Select all
_asm volatile ("outb %b0,%w1"
:
: "a"(val), "d"(port));
The above would be this in Intel syntax:
Code: Select all
_asm {
mov al, [val]
mov dx, [port]
out dx, al
}
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.thx for ur help; but unfortunatly non of them work.
the same error still appear!!!!!!
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.
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
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

Hi All;
Thank u very much for ur attention.
this is the x86.h file that contains the errors:
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
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;
}
it should be something like:
You have to do that so the compiler knows what size of operand you are using.
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
}
}
