简体   繁体   中英

How to know if a register is a “general purpose register”?

I am trying to understand what criteria a register must have to be called a "general purpose register".

I believe a general purpose register is a register that can be used for anything (for calculation, for moving of data to/from it, etc.) and is a register that doesn't have a special purpose.

Now I have read that the ESP register is a general purpose register. I guess the ESP register can be used for anything, but the ESP register also have a special purpose, which is to point to the top of the stack.

So does that mean that ESP register is a special purpose register?

The term General purpose register (GPR) stands in contrast to Special purpose Register . The latter cannot be used in all contexts.

Historically the old 8086 architecture introduced this difference for integer registers present in their names till today:

  • AX = A ccumulator register : accumulates the result(**)
  • BX = B ase register : base offset for certain instruction, eg XLAT
  • CX = C ounter register : counts for loops, eg JCXZ .
  • DX = D ata register : extends the data range, eg the result of MUL is in DX:AX
  • SI = S ource index : source for string instructions, eg LODSB
  • DI = D estination index : destination for string instructions, eg STOSB
  • SP = S tack pointer : points to the current item of the stack
  • BP = B ase pointer : points to the base of the current subroutine (stack frame)

(**) AX/AL is some kind of special purpose register, too. Many instructions have special encodings for AX/AL as operands, eg loading segment registers with MOV .

Other Special Purpose Registers were

  • Segment registers (CS,DS,ES,SS)
  • Flags register (FLAGS) and
  • Instruction Pointer (IP)

Some of these restrictions are used till today in the addressing mode for 16-bit instructions in real-mode (See Intel® 64 and IA-32 Architectures Software Developer's Manual Volume 2, Section 2.1.5, Table 2-1. "16-Bit Addressing Forms with the ModR/M Byte")


With the introduction of the 32-bit architecture - IA-32 - the purpose of the integer registers generalized and (nearly) each register can be used for every purpose (hence general purpose). This also reflects in the addressing mode encoding of the instructions, see Intel Manual Volume 2, Section 2.1.5, Table 2.2 . (Compare Table 2.1 with Table 2.2 to get an idea of the difference)

The names got prefixed with an E and an R to EAX and RAX , respectively, and their historic names indicating the usage are now merely conventional.

With many new architectures new special purpose registers were added. A complete overview is given in the Intel Manual, Volume 1, Section 3.7.2. :

  • 32-bit general-purpose registers (EAX, EBX, ECX, EDX, ESI, EDI, ESP, or EBP)
  • 16-bit general-purpose registers (AX, BX, CX, DX, SI, DI, SP, or BP)
  • 8-bit general-purpose registers (AH, BH, CH, DH, AL, BL, CL, or DL)
  • segment registers (CS, DS, SS, ES, FS, and GS)
  • EFLAGS register
  • x87 FPU registers (ST0 through ST7, status word, control word, tag word, data operand pointer, and instruction pointer)
  • MMX registers (MM0 through MM7)
  • XMM registers (XMM0 through XMM7) and the MXCSR register
  • control registers (CR0, CR2, CR3, and CR4) and system table pointer registers (GDTR, LDTR, IDTR, and task register)
  • debug registers (DR0, DR1, DR2, DR3, DR6, and DR7)
  • MSR registers

A general purpose register is one that can be used for more than one purpose. These purposes are

  • value
  • addressing
  • indexing
  • (counting)
  • (base)

A segment register , for example, can only hold a segment value but cannot be used in an addition. And a FPU register can only hold a floating points value but cannot be used for addressing.

In IA-32 the ESP register is closer to being a general purpose register because it can be used for (nearly) all of the above purposes:

  • as value: mov eax, esp
  • in addressing: mov eax, [esp+4] , but not as (scaled) index like mov eax, [4+esp*2]
  • as base: mov eax, [esp + eax]
  • as count: inc esp before a jump is valid

The only exception for ESP is that the (scaled) index addressing cannot be encoded. It can only be used as a base register which is exceptionally encoded with a SIB-byte (see Intel Manual, Volume 2, Section 2.1.5, Table 2.3 - see footer).

To illustrate the difference in encoding between ESP and the other registers (eg ECX ):

8b 01         mov eax, [ecx]   ; MOV + ModRM (normal)
8b 04 24      mov eax, [esp]   ; MOV + ModRM + SIB byte
8b 41 04      mov eax, [ecx+4] ; MOV + ModRM + disp8
8b 44 24 04   mov eax, [esp+4] ; MOV + ModRM + SIB + disp8

I guess despite this one exception ESP can still count itself a GPR .

Every general-purpose register in x86 is also used implicitly by certain instructions, and is therefore also a special purpose register. Here's just one example for each register, but there are many more examples.
eax: mul
ebx: xlat
ecx: shl
edx: div
edi: stos
esi: lods
ebp: leave
esp: ret

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM