开发者

Is it possible to use 32 bits registers/instructions in real mode?

开发者 https://www.devze.com 2023-03-25 07:28 出处:网络
I\'m confused about a simple assembly problem when studying some simple os source code. In this website: http://wiki.osdev.org/Babystep7 the following code is to switch from real mode to protected mo

I'm confused about a simple assembly problem when studying some simple os source code.

In this website: http://wiki.osdev.org/Babystep7 the following code is to switch from real mode to protected mode

mov  eax, cr0
or al,1
mov  cr0, eax

I know how to s开发者_如何转开发witch from real mode to protected mode.

But my question is since the program is still in the real mode, how can it use the 32 bit registers or instructions?

Is it possible to use 32 bits registers/instructions in real mode?


When the processor operates in real mode (as it is immediately after booting), it defaults to 16-bit code. However, this does not mean that you are unable to use 32-bit instructions.

There is an "operand size override" prefix (66h) that changes the default mode for a single instruction. When this prefix is used with an instruction executed in 16-bit real mode, it will switch the instruction to 32-bit. Conversely, when this prefix is used with an instruction executed in 32-bit protected mode, it will switch the instruction to 16 bit. (A similar prefix, 67h, works to override address sizes.)

Using this prefix, then, allows you to use 32-bit registers in 16-bit real mode. Your assembler will almost certainly emit this prefix automatically when you try and use 32-bit operands with an instruction when assembling 16-bit code.

Unfortunately, there is no such override prefix for 64-bit instructions, so these cannot be used in real mode. You need to switch into "long mode" to allow these.


As far as I understand, real mode does not affect the commands you can run on the CPU, but it affects how the CPU memory reference commands are interpreted.

So, yes, you can use eax, but you won't be able to get the [eax] memory cell.

See relevant part in Intel's Manual.


For simple addressing the the operand and instruction size prefixes worked very well. A 16-bit protected (initally real) mode application I once wrote for Windows (3.1 and later to 9x) could allocate >64K memory areas using the Windows memory API and the question was how to make use of it. In any case using (far) pointer thunking and the prefixes mentioned my app made good use of 40MB areas even though it ran in 16-bit mode.

If you try something similar remember that the instruction size prefix enables the 32-bit instruction set which is incompatible with the 16-bit. 16-bit normally doesn't care if you're in real or protected mode unless you're doing segment arithmetic. So you need to hand code (at least I did) the 32-bit operations using emit because your compiler probably won't generate them without yelling bloody murder.


You can perhaps use the LoadAll opcode 0F07h which gives you 32 bit access in 16 bit real mode.


As far as I know,in real mode,you cannot use 32 bit registers. In the 32-bit control register CR0, real mode and protected mode is determined by looking to first bit of CR0 (PE).In this code,you change the PE in the last line (mov cr0,eax). I guess, after this line,you cannot use 32 bit register references anymore.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号