简体   繁体   中英

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

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 switch 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.

您也许可以使用LoadAll操作码0F07h ,它可以在16位实模式下提供32位访问。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM