User:Imate900/32-bit assembler bare bones: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
Content deleted Content added
added stuff
Line 50: Line 50:


== Without GRUB ==
== Without GRUB ==
=== Starting In Protected Mode ===
If your bootloader loads flat binary images and gives you protected mode for you, use this code as a basis:
If your bootloader loads flat binary images and gives you protected mode for you, use this code as a basis:
<pre>
<pre>
Line 65: Line 64:
jmp short $
jmp short $
</pre>
</pre>
If you start out in real mode, remember to switch to [[Protected Mode]].

=== Starting In Real Mode ===
Usually (in most FAT bootloaders), you must first switch to [[Protected Mode]] in order to write a 32-bit kernel if your kernel starts in [[Real Mode]]. For reference, this code is enough:
<pre>
BITS 16
cli
; Load GDT. Make sure to make one because the Troy told me the GDT was borked :(
lgdt [gdtr]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp 0x08:kernel
BITS 32

kernel:
; reset ds, es, fs, gs here
; write kernel
mov edi, 0xB8000
mov esi, string
mov ah, 0x0F
.displaying:
lodsb
stosw
or al, al
jnz .displaying
jmp short $

gdt:
; Make the GDT here

gdtr:
; Make GDT pointer here
</pre>


{{In Progress}}
{{In Progress}}

Revision as of 20:12, 21 April 2009

Difficulty level

Medium
Kernel Designs
Models
Other Concepts

This tutorial will teach you how to make a 32-bit assembler kernel.

GRUB

All we need is something like this (nasm):

%define MBOOT_MAGIC 0x1badb002
%define MBOOT_FLAGS 0x00010002

bits 32
org 0x100000

__entry_point:
   mov edi, 0xB8000
   mov esi, string
   mov ah, 0x0F
   .displaying:
   lodsb
   stosw
   or al, al
   jnz .displaying
   jmp short $

align 4, db 0
header:
   dd MBOOT_MAGIC
   dd MBOOT_FLAGS
   dd 0 - MBOOT_MAGIC - MBOOT_FLAGS
   dd header
   dd __entry_point
   dd end_of_file
   dd end_of_file
   dd __entry_point

string: db "Hello world!", 0

align 4, db 0
end_of_file:

Link with:

nasm -f bin kernel.asm

Make sure you have a GRUB entry...

title Assembler Barebones
kernel (hd0,0)/boot/kernel.bin

Now, reboot, and enjoy!

Without GRUB

If your bootloader loads flat binary images and gives you protected mode for you, use this code as a basis:

_start:
 ; Write kernel here. It might be good to load a new GDT.
   mov edi, 0xB8000
   mov esi, string
   mov ah, 0x0F
   .displaying:
   lodsb
   stosw
   or al, al
   jnz .displaying
   jmp short $

If you start out in real mode, remember to switch to Protected Mode.

This page is a work in progress.
This page may thus be incomplete. Its content may be changed in the near future.