bits 16 ; calculate logic address of gdt (dx:ax) mov ax, [cs:gdt_base+0x7c00] ; low 16 bits mov dx, [cs:gdt_base+0x7c00+2] ; high 16 bits mov bx, 16 div bx mov ds, ax ; base mov bx, dx ; offset ; fill gdt ; first(0) item is always zero (NULL) ; zero based index mov dword [bx+0x0], 0x0 mov dword [bx+0x4], 0x0 ; data seg mov dword [bx+0x8], 0x8000ffff mov dword [bx+0xc], 0x0040920b ; code seg mov dword [bx+0x10], 0x7c0001ff mov dword [bx+0x14], 0x00409800 ; stack seg mov dword [bx+0x18], 0x00007a00 mov dword [bx+0x1c], 0x00409600 mov word [cs:gdt_bound+0x7c00], 31 lgdt [cs:gdt_bound+0x7c00] ; clear interrupt cli ; into protect mode mov eax, cr0 or eax, 1 mov cr0, eax jmp dword 0x0010:start ; 0000_0000_0001_0000 bits 32 start: ; in 32bits mode, set selector for data segment mov ax, 0x8 ; 0000_0000_0000_1_000 selectot to second(1) index in GDT mov ds, ax ; set selecor for stack segment mov ax, 0x18 ; 0000_0000_0001_1_000 selector to third(2) index in GDT mov ss, ax mov esp, 0x7c00 ; let's show something mov eax, 19268 mov bx, 10 mov cx, 5 .loop: xor dx, dx div bx push dx loop .loop mov byte [0], 'e' mov byte [1], 0x03 ; cyan on black mov byte [2], 'l' mov byte [3], 0x03 mov byte [4], 'd' mov byte [5], 0x03 mov byte [6], 'e' mov byte [7], 0x03 mov byte [8], 'r' mov byte [9], 0x03 mov byte [10], ' ' mov byte [11], 0x00 mov cx, 5 mov bx, 12 .again: pop dx add dl, 0x30 mov [bx], dl inc bx mov byte [bx], 0x02 ; green on black inc bx loop .again mov byte [bx], ' ' mov byte [bx+1], 0x00 mov byte [bx+2], '+' mov byte [bx+3], 0x04 ; red on black mov byte [bx+4], '1' mov byte [bx+5], 0x04 mov byte [bx+6], 's' mov byte [bx+7], 0x04 hlt gdt_bound dw 0 gdt_base dd 0x00007e00 times 510-($-$$) db 0 dw 0xaa55