Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 416
mike820324 Avatar asked Aug 02 '11 19:08

mike820324


People also ask

Can you use 32-bit registers in real mode?

Starting with the 80386, real mode programs can use the 32 bit registers with the Address Size Override Prefix. This allows programs to use an address like DS:[EBX]. In normal real mode, a fault occurs if EBX exceeds 0xFFFF. In unreal mode, the access is allowed.

How many registers are used in real mode?

Thus, it was possible to address at most 1 MB of memory in real mode. There are in total six segment registers: CS: This register contains the base address of the currently used code segment. DS: This register contains the base address of the currently used data segment.

Can I use EAX in real mode?

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

When using a 32-bit register to address memory in the conventional mode contents of the register must?

When using a 32-bit register to address memory in the real mode, contents of the register must never exceed 0000FFFFH.


2 Answers

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.

like image 73
ninjalj Avatar answered Mar 16 '23 14:03

ninjalj


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.

like image 26
Chi-Lan Avatar answered Mar 16 '23 14:03

Chi-Lan