flat assembler
Message board for the users of flat assembler.

flat assembler > OS Construction > Simple protected mode OS

Author
Thread Post new topic Reply to topic
Tommy



Joined: 17 Jun 2003
Posts: 492
Location: Norway
Code:
        format  binary

        use16
        org     0x0000

        jmp     0x07C0:boot.start

boot:
    .start:
        mov     ax,cs
        mov     ds,ax           ; ds = cs
        mov     es,ax           ; es = cs

    .reset:
        xor     ax,ax
        xor     dx,dx
        int     0x13            ; reset disk system
        jc      boot.reset

    .read:
        mov     ax,0x1000
        mov     es,ax
        xor     bx,bx
        mov     ax,0x0205
        mov     cx,0x0002
        xor     dx,dx
        int     0x13            ; read sectors into memory
        jc      boot.read

    .init:
        cli                     ; disable interrupts
        cld                     ; clear direction flag

        in      al,0x92
        or      al,2            ; enable the fast A20 gate
        out     0x80,al         ; 1ms delay
        out     0x92,al

        lgdt    [gdt]           ; load GDT table
        mov     eax,cr0
        or      al,1            ; set protected mode bit
        mov     cr0,eax

    .kernel:
        jmp     1000h:0000      ; jump to kernel

        align   8
gdt:    dw      0x0017          ; GDT table
        dd      gdt
        dw      0x0000
        dw      0xFFFF,0x0000,0x9A00,0x00CF
        dw      0xFFFF,0x0000,0x9200,0x00CF

boot.signature:
        times   510-$ db 0      ; bootable floppy disk signature
        dw      0xAA55

        use32                   ; in protected mode
        org     0x0000
kernel:
        mov     eax,cs
        mov     ds,eax          ; ds = cs
        mov     es,eax          ; es = cs

        mov     esp,0x7C00      ; initialize return stack

        mov     edi,0xB800
        mov     ah,4
        mov     al,4
        stosw                   ; show character on screen

        jmp     $               ; hang     
Ok, guys... Warning Laughing: I'm a newb in OS programming! Embarassed I'm trying to write a simple OS which gets into protected mode and outputs a character to the screen... What am I doin' wrong?? My computer just restarts (an error occurs... Sad)... Sad Can someone please help me?

Best regards,
Tommy
Post 11 Feb 2004, 16:54
View user's profile Send private message Visit poster's website Reply with quote
Ralph



Joined: 04 Oct 2003
Posts: 86
Hey,
I'm currently too lazy to look at the code, but why don't you run it through bochs? If you have windows, the win32 version comes with dochsdbg which has a debugger compiled in. Under Linux you can attach gdb. See where it's erroring out (step through if you have to, it's not a lot of code) and go from there.
http://bochs.sourceforge.net/ if you didn't know already.
Post 12 Feb 2004, 07:18
View user's profile Send private message Reply with quote
Gomer73



Joined: 29 Nov 2003
Posts: 151
Need to change your way of thing about segments, still stuck in real mode thinking.

GDT base address is wrong. The GDT base address(which is the dword right after your GDT: label) should be the linear physical address. To correct this you could put in the following lines:

gdt: dw 0x0017 ; GDT table
dd gdt + 0x7c00

Once you are in protected mode, you need to use your protected mode segments. You only have two of these. They can simply be addressed as 0x08 for code segment and 0x10 for data segment.
So your jump to protected mode should be something like this:

jump 8:0x10000 ; base of segment is 0

Here is the kernel code that should work:
mov eax,0x10
mov es,eax ; only thing we need for a stosw instruction
mov ss,eax
mov esp,0x7C00 ; initialize return stack
; no need to at this point since interrupts
; are disabled and we aint making any calls
mov edi,0xB8000
mov ah,4
mov al,4
stosw ; show character on screen
hang_loop
inc word [0xB8002] ; just to give us something nice to look at
jmp $ ; hang

One other really useful hint I found that somebody else came up with(can't remember exactly who) is you can track your steps in your coding. Basically initialize the GS to 0xB800 and then write something to the screen at every step. For example:

mov ax,0xB800
mov gs,ax
mov word [gs:4], 0x1731 ; I've always liked the color blue
.reset:
xor ax,ax
xor dx,dx
int 0x13 ; reset disk system
jc boot.reset

mov word [gs:4], 0x1732

This even works in protected mode.
Post 12 Feb 2004, 08:42
View user's profile Send private message Reply with quote
Gomer73



Joined: 29 Nov 2003
Posts: 151
There should actualy be a colon after the hang loop. It should be:

hang_loop:
inc word [0xB8002] ; just to give us something nice to look at
jmp hang_loop
Post 12 Feb 2004, 08:45
View user's profile Send private message Reply with quote
Tommy



Joined: 17 Jun 2003
Posts: 492
Location: Norway
Thanks Gomer! I'll test it when I get home from school... Cool
Ralph: yes, I know about Bochs... I use Connecix Virtual PC... Maybe I should try Bochs... Smile
Post 12 Feb 2004, 09:22
View user's profile Send private message Visit poster's website Reply with quote
Tommy



Joined: 17 Jun 2003
Posts: 492
Location: Norway
Well, I have a short beak now, so I tested it, but it doesn't work... :'( (attaching the code from A20 gate is enabled...)
Code:
        ...
        lgdt    [gdt]                   ; load GDT table
        mov     eax,cr0
        or      al,1                    ; set protected mode bit
        mov     cr0,eax

        jmp     0x0008:0x1000           ; jump to kernel

        align   8
gdt:    dw      0x0017                  ; GDT table
        dd      0x7C00+gdt
        dw      0x0000
        dw      0xFFFF,0x0000,0x9A00,0x00CF
        dw      0xFFFF,0x0000,0x9200,0x00CF

        times   510-$ db 0              ; empty space
        dw      0xAA55                  ; signature

        use32                           ; kernel (in protected mode)
        org     0x0000
kernel:
        mov     eax,0x10
        mov     es,eax                  ; only thing we need for a stosw instruction
        mov     ss,eax
        mov     esp,0x7C00              ; initialize stack
        mov     edi,0xB8000
        mov     ah,4
        mov     al,4
        stosw                           ; show character on screen
    .hang_loop:
        inc     word [0xB8002]
        jmp     .hang_loop              ; hang 
    
Post 12 Feb 2004, 10:24
View user's profile Send private message Visit poster's website Reply with quote
Gomer73



Joined: 29 Nov 2003
Posts: 151
The jump is wrong.

It should be:

jmp 0x0008:0x10000

not

jmp 0x0008:0x1000

Real mode segment registers are multiplied by 0x10 to get the physical address. With the 0x0008 segment, it starts at physical address 0, so in order to jump to real mode adress 0x1000:0 in protected mode, you need to jump to 0x0008:0x10000.

PS: John Fine is the one that I got the GS stuff from(I think)
Post 12 Feb 2004, 11:25
View user's profile Send private message Reply with quote
Gomer73



Joined: 29 Nov 2003
Posts: 151
Whups,
Forgot, you might also want to initialize the ds or es: the code I added at the end.

kernel:
mov eax,0x10
mov es,eax ; only thing we need for a stosw instruction
mov ds,eax
mov ss,eax

or
.hang_loop:
inc word [es:0xB8002]
jmp .hang_loop ; hang

Sorry about that.
Post 12 Feb 2004, 11:29
View user's profile Send private message Reply with quote
Tommy



Joined: 17 Jun 2003
Posts: 492
Location: Norway
Ok, I see... Thanks! Smile I'll test it again tonight... I'm at school now... Sad Take care!

Best regards,
Tommy
Post 12 Feb 2004, 13:39
View user's profile Send private message Visit poster's website Reply with quote
Ralph



Joined: 04 Oct 2003
Posts: 86
I know nothing about Connecix, but if it doesn't let you set break points, trace instructions, etc, you're missing out big.
Post 12 Feb 2004, 18:15
View user's profile Send private message Reply with quote
Tommy



Joined: 17 Jun 2003
Posts: 492
Location: Norway
Connectix doesn't.... so I better try Bochs... Wink
Thanks for all help Gomer! Finally it works!!! JIPPI! Very Happy
Post 12 Feb 2004, 19:49
View user's profile Send private message Visit poster's website Reply with quote
Crunch



Joined: 09 Dec 2003
Posts: 10
Tommy,

I'm really a newbie and enjoyed this thread even though I didn't understand as much as I should. It would be a big help if you wouldn't mind posting your debugged code for me to fumble through experiment with and see if I can learn something in the process Very Happy Thanks
Post 13 Apr 2004, 21:50
View user's profile Send private message Reply with quote
thomasantony



Joined: 18 Aug 2004
Posts: 8
Location: Kerala, India
Hi,
Its my first post here. Smile . I use Bochs regulerly to test my OS. But is it possible to put breakpoints in Bochs(in Windows). Smile

Thomas Antony

_________________
C is Moronic, C Programers are morons!! Very Happy

My website : http://www.tomasm.tk
Post 18 Aug 2004, 10:59
View user's profile Send private message Visit poster's website Reply with quote
Tommy



Joined: 17 Jun 2003
Posts: 492
Location: Norway
Hi Crunch!

I'm posting my working code... I haven't done anything on it for months... So it's just a demo on how to get into PM...

Best regards,
Tommy


Description: My working code...
Download
Filename: 2my.zip
Filesize: 2.37 KB
Downloaded: 411 Time(s)

Post 18 Aug 2004, 13:18
View user's profile Send private message Visit poster's website Reply with quote
Crunch



Joined: 09 Dec 2003
Posts: 10
Very Happy Thanks Tommy!

I know I'll learn a lot stumbling through it. Rolling Eyes
Post 18 Aug 2004, 15:03
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-2019, Tomasz Grysztar.

Powered by rwasa.