x86 Processor Modes

I wanted to continue my theme of talking about CPU architecture since I do a lot of it for OS development and it’s actually pretty interesting. Today’s blog post is going to be on the different modes in an x86 (and therefore x86_64) based system CPU.

What are modes?

Anyone who has programmed assembly (or used inline assembly in C) knows that instead of variables, you get a list of predefined registers (some of which have specific purposes). These registers, and their total hex byte space, depend on the processor mode being used.

These registers build off of each other, something that I will explain later. For now, each mode is used to indicate a different address space. Each CPU begins at 16-bit mode, then the OS works its way to 32 or even 64-bit mode.

Now let’s talk about a few modes.

A few examples of modes

There are many modes in an x86 processor, however, here are a few common ones:

  • Real Mode – The mode every x86 CPU starts off in. This mode has access to (in theory) a total of 1 MB of addressable memory, however the actual amount of usable memory is much less. BIOS interrupts can be used to detect more memory. Real mode is very limited, so all modern OSes must switch to at least protected mode.
  • Protected Mode – This is the actual 32-bit mode the CPU is designed for. This mode has complicated memory standards, and works with several virtual address spaces (each of which has 4GB of addressable memory). This enables the system to use I/O and memory protections as well as restrict available instruction set (I’ll write an article on rings later). However, this mode comes at a cost where BIOS interrupts cannot be used.
  • Long Mode – Available on the x86_64 instruction set, long mode is the highest current mode a CPU can jump to. It has access to, wait for it, 18.4 EXABYTES of memory. That is so much memory. This mode is much more complex, it requires memory paging and does not support virtual 8086 or hardware task switching (but it can also run 16-bit protected mode code using the CS register)
  • Virtual 8086 Mode – If you couldn’t get enough of the real mode interrupts, you can basically have a protected mode CPU “emulate” a 16-bit real mode machine. Entering this is a lot more complicated than other modes, but it’s very useful (alternatively, you can use BIOS32 – note that none of these methods work in long mode)

What’s all this about registers?

While these modes change a lot of things like memory addresses, SSE registers, etc, one of the main things they change is registers. The registers heavily relate to each memory address, as mentioned before.

Let’s do an example – AX (real mode register) can only store 16-bit values (since that’s what real mode can do). EAX (protected mode register) can store 32-bit values, but is not an entirely new register

Rather, EAX is an extension of AX. AX is the lower 16 bits of EAX, while EAX is the full 32-bit value. The same applies to long mode registers like RAX.

It’s a bit more complicated than this but that’s the basic overview.

That’s all!

Thanks for reading!

Leave a Comment