;=============================================================== ; BOOT SECTOR ; [http://www.karig.net/0004.html] ;=============================================================== [ORG 0x7C00] [BITS 16] main: ; ------ Straighten out segment registers. ; ------ [http://www.karig.net/0002.html] jmp word 0:segzero segzero: mov ax, cs mov ds, ax mov es, ax mov fs, ax mov gs, ax ; ------ Set up real/unreal-mode stack. cli mov ax, 0x1000 mov ss, ax mov sp, 0xFFFE ; ------ Enable protected mode. ; ------ [http://www.karig.net/0004.html] lgdt [GDT] mov eax, cr0 or al, 1 mov cr0, eax jmp dword 0x08:.1 [BITS 32] ; ------ Change data-segment limits to four gigabytes. .1: mov eax, dseg-GDT mov ds, eax mov es, eax mov fs, eax mov gs, eax ; ------ Ensure that code- and stack-segment limits are 64KB. mov eax, ds16-GDT mov ss, eax jmp dword 0x18:.2 [BITS 16] ; ------ Disable protected mode; jump to real-mode segment. .2: mov eax, cr0 and al, 0xFE mov cr0, eax jmp word 0x00:.3 ; ------ Load stack- and data-segment registers ; ------ with proper real-mode segment numbers. .3: xor ax, ax mov ds, ax mov es, ax mov fs, ax mov gs, ax mov ss, ax ; ------ Enable A20 line (and thus high memory). ; ------ [http://www.karig.net/0003.html] mov al, 0xD1 out 0x64, al mov al, 0x03 out 0x60, al sti ; ------ Prove this code works -- print "YYYYYYYY" if it does. testcode: mov al, 'Y' mov edi, 0x2FFF00 mov dx, 0xABCD mov [edi], dx mov bx, [edi] cmp bx, 0xABCD je .1 mov al, 'N' .1: xor bh, bh mov bl, 0x20 mov cx, 8 mov ah, 9 int 0x10 ; ------ Halt computer. jmp $ ;------------------------------------------------------------------------------- ; ------ Global descriptor table. GDT: dw gdt_limit ; null segment, used to store GDT metadata dd GDT dw 0 cseg: dd 0x0000FFFF, 0x00CF9800 ; code segment, 32-bit, 0 to 4GB dseg: dd 0x0000FFFF, 0x00CF9200 ; data segment, 32-bit, 0 to 4GB cs16: dd 0x0000FFFF, 0x00009800 ; code segment, 16-bit, 0 to 64KB ds16: dd 0x0000FFFF, 0x00009200 ; data segment, 16-bit, 0 to 64KB gdt_limit equ $-GDT-1 ; ------ (Required to make this a boot sector.) times 510 - ($-$$) db 0 db 0x55, 0xAA