One of the main tools in x86 assembly programming is the registers in the CPU. These registers are like variables built into the CPU. Using registers instead of memory to store values can make processing faster and cleaner. The x86 family of processors has several different registers available for use. This article is to describe the main purpose of each register and how to use it. To be clear, the rules described here are more like recommendations for use than strict standards. Some operations do require certain types of registers to complete, but for most operations you are free to use any register.
Listed below are some of the processors offered by the 386 processor. It should be noted that although a 32-bit processor is listed, it can be broken down to use 16-bit or even 8-bit registers.
General Purpose Registers:
EAX EBX ECX EDX
Segment registers
CS DS ES FS GS SS
Index and pointer registers
ESI EDI EBP EIP ESP
Indication register
EFLAGS
general purpose register
General-purpose registers are the most frequently used registers, and most instructions use these registers. 32-bit general-purpose registers have corresponding 16-bit and 8-bit registers.
32位: EAX EBX ECX EDX
16位: AX BX CX DX
8位 : AH AL BH BL CH CL DH DL
The prefixes "H" and "L" for 8-bit registers represent the high and low byte parts. Here's a look at where they are mainly used:
EAX, AX, AH, AL: These are called accumulator registers
and are mainly used for I/O port access, calculations, interrupts, etc.
EBX, BX, BH, BL: These are called base registers,
which are mainly used to access memory as a base pointer, get the return value of some interrupts, and so on.
ECX, CX, CH, CL: These are called counter registers,
which are mainly used for loop counting and transfer, getting some interrupt values, etc.
EDX, DX, DH, DL: These are called data registers
for I/O port access, calculations, interrupts, etc.
segment register
The segment register is responsible for holding the address of the segment. These registers are only 16 bits, and they can only be set by general-purpose registers or some special instructions. The segment register acts as a pointer, which points to a segment descriptor that describes the starting address and size of a segment.
CS: Save the code snippet of the program you are running. Changing its value back crashes the computer.
DS: holds the data segment that your program accesses. Changing its value may get wrong data.
ES, FS, GS: These are additional segment registers used for far pointer addressing.
SS: Holds the stack segment used by your program. Sometimes its value is equal to the value of DS. Changing its value may cause unpredictable errors.
Indexes and pointers
The offset part of the address can be represented by the index and pointer registers. These registers can be combined with segment registers to indicate a far address. Registers prefixed with "E" can only be used in protected mode.
ES: EDI
EDI DI: Destination index register, used for copying and setting of strings, memory sequences, and in conjunction with ES can point to a far address.
DS: ESI
ESI SI: Source Index Register. Copies for strings and memory sequences.
SS: EBP
EBP BP: Stack base address register, saves the base address of the stack.
SS: ESP
ESP SP: Stack Frame Pointer Register. Save the address of the top of the stack.
CS: EIP
EIP IP: Index pointer. Save the offset of the next instruction. Read only.
EFLAGS register
The EFLAGS register holds the state of the processor. It is used by many instructions to compare some parameters, conditional loops, and conditional jumps. Each bit holds specific parameters of the last executed instruction.
Bit Label Desciption
0 CF Carry flag
2 PF Parity flag
4 AF Auxiliary carry flag
6 ZF Zero flag
7 SF Sign flag
8 TF Trap flag
9 IF Interrupt enable flag
10 DF Direction flag
11 OF Overflow flag
12-13 IOPL I/O Priviledge level
14 NT Nested task flag
16 RF Resume flag
17 VM Virtual 8086 mode flag
18 AC Alignment check flag (486+)
19 VIF Virutal interrupt flag
20 VIP Virtual interrupt pending flag
21 ID ID flag
some other registers
There are other registers such as control registers, debug registers, test registers, and protected mode segment registers.
The control registers are CR0 to CR4; the
debug registers are DR0 to DR7; the
test registers are TR3 to TR7;
the segment registers in protected mode are GDTR, IDTR, LDTR, and TR.
Reference: http://www.eecg.toronto.edu/~amza/www.mindsec.com/files/x86regs.html