32-bit x86 assembly programming

This is an old revision of this page, as edited by 213.101.123.22 (talk) at 20:46, 25 January 2004. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

This page should be considerd to belong to X86 assembly language.

Protected mode is the mode in which most modern operating systems run their code. When the computer boots, it first enters real mode; the operating system is responsible for switching into protected mode.

See x86-assembly language in realmode for a better understanding of this article.

The 80286 processor did have a 16-bit protected mode. It is of historic interest only. We will concentrate on the 32-bit protected mode, wich is found on the 80386 processors and all its successors.

All the general application registers (AX, BX, CX, DX, SI, DI, SP and BP) are extended to a total of 32 bits. To denote that you intend the 32-bit register instead of the low 16-bit part, add an E before the name of the register..

EAX, EBX, ECX, EDX, ESI, EDI, ESP and EBP

The segment registers remain 16 bits wide and does not change names.

CS, DS, ES, FS, GS and SS

The segment registers does not work like they did in realmode. Instead, they are used to point out an selector in a table pointed to by the GDTR- or LDTR-register.

The FLAGS- and IP-registers are also extended to 32 bits.

EIP, EFLAGS

There are also some new registers. They are useful only to system programmers.

CR0, CR2, CR3, CR4, GDTR, LDTR, IDTR, TR..

They are used by the operating system and cannot be accessed by ordinary user programs.

There are also some other registers, like the test registers, debug registers, MMX, MSR, XMM and others.

How to switch to protected mode:

* load GDTR with the pointer to the GDT-table.
* load IDTR with the pointer to the IDT OR dissable interrupts.
* set the PE-bit in the CR0-register.
* make a far jump to the 32-bit code to flush the PIQ.
* initialize TR with the selector of a valid TSS.
* optional: load LDTR with the pointer to the LDT-table.