Page 1 of 1

switching from real to protected in assembly

Posted: Sun Jul 15, 2007 1:32 pm
by Ninjarider
ok. using a .386p

theres a struct for Desc and a struct for table reg
temp_gdt_null_desc is a Desc
temp_gdt_scratch is a table_reg

the following code has a 66h proceding it
mov ebx, offset temp_gdt
mov dword ptr [ebx], 0
mov dword ptr [ebx] + 4, 0
mov dword ptr [ebx] + 8, Linear_Proto_hi //0000ffffh
mov dword ptr [ebx] + 12, linear_proto_lo //000cf9200h

mov temp_gdt_scratch.table_linear, ebx
mov temp_gdt_scratch.table_lim, 15

lgdt temp_gdt_scratch


mov ebx, cr0 // is not lead by a 66h
or ebx, 1
mov cr0, ebx ///////////// for some reason when it executes this line the computer reboots

Posted: Sun Jul 15, 2007 1:59 pm
by niteice
You sure it's cr0 and nor cro?

Posted: Sun Jul 15, 2007 2:22 pm
by Ninjarider
its suppose to be cr0 that was a mistype.
i know its cr0 because it assembles correct and doesn't give me and invalid operand.

Posted: Sun Jul 15, 2007 3:51 pm
by Dex
Do you do something like this in you original ?

Code: Select all

lgdt  [source]
And do you disable int ?

Posted: Sun Jul 15, 2007 4:23 pm
by exkor
Why don't you go with some template for PM switching there are plenty out there. And maybe static GDT.

Example, Fasm:

Code: Select all

macro descr name, lim1, limit, pDPLs, type, gDBavl, base{
 .#name#.sel       = (($-GDT)/8) shl 3
 .#name:           dw limit
 .#name#.base3     dw (base) and 0ffffh
 .#name#.base2     db ((base) shr 16) and 0ffh
 .#name#.flags2:   db pDPLs#type#b
 .#name#.flags1:   db gDBavl#lim1#b
 .#name#.base1     db (base) shr 24
}

align 8
GDT:
  dq 0          ;                           L
;               [   LIMIT   ][PDLS  TYPE] GD0A  BASE
  descr data,    1111,0ffffh, 1001, 0010, 1100, 0
  descr code,    1111,0ffffh, 1001, 1010, 1100, 0
  descr code64,  1111,0ffffh, 1001, 1010, 1010, 0
  .reg:
  .limit  dw $-GDT-1
  .addr   dq GDT     

use16
 lgdt [cs:GDT.reg]           ; load GDT
  mov  eax, cr0
  or   al, 1                  ; set PE flag
  mov  cr0, eax               ; enable protected mode
  jmp  GDT.code.sel:PMode32   ; jump to protected mode segment

use32
PMode32: 
  mov  eax, GDT.data.sel ; load 4 GB data descriptor
  mov  ds,ax             ; to all data segment registers      


and you'll need to disable interupts

Posted: Sun Jul 15, 2007 6:24 pm
by 1234
[post deleted]

Posted: Sun Jul 15, 2007 6:46 pm
by Ninjarider
i do diable interrupts.

i will try a static gdt when i get home