# x86 (32-bit)

{% embed url="https://textbook.cs161.org/memory-safety/x86.html#24-little-endian-words" %}

### 1. Little Endian

### 2. Stack grows downwards to lower memory addresses

### 3. Registers

#### Special registers

a. `EIP`: Extended Instruction Pointer

* Stores the address of the current machine instruction

b. `EBP`: Extended Base Pointer

* Stores the address of the top of the current stack frame
* Used to create a stable reference point&#x20;
  * used as an offset to reference local variables

c. `ESP`: Extended Stack Pointer

* Stores the address of the bottom of the current stack frame

#### General-purpose registers

`EAX`, `EBX`, `ECX`, `EDX`, `ESI` and `EDI`

> Note: the `e` prefix for the register naming stands for "extended". This indicates a 32-bit system (extended from the original 16-bit).

### Write/read operations on the stack

Since we are working with a 32-bit architecture, the increment/decrement value will be 4 bytes (32 bits). Additionally, remember that the x86 architecture grows the stack downwards to lower memory addresses.

**Pushing to stack (WRITE)**

When data is pushed to the stack (`push` command), the `ESP` is decremented by 4, before the CPU appends the data to the memory location found in the `ESP` .

**Reading (`pop`) from stack (READ)**

To retrieve data from the stack, the memory location value stored in the `EIP` will be retrieved, before being incremented by 4.&#x20;

> Note that the data stored in the memory location of the old `EIP` value (before increment) will still be present. However, due to the change of the `EIP` value, the data will be treated as if it has been removed, and subsequent writes will overwrite this value

### Memory layout

<figure><img src="https://667177664-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FMsAGomcNx5xaB1xFygc9%2Fuploads%2FvrxmcwQxy7pU8vBad1dT%2Fimage.png?alt=media&#x26;token=51338a3d-919d-4dc6-9a5f-b570caa50f3c" alt=""><figcaption></figcaption></figure>
