Re:Where do i start??
Posted: Fri Dec 13, 2002 7:56 pm
Thank you sooooo much, this has helped ALOT!! ;D
Code: Select all
[BITS 16]
[ORG 0x7E00]
;Jump over the Functions & Variables, and goto the BootCode
jmp StartOf_Code
BootDrive db 0
StartOf_Code:
mov [BootDrive], dl
;Reset the FloppyDrive
Reset_FloppyDrive:
mov ax, 0x00 ;Select Reset Function
mov dl, [BootDrive] ;Select Drive to Reset
int 0x13 ;Call FloppyDrive Interrupt
jc Reset_FloppyDrive ;If there is an Error, Try Again!
;Read in Kernel
ReadIn_Kernel:
mov dl, [BootDrive] ;Drive to Read From
mov ax, 0x9000 ;Segment to load DiskData to
mov es, ax
mov bx, 0 ;Offset
mov ah, 0x02 ;Get the Segment:Offset from ES:BX
mov ch, 0 ;Cylinder
mov dh, 0 ;Head
mov cl, 6 ;Sector to Start Loading at
mov al, 10 ;Sectors to Load
int 0x13 ;Read!
jc ReadIn_Kernel ;If there is an Error, Try Again!
;Enter PM (Protected Mode)
Enter_ProtectedMode:
;Disable Interrupts
cli
; Enable the A20 Gate
call Empty_8042
mov al, 0x0d1
out 0x64, al
call Empty_8042
mov al, 0x0df
out 0x60, al
call Empty_8042
jmp A20_GateDone
Empty_8042:
mov al, 0x0d0
in al, 0x64
test al, 2
jnz Empty_8042
ret
A20_GateDone:
; Move the GDTR_BASE_ENTRIES into place
mov ax, 0x1000
mov es, ax
mov si, GDTR_BASE_ENTRIES
xor di, di
mov cx, 6
cld
rep movsd
;Load the GDT (Global Descriptor Table)
lgdt [GDTR_PTR]
; Set PE (PMode) Bit
mov eax, cr0
or eax, 1
mov cr0, eax
; Jump into PMode Code
jmp 0x08:In_ProtectedMode
[BITS 32]
;This is where Protected Mode is Entered!
In_ProtectedMode:
; Put the Data Selector into eax for setting up other registers.
mov eax, 0x10 ; Data Selector.
; Make SS, DS, ES, FS and GS = the Data Selector ( In the GDT )
mov ss, eax
mov ds, eax
mov es, eax
mov fs, eax
mov gs, eax
; Set up the PMode stack:
mov ax, 0x10
mov ss, ax
mov esp, 0xFFFF
jmp 0x9000 ;Jump to the Kernel
GDTR_PTR
dw 0xffff
dd 0x00010000
GDTR_BASE_ENTRIES
dd 0
dd 0
db 11111111b ; 7:0 of Limit
db 11111111b ; 15:8 of Limit
db 00000000b ; 7:0 of Base
db 00000000b ; 15:8 of Base
db 00000000b ; 23:16 of Base
db 10011010b
db 11001111b
db 00000000b ; 32:24 of Base
db 11111111b ; 7:0 of Limit
db 11111111b ; 15:8 of Limit
db 00000000b ; 7:0 of Base
db 00000000b ; 15:8 of Base
db 00000000b ; 23:16 of Base
db 10010010b
db 11001111b
db 00000000b
GDTEnd
CodePadding times 2048-($-$$) db 0
Code: Select all
jmp 0x8000 ;Jump to the Kernel
Code: Select all
mov byte [ds:0B8000h], 'P'
mov byte [ds:0B8001h], 1Bh
Code: Select all
jmp 0x8000 ;Jump to the Kernel
Code: Select all
jmp $
Code: Select all
jmp 0x8:0x8000 ; jump to C/asm kernel
Code: Select all
;Initialise the Segment Registers
xor ax, ax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
Code: Select all
mov ax, 0x1000
mov es, ax
mov si, GDT
xor di, di
mov cx, 6
cld
rep movsd
Code: Select all
;Put the Data Selector into eax for setting up other registers
mov eax, 0x10
;Make SS, DS, ES, FS and GS = The Data Selector
mov ss, eax
mov ds, eax
mov es, eax
mov fs, eax
mov gs, eax
;Set up a Protected Mode Stack
mov eax, 0x10
mov ss, eax
mov esp, 0xFFFF
"mov ax, 0" works fine, the "xor ax,ax" just saves a byte. This kind of thing gets used by people that set "org xxx" at the top of their code. The segment registers are set to zero because the assembler takes care of making sure jumps are offset correctly within the segment. Effectively they are making all memory addresses as 0:offset.Berserk wrote:Code: Select all
;Initialise the Segment Registers xor ax, ax mov ds, ax mov es, ax mov fs, ax mov gs, ax
He moves 6 doublewords from the addresses following the label GDT to addresses following 0x10000. This is just moving everything to the place he wants it. Presumably in his kernel scheme the GDT will live permanently in memory following 0x10000.Code: Select all
mov ax, 0x1000 mov es, ax mov si, GDT xor di, di mov cx, 6 cld rep movsd
This code sets up all segment registers to be loaded with the second selector in the GDT. It's reasonable to assume that this is a data selector, and extrapolating general posts on this board that it is a 4gb limit 0 base selector. Without a valid selector in PMode the processor will not function properly (This can be useful, research big real mode for details). In PMode addressing is done via selector:offset, so when you address a piece of memory, eg mov ax,[some label] then what actually happens is the processor uses the base of the selector in ds, adds offset to it, checks permissions and limits then retrieves the data.Code: Select all
;Put the Data Selector into eax for setting up other registers mov eax, 0x10 ;Make SS, DS, ES, FS and GS = The Data Selector mov ss, eax mov ds, eax mov es, eax mov fs, eax mov gs, eax ;Set up a Protected Mode Stack mov eax, 0x10 mov ss, eax mov esp, 0xFFFF
Code: Select all
Which Selectors can i use using my GDT (The Code is in a Previous Post)?
Code: Select all
mov ah,0x02 ; load disk data to ES:BX
mov al,5 ; load 5 sectors
mov ch,0 ; cylinder = 0
mov cl,3 ; sector = 2 (starts at sector 1 not 0)
mov dh,0 ; head = 0 = side one
mov dl,0 ; Floppy drive A = 0
int 0x13 ; read it
jc read_me ; if there's an error then we'll
Code: Select all
Could somebody please post some example code on how to load it and jump to it, i am having big problems trying to load it, my cpu keeps triple faulting
Code: Select all
Code: Select all
jmp 0x08:0x1000 ; Jump to ASM/C Kernel
Code: Select all
;Read in BootLoader
ReadIn_Kernel:
mov dl, [BootDrive] ;Drive to Read From
mov ax, 0x08 ;Segment to load DiskData to
mov es, ax
mov bx, 0x1000 ;Offset
mov ah, 0x02 ;Get the Segment:Offset from ES:BX
mov ch, 0 ;Cylinder
mov dh, 0 ;Head
mov al, 4 ;Sectors to Load
mov cl, 6 ;Sector to Start Loading at
int 0x13 ;Read!
jc ReadIn_Kernel ;If there is an Error, Try Again!