Post

Chapter 2 > Random Access Memory

In this chapter, we will discuss Random Access Memory (RAM).

RAM, a device that provides temporary storage for your OS, is essential for computer functionality. When your computer boots up, the operating system is stored in RAM, and subsequently, every application you execute is also placed in RAM.

Why wouldn’t the computer place the running OS and applications on a drive? Because that would be too slow. RAM devices offer incredibly fast storage.

RAM consists of addresses, which in turn consist of segments:

Stack:

The stack is an area in memory used to temporarily store data. When programs get interrupted by other programs, their data is also written to the stack. When a function in a program is called, its variables and parameters are placed on the stack to prevent data loss. The stack operates as a Last-In-First-Out (LIFO) data structure, meaning the latest item added to the stack will be the first one removed.

Now, let’s introduce our first Assembly commands.

There are only two commands for working with the stack: push and pop.

1
push ax

push moves data to the stack. We need to specify an argument that tells the assembly where to take the data. In our case, it’s the ax register.

1
pop bx

pop removes data from the stack. The specified argument tells the assembly where to put the data. In our case, it’s the bx register. It pops the most recent element we have included on the stack.

Imagine our initial stack:

4
3
2

Now, if we push a value into the stack (let’s say 5):

4
3
2
5

Using pop, 5 will be taken from the stack and placed in the specified location:

4
3
2

Stack memory is limited.

If the stack fills up, its values will be written to the heap. This is called a Stack Overflow.

Heap:

You have probably opened the process monitor in Windows, macOS, or GNU/Linux. You can imagine the heap as all the processes running in the background.

Data:

Data contains initialized variables. When you declare a variable and give it a value, it is placed in the Data segment.

BSS (Block Started by Symbol):

Unlike Data, BSS contains uninitialized variables. These variables are initialized as null values before the program starts. Declaring variables without values will place them in the BSS.

Text:

Text contains the compiled version of your code (binary code). It’s a read-only area, and the CPU uses this area to execute instructions for your program.

Now, let’s revisit memory addresses.

Here is an example of some memory addresses:

AddressValue
0x00010000 0000 0010 0110 1100
0x00020100 0010 1010 0111 1100
0x00030010 1100 1111 0011 0000

As you can see, addresses contain binary values. However, when we place data at the address, we need to specify the offset. The offset is the specific place in the address where we need to put the desired value.

This post is licensed under CC BY 4.0 by the author.

Trending Tags