Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [bits 16]
- [extern kernel]
- section .boot
- global boot
- KERNEL_SIZE_SECTORS equ 128 ;Kernel size in sectors - values > 128 not
- ; usually supported with Int 13h/AH=42h!
- boot:
- cld ;set direction flag forward, needed for C code
- ; and any MOVS instructions that expect normal
- ; behaviour
- xor ax, ax
- mov ds, ax ; set DS to 0
- mov ss, ax
- mov sp, 0x7c00 ;Set stack SS:SP below bootloader@0x0000:0x7c00
- mov [disk], dl ;Save bootdrive in DL to disk variable
- mov ax, 0x2401 ;Enable A20 line, Int 15h/AX=2401 doesn't work
- ; in all environments
- int 0x15
- mov ax, 0x3 ;Set the VGA mode, unknown at boot
- int 0x10
- mov ah, 42h ;Do modern disk reading (no fuckery)
- mov dl, [disk] ;Account for cosmic bit flips
- mov si, dap ;Load disk address packet
- int 0x13
- jc disk_err ;Jump if disk read error
- cli ;Clear the interrupts
- lgdt [GDT_POINTER] ;Load the GDT using a gdt pointer
- mov eax, cr0
- or eax, 0x1
- mov cr0, eax
- jmp CODE_SEG:boot2
- print:
- pusha
- start:
- mov al, [bx]
- cmp al, 0
- je done
- mov ah, 0x0e
- int 0x10
- add bx, 1
- jmp start
- done:
- popa
- ret
- print_nl:
- pusha
- mov ah, 0x0e
- mov al, 0x0a
- int 0x10
- mov al, 0x0d
- int 0x10
- popa
- ret
- print_hex:
- pusha
- mov cx, 0
- hex_loop:
- cmp cx, 4
- je end
- mov ax, dx
- and ax, 0x000f
- add al, 0x30
- cmp al, 0x39
- jle step2
- add al, 7
- step2:
- mov bx, HEX_OUT + 5
- sub bx, cx
- mov [bx], al
- ror dx, 4
- add cx, 1
- jmp hex_loop
- end:
- mov bx, HEX_OUT
- call print
- popa
- ret
- disk_err: ;Label for any disk errors
- mov bx, disk_read_error ;Print the disk error
- call print
- call print_nl
- mov dh, ah ;Load error code in ah register
- call print_hex
- jmp $
- GDT_START:
- dq 0x0
- GDT_CODE:
- dw 0xFFFF
- dw 0x0
- db 0x0
- db 10011010b
- db 11001111b
- db 0x0
- GDT_DATA:
- dw 0xFFFF
- dw 0x0
- db 0x0
- db 10010010b
- db 11001111b
- db 0x0
- GDT_END:
- GDT_POINTER:
- dw GDT_END - GDT_START
- dd GDT_START
- CODE_SEG equ GDT_CODE - GDT_START
- DATA_SEG equ GDT_DATA - GDT_START
- ; Bootloader data
- disk:
- db 0x0
- dap:
- db 0x10
- db 0
- dw KERNEL_SIZE_SECTORS
- dw 0 ;
- dw 0x07e0 ;value recommended by a friend
- dq 1 ;Start reading from the second sector
- HEX_OUT:
- db '0x0000',0
- disk_read_error: db "Disk read error!", 0
- times 510 - ($-$$) db 0
- dw 0xaa55
- [bits 32]
- copy_target:
- boot2:
- mov ax, DATA_SEG
- mov ds, ax
- mov es, ax
- mov fs, ax
- mov gs, ax
- mov ss, ax
- mov esi, loaded_msg
- mov ebx, 0xb8000
- .loop: ;Print a message using a loop
- lodsb
- or al, al
- jz load_kernel ;If the message is finished, load kern
- or eax,0x0F00
- mov word [ebx], ax
- add ebx,2
- jmp .loop
- load_kernel:
- mov esp, kernel_stack_top;Load the stack to call C functions
- call kernel ;Call the actual kernel
- halt:
- cli
- hlt
- loaded_msg: db "Operating system loaded",0
- ;Message here to verify disk read
- section .bss
- align 4
- kernel_stack_bottom: equ $ ;equ the current address for the stack
- resb 16384 ;Use 16kb for stack size
- kernel_stack_top: ;Top of the stack
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement