flat assembler
Message board for the users of flat assembler.

Index > OS Construction > whats wrong with my code?

Author
Thread Post new topic Reply to topic
ghostrider



Joined: 05 Feb 2007
Posts: 1
Location: Earth
ghostrider 05 Feb 2007, 23:38
I've just begun working on my OS I've wanted to write. I've been working with protected mode for a while but I can't seem to find the bug in it. Can anyone help me?

Code:
;Latin V1.0.0
;Learn protected mode.

ORG 7C00h
USE16 ;Use 16 bit code for now.

;This build of Latin has one purpose, to go into protected mode and display something on the screen.  Protected mode isnt as complex as it sounds.  Below are the steps.

;1. Build a Global Descriptor Table (GDT) which includes a null segment, a segment for code, and a segment for data.  The segments can overlap, which is nice.  Displaying something on the screen does not require a stack segment.
;2. Build the GDTR, a 48-bit register that has the number of descriptors (including null descriptor) and the LINEAR address of the GDT(take segment * 16 + offset).  BE SURE TO SUBTRACT ONE FROM THE LENGTH OF THE GDT.
;3. Turn on the lowest bit in the CR0 register.
;4. Turn off interrupts.
;5. Make a jump into protected mode.

start:
use16

xor ax, ax
mov ss, ax
mov sp, 7c00h

push ax
push ax
pop ds
pop es
;All registers are set up

;Our Global Descriptor Table has already been set up.  Since we know our segment is zero, we don't need to multiply anything by 16.  The linear address is simply its position in memory.

lea eax, [GDT1] ;Thats where the GDTR starts.
mov [GDTRLinAdd], eax
mov ebx, eax
lgdt [ebx] ;Our Global Descriptor Table is in memory

cli ;Turn off inturrupts

mov eax, cr0
or eax, 1h ;Sets the very first bit.
mov cr0, eax
;We are now technically in protected mode.  We just need to jump and we are in.
jmp 08h:InPMode
xor ax, ax ;An instruction to jump over.
use32
InPMode:
hlt ;HLT was here for testing purposes.  
;Latin Data
;Null Segment
GDT1 dq 0h
;Code Segment.  00000000-FFFFFFFFh.  Ring zero, non-conforming, readable, 32-bit ops.
GDT2 dw 0FFFFh ;Bits 15-0 of limit (the top part)
dw 0000h ;Base bits 15-0
db 0h ;Base 23-16
db 10011011b ;Present, ring 0, executable, non-conforming, readable.
db 11001111b ;4 KB, 32-bit ops, 2 reserved bytes, bits 19-16 of limit
db 0h ;Bits 31-27 of base.
;Data Segment.  00000000-FFFFFFFFh.  Ring zero, expands up, writeable, 32-bit data.
GDT3 dw 0FFFFh
dw 0000h
db 0h
db 10010011b ;Present, ring 0, non-executable, writeable
db 11001111b ;4 KB, 32 bit data (for stack anyways), high bits of base.
db 0h

GDTRSize dw 23d
GDTRLinAdd dd ? ;Linear address of Global Descriptor Table
    

_________________
GhostRider
Post 05 Feb 2007, 23:38
View user's profile Send private message Visit poster's website AIM Address Reply with quote
pfranz



Joined: 13 Jan 2007
Posts: 116
Location: Italy
pfranz 07 Feb 2007, 11:47
What do you expect from this code? If it is right (I haven't checked) it jumps to the halt instruction and stops there, since interrupts are disabled.
What you see is a frozen machine, with only power / reset buttons working ...
Post 07 Feb 2007, 11:47
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 07 Feb 2007, 22:34
That code seems ok but there are a few safety precautions you should take when setting up selectors and also some nice tricks to let FASM do some of the work for you:

Code:
use16
org 0x7C00

start:

cli ; Stop interrupts
xor ax,ax
mov ds,ax
mov es,ax
mov ss,ax
mov sp, 0x7C00h
; sti ; Uncomment if code used below

; Other Real Mode Stuff Here
; If required...

;cli ; Uncomment if code used above
lgdt [GDTR]     ; Load GDT

mov eax,cr0     ;\
or eax,1        ; Set PM bit
mov cr0,eax     ;/

jmp pword 0x0008h:PMode ; pword is imporatant here

use32
PMode:
; Must do this first
mov ax,0x0010   ; Data selector
mov ds,ax
mov es,ax
mov ss,ax       ; Can use a different selector if required
mov sp,0x00007C00 ; Can be changed

; Protected Mode Code Here

jmp $           ; Infinite loop

align 16

GDT1:           ; Null Selector
        dq 0

GDT2:           ; Code Selector
dw 0xFFFF       ; Bits 15-0 of limit (the top part)
dw 0x0000       ; Base bits 15-0
db 0x00         ; Base 23-16
db 10011011b    ; Present, ring 0, executable, non-conforming, readable
db 11001111b    ; 4 KB, 32-bit ops, 2 reserved bytes, bits 19-16 of limit
db 0x00         ; Bits 31-27 of base

GDT3:           ; Data Selector
dw 0xFFFF       ;
dw 0x0000       ;
db 0x00         ;
db 10010011b    ; Present, ring 0, non-executable, writeable
db 11001111b    ; 4 KB, 32 bit data (for stack anyways), high bits of base.
db 0x00         ;

GDTR:           ; Global Descriptor Table Register
GDTRSize dw ($ - GDT1 - 1) ; Let FASM compute GDT size for you
GDTRLinAdd dd GDT1 ; Start of Global Descriptor Table                 
    


If this doesn't work there may be some problem with the GDT table (I havn't checked it)
Post 07 Feb 2007, 22:34
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.