Re: Protected mode bootloader question



Frank Kotler wrote:

....
mov ax, 7C0h

mov ds, ax

Ah, yes, but once we enter pmode, our descriptors are zero-based. I suppose we could fudge the descriptors to start at 7C00h instead of zero. I did not try that. I fudged the addresses, instead. Obviously, it would have been easier to tell Nasm the truth in the first place!

Best,
Frank

; from Alex Buell, via c.l.a.x.
;-- http://www.munted.org.uk
; One very high maintenance cat living here.
;
; protected mode bootloader

BITS 16
ALIGN 1

jmp NEAR start

greeting_msg:
DB '(part2) Hello, world!', 0

gdtinfo:
DW gdt_end - gdt - 1
DD gdt + 7C00h

gdt: DQ 0 ; always unused
gdt_code:
DW 0xFFFF
DW 0
DB 0
DB 10011010b
DB 11001111b
DB 0
gdt_data:
DW 0xFFFF
DW 0
DB 0
DB 10010010b
DB 11001111b
DB 0
gdt_end:

start:
cli
mov ax, 7C0h ; 0
mov ds, ax

lgdt [gdtinfo]

mov eax, cr0
or al, 1
mov cr0, eax

jmp 0x08:protected_mode + 7C00h ; clear processor pipeline

BITS 32

protected_mode:
mov ax, 0x10 ; was al ; data selector
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax

mov esp, 0x90000

mov ebx, greeting_msg + 7C00h
call print32

loop: hlt
jmp loop

; All code below is 32 bits; and accesses the screen directly.
%define VIDEO_MEMORY 0xB8000
%define COLS 80
%define ROWS 25
%define CHAR_ATTR 15

_CurX DB 0
_CurY DB 0

print32:
pusha
push ebx
pop edi

..loop:
mov bl, BYTE [edi]
cmp bl, 0
je .done

call putch32

inc edi
jmp .loop

..done:
mov bh, BYTE [_CurY + 7C00h]
mov bl, BYTE [_CurX + 7C00h]
call movecursor32

popa
retn

; BL = character to output
putch32:
pusha
mov edi, VIDEO_MEMORY
xor eax, eax

mov ecx, COLS * 2
mov al, BYTE [_CurY + 7C00h]
mul ecx
push eax

mov al, BYTE [_CurX + 7C00h]
mov cl, 2
mul cl
pop ecx
add eax, ecx

xor ecx, ecx
add edi, eax

cmp bl, 0x0A
je .row

mov dl, bl
mov dh, CHAR_ATTR
mov WORD [ds:edi], dx

inc BYTE [_CurX + 7C00h]
jmp .done

..row:
mov BYTE [_CurX + 7C00h], 0
inc BYTE [_CurY + 7C00h]

..done:
popa
retn

; Updates the hardware cursor
; BL = X, BH = Y

movecursor32:
pusha

xor eax, eax
mov ecx, COLS
mov al, bh
mul ecx
add al, bl
mov ebx, eax

mov al, 0x0f
mov dx, 0x03D4
out dx, al

mov al, bl
mov dx, 0x03D5
out dx, al

xor eax, eax

mov al, 0x0e
mov dx, 0x03D4
out dx, al

mov al, bh
mov dx, 0x03D5
out dx, al

popa
retn

TIMES 510 - ($ - $$) DB 0 ; fill sector w/ 0's
DW 0xAA55 ; boot block signature
;-------------------------------------------------------------


.