Skip to content

Instantly share code, notes, and snippets.

@puddingpimp
Created May 7, 2014 11:00
Show Gist options
  • Save puddingpimp/8e189df805091c0f0b55 to your computer and use it in GitHub Desktop.
Save puddingpimp/8e189df805091c0f0b55 to your computer and use it in GitHub Desktop.
Multiboot minimalist OS
use32
UMB_START = 0x00100000
org UMB_START
MB_MAGIC = 1BADB002h
MB_F_BOOTALIGNED = 1
MB_F_MEMINFO = (1 shl 1)
MB_F_VIDEOTABLE = (1 shl 2)
MB_F_USE_MBOFFSETS = (1 shl 16)
MB_FLAGS = MB_F_BOOTALIGNED or MB_F_MEMINFO or \
MB_F_USE_MBOFFSETS
MB_CHECKSUM = dword - (MB_MAGIC + MB_FLAGS)
VIDEO_MODE = 1 ; EGA text mode
VIDEO_WIDTH = 80
VIDEO_HEIGHT = 25
VIDEO_DEPTH = 0
VIDEO_BASE_ADDR = 0xB8000
multiboot_header:
dd MB_MAGIC ; Multiboot magic number
flags: dd MB_FLAGS
checksum: dd MB_CHECKSUM
header_addr: dd multiboot_header
load_addr: dd multiboot_header ; start of program text
load_end_addr: dd load_end ; end of program text+data
bss_end_addr: dd bss+10000h
entry_addr: dd entry_point ;
mode_type: dd VIDEO_MODE ; Video mode
width: dd VIDEO_WIDTH ; Video Width
height: dd VIDEO_HEIGHT ; Video Height
depth: dd VIDEO_DEPTH ; Vide Depth
entry_point:
cmp eax,0x2BADB002 ; Are we multibooted
je multiboot
hlt ; not really sure how we'd ever get here
jmp $-1
multiboot:
setup_stack:
mov ecx, mbis
mov [ecx], ebx
add ecx, 4 ; ecx = umb_end
mov eax, [ebx] ; check memory regions
and eax, 1 ; are specified in multiboot
jz fail ; information structure
mov eax, [ebx+8] ; load upper memory size
shl eax,10 ; size is in kb, convert to bytes
add eax,UMB_START ; eax = end of UMB
mov esp,eax ; place stack at top of UMB
mov [ecx], eax
jmp write_str
mem_zero: ; ecx = number of bytes to clear
; eax = start address
cmp ecx, 4
jl mem_zero_tail
mov byte [eax],0
add eax,4
sub ecx,4
jz mem_zero_end
cmp ecx,4
jge mem_zero
mem_zero_tail: ; 4 > ecx > 0
mov byte [eax], 0
inc eax
dec ecx
jnz mem_zero_tail
mem_zero_end:
ret
setup_gdt:
write_str:
mov edi, welcome_msg_end-welcome_msg
mov ebx, welcome_msg
set_b:
mov edx, VIDEO_BASE_ADDR
write_loop:
mov al, [ebx]
mov [edx], al
mov [edx+1], dl
inc ebx
add edx,2
cmp edx, VIDEO_BASE_ADDR+(2*VIDEO_WIDTH*VIDEO_HEIGHT)
jge set_b
dec edi
jnz write_loop
mov edi, welcome_msg_end-welcome_msg
mov ebx, welcome_msg
jmp write_loop
welcome_msg:
db 'Hello there!'
welcome_msg_end:
fail:
hlt
load_end:
bss:
mbis: rd 0
umb_end: rd 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment