flat assembler
Message board for the users of flat assembler.

Index > Main > Strange stack problem in PM

Author
Thread Post new topic Reply to topic
Tyler Durden



Joined: 24 Feb 2004
Posts: 50
Tyler Durden 23 Nov 2004, 10:23
Hi, that's me again Smile I wrote tiny (acctually it's not so tiny, but I've remove some stuff (tasks,ints a.s.o., to simplify it) proggy that turns CPU in protected mode. But it results a CPU reset after (and only after !) exiting the proggy (if I don't setup "SS" register, it becames to work properly... why ?)

P.S. The problem is not in "RETN" instruction, you can exit by "MOV AH,4CH;int 21h", but the result will be the same...

Code:
;=[code16]===================================================================;
org 100h
use16
;test if in v86
mov eax,cr0
test al,1
jz .noV86
retn
.noV86:
;get descriptors bases
xor eax,eax
mov ax,cs
mov [code16],ax
shl eax,4
push eax
mov word [descriptorCode32+2],ax
mov word [descriptorData32+2],ax
mov word [descriptorCode16+2],ax
shr eax,16
mov byte [descriptorCode32+4],al
mov byte [descriptorData32+4],al
mov byte [descriptorCode16+4],al
;load gdtr
pop eax
add eax,gdt
mov dword [gdtr+2],eax
lgdt fword [gdtr]
;open a20
in al,92h
or al,2
out 92h,al
;disable interrupts (+nmi)
cli
in al,70h
or al,80h
out 70h,al
;switch to PM
mov eax,cr0
or al,1
mov cr0,eax
;jump to selectorCode32
db 66h
db 0eah
dd $+6
dw selectorCode32
;=[code32]===================================================================;
use32
;initialize segment registers
push selectorData32
pop ss
mov esp,stackTop
push selectorData32
pop ds
;initialize eflags (clear nt)
pushfd
pop eax
and eax,0ffffbfffh
push eax
popfd
;jump to selectorCode16
db 0eah
dd $+6
dw selectorCode16
use16
;switch to real mode
mov eax,cr0
and al,0feh
mov cr0,eax
;load cs/clear cashe
db 0eah
dw $+4
code16 dw ?
;set ss/ds/sp
mov ax,cs
mov ss,ax
mov ds,ax
mov sp,0fffeh
;enable interrupts (+nmi)
in al,70h
and al,07fh
out 70h,al
sti
;get out
retn
;=[const32]==================================================================;
;selectors
selectorCode32=1000b
selectorData32=10000b
selectorCode16=11000b
selectorFlat=100000b
;=[data32]===================================================================;
;global descriptors table
align 8
gdt:
rb 8
descriptorCode32 db 0ffh,0ffh,0,0,0,10011010b,11001111b,0
descriptorData32 db 0ffh,0ffh,0,0,0,10010010b,11001111b,0
descriptorCode16 db 0ffh,0ffh,0,0,0,10011010b,0,0
descriptorFlat db 0ffh,0ffh,0,0,0,10010010b,11001111b,0
gdtr dw $-gdt-1
dd ?
;stack
rb 200h
stackTop:
    


Description:
Download
Filename: PM.ASM
Filesize: 1.92 KB
Downloaded: 416 Time(s)


_________________
Image
Post 23 Nov 2004, 10:23
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
MazeGen



Joined: 06 Oct 2003
Posts: 977
Location: Czechoslovakia
MazeGen 21 Dec 2004, 19:05
I guess the processor could be confused by code sequence

push selectorData32
pop ss

The "pop ss" initally interprets the value of SS like in real mode, but the instruction changes SS selector to protected mode logic and additionally uses SS itself - there's probably some conflict.
Try "mov reg,selectorData32 + mov ss, reg" or "lss sp" instruction, it could work.
Post 21 Dec 2004, 19:05
View user's profile Send private message Visit poster's website Reply with quote
Tyler Durden



Joined: 24 Feb 2004
Posts: 50
Tyler Durden 21 Dec 2004, 21:06
Nop Sad "pop ss" are suitable (as intel says) command for loading SS register... The interrupts even disabled after it, so the next command must be mov esp,somthing... Anyway mov ss,ax doesn't work Sad

_________________
Image
Post 21 Dec 2004, 21:06
View user's profile Send private message Visit poster's website ICQ Number 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.