Assembly Language Registers x86 Registers


Example

In the 32-bit world, the general-purpose registers fall into three general classes: the 16-bit general-purpose registers, the 32-bit extended general-purpose registers, and the 8-bit register halves. These three classes do not represent three entirely distinct sets of registers at all. The 16-bit and 8-bit registers are actually names of regions inside the 32-bit registers. Register growth in the x86 CPU family has come about by extending registers existing in older CPUs

There are eight 16-bit general-purpose registers: AX, BX, CX, DX, BP, SI, DI, and SP; and you can place any value in them that may be expressed in 16 bits or fewer.

When Intel expanded the x86 architecture to 32 bits in 1986, it doubled the size of all eight registers and gave them new names by prefixing an E in front of each register name, resulting in EAX, EBX, ECX, EDX, EBP, ESI, EDI, and ESP.

With x86_64 came another doubling of register size, as well as the addition of some new registers. These registers are 64 bits wide and are named (slash used to show alternate register name): RAX/r0, RBX/r3, RCX/r1, RDX/r2, RBP/r5, RSI/r6, RDI/r7, RSP/r4, R8, R9, R10, R11, R12, R13, R14, R15.

While the general purpose registers can be technically used for anything, each register also has an alternate/main purpose:

  • AX (accumulator) is used in arithmetic operations.
  • CX (counter) is used in the shift and rotate instructions, and used for loops.
  • DX (data) is used in arithmetic and I/O operations.
  • BX (base) used as a pointer to data (specifically as an offset to the DS segment register when in segmented mode).
  • SP (stack) points to the top of the stack.
  • BP (stack base) points to the base of the stack.
  • SI (source) points to a source in memory for stream operations (e.g. lodsb).
  • DI (destination) points to a destination in memory for stream operations (e.g. stosb).

Segment registers, used in segmented mode, point to different segments in memory. Each 16-bit segment register gives a view to 64k (16 bits) of data. After a segment register has been set to point to a block of memory, registers (such as BX, SI, and DI) can be used as offsets to the segment register so specific locations in the 64k space can be accessed.

The six segment registers and their uses are:

RegisterFull nameDescription
SSStack SegmentPoints to the stack
CSCode SegmentUsed by the CPU to fetch the code
DSData SegmentDefault register for MOV operations
ESExtra SegmentExtra data segment
FSExtra SegmentExtra data segment
GSExtra SegmentExtra data segment