(The following code has been written in the nasm style)

org 0100h

LABEL_DESC_DATA:  Descriptor    0,      DataLen-1, DA_DRW    ; Data
LABEL_DESC_STACK:  Descriptor    0,    TopOfStack, DA_DRWA+DA_32; Stack, 32bit
.... (Descriptor is the macro used here for
Descriptor .
attributes such as DA_DRW,DA_DRWA .. stand for
DA_ : Descriptor Attribute
D : data segment
C : code segment
S : system segment
R : read only
RW: read and write
A : accessed
) ...
;gdt selector
; end of

; data segment

SPValueInRealMode dw 0
; strings for out put testing
PMMessage: db "In Protect Mode now. ^-^", 0 ;to be printed in protected mode
OffsetPMMessage equ PMMessage - $$
OffsetStrTest equ StrTest - $$
DataLen equ $ - LABEL_DATA
; END of

mov ax, cs
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0100h
(the following code just fill in the descriptor's base address with the ds value)
; initialize the data segment's contain
xor eax, eax
mov ax, ds
shl eax, 4
add eax, LABEL_DATA
mov word , ax
shr eax, 16
mov byte , al
mov byte , ah

; initialize the stack segment's contain
xor eax, eax
mov ax, ds
shl eax, 4
add eax, LABEL_STACK
mov word , ax
shr eax, 16
mov byte , al
mov byte , ah

I was learning how to switch between the real mode and protected mode on a common Intel\IA32 machine, and the segment 'data and stack' are intended to be used after entering the protected mode. But I wonder why should the base address match the value in ds register when still in real mode. In that case, the code of real mode will be overwritten when excuting the code in protected code!

Please anyone gives a brief explaination or suggestion of reading, thanks a lot!
Posted on 2010-01-25 22:07:01 by JackCrital2005
I can't follow your code past "mov sp, 100h", Jack. What's that for? What's the purpose of the different section names? Your gdtr needs a linear address for the gdt, so that will have to be adjusted by ds * 16. I don't know about the rest of it. You seem to be defining "OffsetPMMessage" from the start of "section .data1". I guess the base is being adjusted to match the start of "section .data1"??? I'm confused.

I can probably dig up an example of switching to Pmode from a .com file, but it may not be what you're trying to do. It's been a while since I did anything with this, so my memory's pretty fuzzy. (an "interesting" thing to know how to do, but not really very "Practical")


Posted on 2010-01-26 14:22:59 by fbkotler
I'm very sorry Frank, the thing seems fuzzy because the offset is predefined. and because the code is for brief introduction
and premature practice so the initialization of the base address has been neglected.

I'm a newbie to IA32 programming, so questions turn puzzling even for experts simply because the lack of some basic knowledge.
Thanks a lot any way.

Posted on 2010-01-26 17:36:40 by JackCrital2005