Memory

Learn how memory works in a computer system. Learn about memory addressing, memory layout, and how to read memory tables. We'll also explore the concept of byte offsets, endianess and how data is stored in memory.

The next component is Memory

Memory is a large storage area where data and instructions are stored. If you already have experience in programming, see it as a large array of bytes.

We say that memory can be addressed, meaning that each byte in memory has a unique address, and if we want to read or write to a specific byte, we need to know its address. The address can also be seen as an offset from the start of memory, which says "how many bytes from the beginning of memory do I need to go to."

You will usually see memory represented as a table, where each row is 16 bytes, and each column is 1 byte. If you want to read the value of the FF inside the table, you can see that it's located in row 00000020 and column B, which means that its address is:

00000020 + B = 0000002B

Offset0123456789ABCDEF
0000000000000000000000000000000000000000
0000001000000000000000000000000000000000
0000002000000000000000000000BBFFAA000000
0000003000000000000000000000000000000000

Endianess

When we read from memory, we can read more than one byte at a time — usually either 1, 2, 4 or 8 bytes. Usually, instructions allow you to specify the address of the first byte you want to read and the size of how many bytes you want to read.

But the issue is: Do you read them from left to right or right to left?

This is where the concept of endianness comes into play. Endianness is the order in which bytes are stored in memory. There are two main types of endianness:

  • Little Endian
  • Big Endian

As a quick reminder: Say we have number 0x12345678, the most significant byte (MSB) is 0x12 (the leftmost one), and the least significant byte (LSB) is 0x78 (the rightmost one). When we read a multi-byte value from memory, we need to know the endianness to understand how to interpret the bytes.

Little Endian

In little endian, the least significant byte (LSB) is stored at the lowest address, and the most significant byte (MSB) is stored at the highest address.

For example, if you have the value 0x12345678 at address 0x00000000 in memory, it would be represented as:

Offset0123
0000000078563412

Big Endian

In big endian, it's the opposite — the most significant byte (MSB) is stored at the lowest address, and the least significant byte (LSB) is stored at the highest address.

This is easier to understand, because it reads the same way we write numbers.

For example, if you have the value 0x12345678 at address 0x00000000 in memory, it would be represented as:

Offset0123
0000000012345678