Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Assembly Language - Registers

Assembly Language - Registers

Registers (Register) are high-speed storage units inside the CPU and the most frequently manipulated objects in assembly programming. Understanding registers is the key first step to learning assembly language well.


What is a register

A register is an ultra-high-speed small memory integrated inside the CPU chip, used to temporarily hold instructions, data, and addresses.

Unlike memory, registers are embedded inside the CPU; accessing them has almost zero latency, while accessing memory takes tens to hundreds of clock cycles.

In assembly, the vast majority of operations revolve around registers: data is loaded from memory into registers, the operation is performed in registers, and the result is stored back to memory.

You can think of registers as the CPU's "workbench". Tools on the workbench are always at hand, while memory is like a warehouse you have to walk to for fetching and storing.


Classification of x86 32-bit registers

The x86 32-bit architecture provides several types of registers, each with different uses. The figure below shows the complete register classification:

Each type of register is described in detail below:


General-purpose registers

General Purpose Registers are the most commonly used registers, for holding operand data and temporary results.

x86 provides 8 32-bit general-purpose registers:

32-bit 16-bit Low 8 bits High 8 bits (of low 16 bits) Main Use
EAX AX AL AH Accumulator, holds function return values and arithmetic results
EBX BX BL BH Base register, often used to hold a memory base address
ECX CX CL CH Counter, often used for loop counting and shifts
EDX DX DL DH Data register, holds the high-order results of multiplication/division
ESI SI SIL - Source index register, source address for string operations
EDI DI DIL - Destination index register, target address for string operations
EBP BP BPL - Base pointer, points to the bottom of the current stack frame
ESP SP SPL - Stack pointer, always points to the top of the stack

Naming convention: the E prefix means Extended (extended to 32 bits), and the X suffix means it can be split into high and low bytes.

Example

; File path: register_parts.asm

; Demonstrate access to parts of a register

section

.text

   

global

_start

_start

:

   

mov

eax

,

0x12345678

   

; The full 32-bit register

   

; At this point: EAX = 0x12345678

   

; AX = 0x5678 (low 16 bits)

   

; AH = 0x56 (high 8 bits, the high 8 bits of AX)

   

; AL = 0x78 (low 8 bits)

   

mov

ax

,

0xAABB

         

; Modify AX (low 16 bits)

   

; At this point: EAX = 0x1234AABB (high 16 bits unchanged!)

   

;       AX  = 0xAABB

   

;       AL  = 0xBB

   

mov

al

,

0xCC

           

; Modify AL (lowest 8 bits)

   

; At this point: EAX = 0x1234AACC (only the low 8 bits changed)

   

;       AX  = 0xAACC

   

;       AL  = 0xCC

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80

When modifying the low 16 bits of a 32-bit register (e.g. AX), the high 16 bits remain unchanged. But when using a 32-bit register as the destination operand, the entire 32 bits are overwritten. This is a place where beginners easily make mistakes.


Segment registers

Segment registers are used to specify the memory segment currently in use:

Registers Name Purpose
CS Code segment register Points to the segment containing the current instruction
DS Data segment register Points to the segment containing data
SS Stack segment register Points to the segment containing the stack
ES Extra segment register An additional data segment
FS Extra segment register General-purpose, often used for thread-local storage
GS Extra segment register General-purpose, often used for thread-local storage

When programming in 32-bit protected mode, the OS has already set up the segment registers, so you usually do not need to modify them manually.


Pointer and index registers

These registers are mainly used to access memory and hold memory addresses:

Registers Full name Purpose
EIP Instruction Pointer (Instruction Pointer) Points to the address of the next instruction the CPU will execute (not directly accessible)
ESP Stack Pointer (Stack Pointer) Points to the top of the stack; PUSH/POP instructions adjust it automatically
EBP Base Pointer (Base Pointer) Points to the bottom of the current function's stack frame, used to access function parameters and local variables
ESI Source Index (Source Index) Source address for string/memory operations
EDI Destination Index Destination address for string/memory operations

ESP and EBP cannot be freely used as ordinary general-purpose registers. ESP points to the top of the stack, and push/pop/call/ret all change it; EBP is key to accessing function parameters. Modifying them carelessly will crash the program.


Flags Register (EFLAGS)

EFLAGS is a 32-bit register where each bit represents a status flag (Flag).

You cannot directly read or write the entire EFLAGS, but the CPU automatically updates these flags based on operation results, and conditional jump instructions use the flags to decide whether to jump.

Flags Name Meaning
CF Carry Flag Set to 1 when an unsigned operation produces a carry/borrow
PF Parity Flag Set to 1 when the number of 1 bits in the low 8 bits of the result is even
AF Auxiliary Carry Flag Set to 1 when there is a carry/borrow from the low 4 bits to the high 4 bits
ZF Zero Flag Set to 1 when the operation result is 0
SF Sign Flag Set to 1 when the operation result is negative (equal to the highest bit of the result)
OF Overflow Flag Set to 1 when a signed operation overflows

Example

; File path: flags_demo.asm

; Demonstrate the effect of operations on the flags

section

.text

   

global

_start

_start

:

   

mov

eax

,

10

   

sub

eax

,

10

       

; 10 - 10 = 0

   

; ZF = 1 (result is zero)

   

; SF = 0 (result is non-negative)

   

; CF = 0 (no borrow)

   

mov

eax

,

0xFFFFFFFF

   

add

eax

,

1

         

; 0xFFFFFFFF + 1 = 0x100000000 (exceeds 32 bits)

   

; ZF = 1 (32-bit result is 0)

   

; CF = 1 (a carry is generated)

   

; OF = 0 (no overflow from a signed perspective)

   

mov

eax

,

1

   

mov

ebx

,

0

   

int

0x80


Register usage conventions

In real-world programming, some registers have customary usages—known as the calling convention (Calling Convention):

Registers Uses in the calling convention
EAX Holds the function return value
ECX Counter (loop counting)
EDX Holds the high-order result of division, extends EAX
EBX, ESI, EDI, EBP The called function must save and restore (callee-saved)
EAX, ECX, EDX The caller is responsible for saving (caller-saved)

When writing your own assembly programs, you do not have to strictly follow the calling convention. But if you want to mix with C code, you must follow conventions such as cdecl.