eelCloud

Working Mechanism of a CPU chip in x86 architecture Part 2 : Registers And It's Function

This blog is the second part. From this blog you get knowledge about the registers and it's functions in CPU.


Feb-17-2025, Monday

Author: Deepsu Gautam

register is a small and high speed storage inside the CPU. Register stores data temporarily while the CPU processes instructions.

Names and Purpose of Registers


R0 (EAX/RAX) : This register is known as Extended Accumulator Register. It stores the result of airthmetic operations. They are used in muliplication and divison operations.


R1 (EBX/RBX) : This register is known as Extended Base Register. It holds memory address. It is used for memory based operations like accessing array and structures.


R2 (ECX/RCX) : This register is known as Extended Counter Register. It is used as a loop counter in itertive(repeating or looping) operations. It often decreases until it reaches 0 in loop.


R3 (EDX/RDX) :  This register is known as Extended Data Register. It is used in multiplication and divison to store higher parts of
 results (extra data). It is also used in I/O operations to hold data for input/output parts.


R4 (ESI/RSI) : This register is known as Extended Source Index Register. It sotres the memory address of source data during string operations. Eg: copy data from memory to another location.


R5 (EDI/RDI) : This register is known as Extended Destination Index Register. It holds memory address where data is to be stored.


R6 (EBP/RBP) : This register is known as Extended Base Pointer. It stores the base address of the stack frame during function calls, allowing local variables and function arguments to be accessed.


R7 (ESP/RSP) : This register is known as Extended Stack Pointer. It points to the top of the stack in the memory. Hence this register stores the address of the latest value pushed in stack.


R8 to R15 : These Registers are known as Extended General Registers. It is used for additional computation and optimizations. They are additional registers providing additional storage to handle complex processes in modern CPUs.


R16 (EFLAGS/RFLAGS) : This Register is known as Flag Register or Status Register. It stores CPU status flag like zero flag, carry flag, overflow flag, and negative flag.


Lets solve a problem in CPU to understand Registers better. We will use basic Syntax of Assembly language to better understand it. We will perform calculation (50/7) step by step.

(Note : When coding in registers, this is how the CPU must be instructed)


STEP 1 : Make R0 (EAX) hold the number that is to be divided in our case 50.

  MOV EAX, 50   

Here, MOV refers to Move so we just moved or assigned 50 to R0 (EAX).


STEP 2 : Clear storage of R3 (EDX). We can clear it by assigning it 0. Later on this EDX will hold remainder which is extra data from divison.

  MOV EDX, 0   


STEP 3 : Assign divisor to the R1 (EBX). Yes it can store address and values.

  MOV EBX, 7   


STEP 4 : Now is time for main calculation. Now since EBX is used for memory base calculations and operations, this is how we command.

  DIV EBX   


Now lets understand what happens in CPU at step 4.


So now the CPU reads EAX because EAX is where value is stored and will be updated with new value. Then the CPU reads EDX because it is where the extra data is going to be stored. Now EBX is where divison happens. Therefore The CPU will now Divide EAX value on EBX with value stored in EBX. Then as discussed earlier CPU will store new primary value that is quotient back to EAX. Then there is extra data remainder which will be stored in EDX.


Registers Value at End
R0 (EAX) 7 (Quotient)
R3 (EDX) 1 (Remainder)
R1 (EBX) 7 (Unchanged value)


This is how CPU will process it step wise.