User:Imate900/32-bit assembler bare bones

From OSDev Wiki
Jump to navigation Jump to search


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

Preface

Most OSes are in C, but you may consider it in Assembly. This is moderate-easy, and requires knowing GRUB and Assembly.

GRUB

All we need is something like this (Nasm):

%define MBOOT_MAGIC 0x1badb002
%define MBOOT_FLAGS 0x00010002

bits 32
org 0x100000

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
align 0, db 0
__entry_point:
   mov edi, 0xB8000
   mov esi, string
   mov ah, 0x0F
   .displaying:
   lodsb
   stosw
   or al, al
   jnz .displaying
   jmp short $
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 $

string: db "Hello world!", 0

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

Postface

Good luck writing your 32-bit Assembly kernel! But, there ARE catch-yas!

  • As soon as possible, load a new GDT.
  • You must not depend on the BIOS to do everything for you. Most prominent are disk access, printing to the screen (this tutorial has a way to printing: writing to 0xb8000) and reading keys from the keyboard. No BIOS ints will help and only cause a GPF.