Page 1 of 1

How to jump R-Mode from 32-bit P-Mode

Posted: Wed May 18, 2011 7:36 am
by leetow2003
When I jump to R-Mode from 32-bit P-Mode,the PC is always down,why?
How to correct it?
Look:

Code: Select all

......
jump16   macro  selector,offsetv
         db     66h
         db     0eah       ;jmp
         dw     offsetv   ;2 bytes offset address
         dw     selector 
        endm
....
cseg1       segment   use16   
            assume  cs:cseg1
start:     
...
cli                 
            ;
            mov eax,cr0
            or eax,1
            mov cr0,eax
.....
toreal:    
           sti
           mov ax,4c00h
           int 21h           
cseg1       ends
;
cseg3       segment   use32   
               assume  cs:cseg3
 spm32:     
            mov eax,cr0
            and eax,0fffffffeh
            mov cr0,eax
            ;Return to code
            jump16 <seg toreal>,<offset toreal>
            ;

cseg3    ends

Re: How to jump R-Mode from 32-bit P-Mode

Posted: Wed May 18, 2011 1:01 pm
by DavidCooper

Code: Select all

......
jump16   macro  selector,offsetv
         db     66h
         db     0eah       ;jmp
         dw     offsetv   ;2 bytes offset address
         dw     selector 
        endm
You should explain that that's a prefixed far jump instruction. Is there some reason why you need to write this in a mixture of direct machine code and assembler? Is there some reason why you need a macro to do a simple far jump?

Code: Select all

....
cseg1       segment   use16   
            assume  cs:cseg1
start:     
...
cli                 
            ;
            mov eax,cr0
            or eax,1
            mov cr0,eax
.....
Do these lines of dots represent missing code that you've edited out? I'm guessing that they must do because I can't imagine that you've tried to switch to protected mode without setting up a GDT.

Code: Select all

toreal:    
           sti
           mov ax,4c00h
           int 21h           
Again there must be code missing, because it looks as if it then runs straight on into this, but clearly it can't.

Code: Select all

cseg1       ends
;
cseg3       segment   use32   
               assume  cs:cseg3
I don't know what that does, but I'm sure you know more about assembler than I do.

Code: Select all

 spm32:     
            mov eax,cr0
            and eax,0fffffffeh
            mov cr0,eax
            ;Return to code
            jump16 <seg toreal>,<offset toreal>
            ;

cseg3    ends
That appears to switch to real mode and then try to jump to the stuff further up via your macro. I assume the macro posts a far jump instruction into your code at this point. I don't think it's possible to jump straight back from 32-bit protected mode into real mode without going through 16-bit protected mode along the way. So far as I'm aware, you have to do a far jump into a 16-bit protected mode segment first, then switch to real mode, then do another far jump to load CS with a real mode value, and neither of those far jumps will take a prefix.

Re: How to jump R-Mode from 32-bit P-Mode

Posted: Wed May 18, 2011 6:25 pm
by leetow2003
So far as I'm aware, you have to do a far jump into a 16-bit protected mode segment first, then switch to real mode
I want to know why?

Re: How to jump R-Mode from 32-bit P-Mode

Posted: Wed May 18, 2011 6:53 pm
by Brendan
Hi,
leetow2003 wrote:
So far as I'm aware, you have to do a far jump into a 16-bit protected mode segment first, then switch to real mode
I want to know why?
If you don't you end up with real mode addressing , 32-bit default code size and strange segment limits; and everything crashes because nothing was designed to handle "32-bit real mode" (probably including the CPU's microcode).

I haven't tried it though - it might work in some limited/strange way.


Cheers,

Brendan

Re: How to jump R-Mode from 32-bit P-Mode

Posted: Thu May 19, 2011 2:28 pm
by DavidCooper
leetow2003 wrote:I want to know why?
Why would they add unnecessary circuitry to the processor to give you extra ways of doing the same thing? They designed it to work in one particular way and they expect you to follow their instructions and do it that way. Your code might work by chance if you remove the prefix and use a two-byte address, so you can try that if you like, but even if it does there's no guarantee that it will work on every machine.