org 0x7c00 bits 16 %ifndef SECTOR_CNT %define SECTOR_CNT 3 %endif jmp 0x0000:mbr_start mbr_start: cli cld ; Clear rest of segments xor ax, ax mov ds, ax mov es, ax mov ss, ax mov sp, 0x7c00 mov bp, sp ; Store boot device push dx ; swap to unreal mode push ds lgdt [gdtEnd] mov eax, cr0 or al, 1 mov cr0, eax jmp .pmode .pmode: mov bx, 0x08 mov ds, bx and al, 0xFE mov cr0, eax pop ds sti ; Clear screen xor ax, ax mov al, 0x03 int 0x10 ; Initialize serial port mov dx, 0x3F9 xor al, al out dx, al add dx, 2 mov al, 0x80 out dx, al sub dx, 3 xor al, al out dx, al inc dx out dx, al add dx, 2 add al, 3 out dx, al dec dx mov al, 0xC7 out dx, al add dx, 2 mov al, 0x0B out dx, al load_second_stage: pop dx push dx mov bx, loader_entry xor ch, ch mov cl, 0x02 xor dh, dh mov byte [.sectors_to_load], SECTOR_CNT .read_start: mov di, 5 .read: mov ah, 0x02 mov al, byte [.sectors_to_load] int 0x13 jc .retry cmp al, byte [.sectors_to_load] je .done sub byte [.sectors_to_load], al mov cl, 0x01 xor dh, 1 jnz .read_start inc ch jmp .read_start .retry: ; disk read failed, reset disk xor ah, ah int 0x13 dec di jnz .read mov si, .msg_diskread_fail jmp .fail .done: jmp loader_entry .fail: lodsb or al, al mov ah, 0x0E int 0x10 test al, al jnz .fail .hang: cli hlt jmp .hang .sectors_to_load: db 1 .msg_diskread_fail: db "HALT: Failed to read boot disk", 0x0A, 0x0D, 0 ALIGN 4 gdt32: dq 0 code: dw 0xffff dw 0 db 0 db 10011010b db 11001111b db 0 data: dw 0xffff dw 0 db 0 db 10010010b db 11001111b db 0 gdtEnd: dw gdtEnd - gdt32 - 1 dd gdt32 ; Padding & Signature times 510-($-$$) db 0 dw 0xAA55 loader_entry equ 0x7e00 bits 32 _start: ; mov ax, 16 ; mov ds, ax ; mov ss, ax ; mov es, ax ; mov fs, ax ; mov gs, ax cli in al, 0x70 or al, 0x08 out 0x70, al lgdt [gdtEnd] mov eax, cr0 or eax, 0x01 mov cr0, eax jmp x x: mov dword [0xb8000], 0x07690748 jmp $ mov ebx, hello call printString dump: mov ebx, 0x7e00 mov cl, 5 .loop: mov al, [ebx] call print_hex_byte inc ebx dec cl jnz .loop jmp $ printString: push ebx push ax .loop: mov al, [ebx] or al, al jz .return call printChar inc ebx jmp .loop .return: pop ax pop ebx ret print_hex_byte: mov [.temp],al shr al,4 cmp al,10 sbb al,69h das call printChar mov al,[.temp] ror al,4 shr al,4 cmp al,10 sbb al,69h das call printChar ret .temp db 0x00 printChar: push ebx mov ebx, [.position] mov [ebx], byte al inc ebx mov [ebx], byte 0x07 inc ebx mov [.position], ebx pop ebx ret .position dd 0xb8000 old: nop nop mov ax, 16 mov ds, ax mov ss, ax mov es, ax mov fs, ax mov gs, ax mov dword [0xb8000], 0x07690748 jmp $ hello db 'hello world! ', 0x00 db 0xB1, 0x05, 0xF0, 0x0D ; biosfood CODE_SEG equ 0x08 dq 0x00 stackTop: