User:Imate900/32-bit assembler bare bones: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
Line 89: | Line 89: | ||
; Don't remove the next line |
; Don't remove the next line |
||
db 0, 0, 0 |
db 0, 0, 0 |
||
; Make |
; Make the rest of the GDT here |
||
gdtr: |
gdtr: |
Revision as of 00:35, 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 (GNU as):
.global loader # making entry point visible to linker # setting up the Multiboot header - see GRUB docs for details .set ALIGN, 1<<0 # align loaded modules on page boundaries .set MEMINFO, 1<<1 # provide memory map .set FLAGS, ALIGN | MEMINFO # this is the Multiboot 'flag' field .set MAGIC, 0x1BADB002 # 'magic number' lets bootloader find the header .set CHECKSUM, -(MAGIC + FLAGS) # checksum required .align 4 .long MAGIC .long FLAGS .long CHECKSUM _start: # Write the kernel here jmp _start
Link with:
as -o kernel.o kernel.s ld --oformat binary -o kernel.bin kernel.o
For NASM:
global loader ; making entry point visible to linker ; setting up the Multiboot header - see GRUB docs for details ALIGN equ 1<<0 ; align loaded modules on page boundaries MEMINFO equ 1<<1 ; provide memory map FLAGS equ ALIGN | MEMINFO ; this is the Multiboot 'flag' field MAGIC equ 0x1BADB002 ; 'magic number' lets bootloader find the header CHECKSUM equ -(MAGIC + FLAGS) ; checksum required mboot: dd MAGIC dd FLAGS dd CHECKSUM _start: ; Write the kernel here jmp _start
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
Starting In Protected Mode
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. jmp _start
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:
BITS 16 cli ; Load GDT. Make sure to make one because 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 mov ax, gdt: ; Don't remove the next line db 0, 0, 0 ; Make the rest of the GDT here gdtr: ; dunno if OK, correct if needed... db 0 db 0