paint-brush
Demystifying Registersby@stefansilverio

Demystifying Registers

by stefan silverioOctober 29th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Let’s go under the hood and learn about registers.

Company Mentioned

Mention Thumbnail
featured image - Demystifying Registers
stefan silverio HackerNoon profile picture

Let’s go under the hood and learn about registers.

Registers are internal memory storage locations the processor utilizes to more efficiently process data. If not for registers, the processor would be forced to interact with the control bus anytime it wanted to read or write data from memory.

Registers are similar to variables, except there are a fixed number of registers on any given architecture. Registers store data elements for processing without having to dive deep and access the memory. More specifically, each register is a special location in the CPU where a single piece of data is stored. Often, registers hold pointers which reference other locations in memory — and movement of values between registers is very common. There are ten different registers in the 32-bit IA-32 architecture. The registers are grouped into three categories.

  1. General registers
  2. Control registers-comparisons and mathematical calculations
  3. Segment registers-stores the starting addresses of different segments (code, data, and stack)

General registers can be further dissected into the following subgroups:

  • Data registers-arithmetic, logical, and other operations
  • Pointer registers-point to locations on the stack and in memory
  • Index registers-indexed addressing

The most common 32-bit registers are EAX, EBX, ECX, and EDX.

Registers make efficient use of space. In fact, the lower halves of 32-bit registers can be used as 16 or 8-bit registers.

For example, AX is the primary accumulator register. It is generally used in input/output and most arithmetic instructions. In a multiplication operation for example, one operand is stored in the EAX or AX, or AL register, according to the size or that specific operand.

One particularly important general register is the eax register. Eax has two common uses, to store the return value of a function and as a special register for certain calculations. Eax is what’s known as a volatile register; that is, a register that is not guaranteed to retain its value once the function returns. This means the processor may, after the function returns, use this register space to store a different piece of data. However, in the scope of a function, eax is set to the return value of a function before it returns. Here is a function set to return 3:

return 3; // Return the value 3

Here is the same code in assembly:


mov eax, 3 ; Set eax (the return value) to 3ret ; Return

Another important general register is the esp register. The esp register stores a pointer to the top of the stack. Note that the stack grows downwards in virtual memory towards the heap.

Hopefully your feeling more comfortable about registers. Put crudely, they are simply special places in the CPU reserved for a specific type of data. There values are being constantly changed as the function evaluates and eventually terminates.