flat assembler
Message board for the users of flat assembler.

Index > OS Construction > Pmode help

Author
Thread Post new topic Reply to topic
xleelz



Joined: 12 Mar 2011
Posts: 86
Location: In Google Code Server... waiting for someone to download me
xleelz 10 Jun 2012, 21:40
Been way too long since I've tried my hand at OS Dev..
I'm working on a 2 stage FAT12 bootloader, and upon reaching PMode in the second stage, it seems to just restart... any idea what went wrong?
here's the code:
Code:
use16
org 500

start:
       cli
 xor ax,ax
   mov ds,ax
   mov fs,ax
   mov gs,ax
   mov es,ax
   mov ax,0x9000
       mov ss,ax
   mov sp,0xffff


       pusha
       lgdt [gdtr]
 popa

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

     jmp codeSeg:Pmode

gdt:
       dq 0
        
codeSeg:
    dw 0xffff
   dw 0
        db 0
        db 10011010b
        db 11001111b
        db 0
dataSeg:
        dw 0xffff
   dw 0
        db 0
        db 10010010b
        db 11001111b
        db 0
gdtEnd:

gdtr:
        dw gdtEnd - gdt - 1
 dd gdt

use32

Pmode:
       mov ax,dataSeg
      mov es,ax
   mov fs,ax
   mov gs,ax
   mov ds,ax
   mov ss,ax
   mov esp,90000h

  mov edi, 0xB8000
    mov ah,1fh
  mov al,'A'
        mov word [edi],ax

loopr:
 hlt
 jmp loopr    

_________________
The person you don't know is the person that could help you the most... or rape you, whichever they prefer.
Post 10 Jun 2012, 21:40
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 10 Jun 2012, 21:59
The pusha/popa around the lgdt isn't needed. You probably just need to rebase the selectors:
Code:
; ...
jmp (codeSeg - gdt):Pmode
; ...
mov ax,(dataSeg - gdt)
; ...    
Post 10 Jun 2012, 21:59
View user's profile Send private message Reply with quote
xleelz



Joined: 12 Mar 2011
Posts: 86
Location: In Google Code Server... waiting for someone to download me
xleelz 11 Jun 2012, 01:02
thanks for the quick response but it still reboots... I did some debugging and it seems to make it into protected mode, fs and gs aren't set to the data segment but ds, ss, and esp are..
Post 11 Jun 2012, 01:02
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20632
Location: In your JS exploiting you and your system
revolution 11 Jun 2012, 02:03
xleelz wrote:
Code:
        mov sp,0xffff    
It is probably a good idea to align your stack properly.

Perhaps this instead:
Code:
xor sp,sp ;sp=0 to give a 64k stack aligned to 0 mod 4    
Post 11 Jun 2012, 02:03
View user's profile Send private message Visit poster's website Reply with quote
BAiC



Joined: 22 Mar 2011
Posts: 272
Location: California
BAiC 11 Jun 2012, 07:12
"codeSeg" is a label just as much as Pmode (Pmode is the label you want)...

codeSeg needs to be a selector.. from your code it looks like it needs to be 8..

Code:
jmp 8:Pmode     

I agree with cod3b453; get rid of the pusha/popa. I also agree with revolution; align your stack...

furthermore; your "hlt" loop at the end is terminal unless you enable interrupts.. since you don't have an IDT that won't actually matter.
Post 11 Jun 2012, 07:12
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 794
Location: Adelaide
sinsi 11 Jun 2012, 07:22
"org 500" - how do you load it there? That's part of the interrupt table.
Possibly at the top of 640K there is the EBDA, maybe your stack is causing some problem?
Post 11 Jun 2012, 07:22
View user's profile Send private message Reply with quote
xleelz



Joined: 12 Mar 2011
Posts: 86
Location: In Google Code Server... waiting for someone to download me
xleelz 11 Jun 2012, 15:07
Quote:

Possibly at the top of 640K there is the EBDA, maybe your stack is causing some problem?

maybe, but it does seem to work past that point. Debugging says it reboots when it loads the gdt, apparently it's invalid.

Thanks for all the replies and I made the respective changes.
Post 11 Jun 2012, 15:07
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 11 Jun 2012, 17:37
The IVT is at 0x00000-0x00400 (256 * (2+2)). The BDA is 0x00400-0x00500 so org 0x500 should be fine. (The EBDA is usually at 0xE0000). Also might be worth trying:

Code:
jmp pword (codeSeg - gdt):Pmode
; ...
align 4
Pmode:    
Post 11 Jun 2012, 17:37
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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.