Aug 13, 2011

Simple OS - note

While I was working on my simple operating system project. I found something interesting.
Almost all the x86 system boot up in 16-bit real mode. And the way to enable the protected mode is quite easy.
As osdev wiki suggest, using the following instruction can take us from real mode to protected mode.
mov  eax, cr0   ; switch to pmode by
or al,1         ; set pmode bit
mov  cr0, eax
But there are one thing that bother me a lot, since our code is still in the real mode, 
how can we use the 32-bit register and instructions.
After google for a while I found a very helpful website that completely solved my question.
answer in stackoverflow 
answer in nasm forum 
The answer is that
When intel introduced 32-bit code - they used the same opcodes! 
When using 32bit register in 16 bit real mode, assembler will place a prefix in front of the instruction. (0x66 according to the nasm forum) This tell the cpu that I'm using 32-bit register
in 16-bits real mode.

I take some picture to verified the result.
My environment is ubuntu 10.10 and gcc 4.4.5
I' using qemu and gdb to verified the result.

As you can see, there are 0x66 prefix in front of the mov eax, 0 instruction.

No comments:

Post a Comment