Vinícius Vidal

Full-Stack Developer

Hardware Basics - Memory

Memory is where the computer stores its programs and data. Without it, processors would not be able to read or write information, and computers as we know them simply would not exist.

image.png

What are bits

The smallest unit of information is the bit, which can take on two values: 0 or 1.

You can imagine this as a light that can be off (0) or on (1).

Computers use this two-value system because it is easier and safer for electronic devices. If they had to use many different values, it would be harder to ensure that everything worked correctly, without errors.

When we combine several bits, we can represent more complex things. For example:

  • With 4 bits, it is possible to form 16 different combinations.
  • With 8 bits (called 1 byte), it is possible to form 256 combinations.

In this way, computers are able to store texts, images, music, and many other types of information using only combinations of 0s and 1s.

This system is known as binary, precisely because it uses only two values. It constitutes the fundamental basis for the operation of all computers.

Memory address

Memories are made up of several cells, which are small spaces capable of storing pieces of information. Each cell has a unique number identifier, called an address, which allows the processor to locate and directly access the content stored in that position.

image.png

Memory addresses are represented by binary numbers, with a fixed number of bits. If an address has n bits, it can reference up to 2^n different positions in memory. For example, a system with 16-bit addresses can access up to 65,536 cells.

The number of bits used for the address is independent of the number of bits that each cell stores. Thus, a memory with 2^12 cells of 8 bits each, or with 64 bits per cell, will always need 12 bits for addressing. What changes is the total amount of information that can be stored, but not the way each cell is identified.

The cell is the smallest addressable unit of memory. In the past, different architectures used cells with varied sizes, from 1 bit up to 60 bits. However, today there is a standard: the cell has 8 bits and is called a byte or octet.

Bytes can be grouped into larger units called words. The size of a word is defined by the processor's architecture. For example, in 32-bit systems, a word corresponds to 4 bytes, while in 64-bit systems, the word has 8 bytes.

The word size determines the amount of data the processor is capable of handling in a single operation, directly influencing the size of the registers, the width of the data bus, and the design of the instruction set architecture (Instruction Set Architecture).

Hexadecimal notation for memory addresses

Although memory addresses are internally represented in binary format, in practice it is common to use hexadecimal notation to represent them in a more compact and readable way.

Each hexadecimal digit represents exactly 4 bits (a nibble). Therefore, it is much easier to read, write, and interpret long addresses in hexadecimal than in pure binary.

For example, a 16-bit address can be represented as a binary number of 16 digits (bits), which can be complex to read, like:

0001 1010 1110 0101

The same address in hexadecimal is:

1AE5

It is clear that the hexadecimal representation is much shorter and clearer, making it easier to visualize the address.

Moreover, in many programming languages and technical documentation, the 0x prefix is used to indicate that the number is in hexadecimal notation. For example, the same address would be written as:

0x1AE5

This prefix does not change the value; it only serves to signal that the number is represented in base 16, avoiding confusion with other numerical bases.

image.png

How hexadecimal notation works

Hexadecimal notation is a base-16 numbering system that uses 16 symbols to represent values: the digits from 0 to 9, followed by the letters A to F. Each symbol represents a numerical value between 0 and 15.

For example, the digit A represents the decimal value 10, B represents 11, and so on up to F, which represents 15.

Each hexadecimal digit corresponds exactly to 4 bits in the binary system, since 2⁴ = 16. This means that a single hexadecimal digit can represent any 4-bit binary value.

Thus, hexadecimal notation is a compact and direct way to represent long binary numbers. For example, the binary value 1111 1010 can be divided into two groups of 4 bits: 1111 and 1010.

To understand why 1111 equals 15 in decimal, consider the powers of 2 associated with each bit (from right to left):

  • The first bit is worth 2⁰ = 1
  • The second bit is worth 2¹ = 2
  • The third bit is worth 2² = 4
  • The fourth bit is worth 2³ = 8

Since all bits are 1, we sum: 8 + 4 + 2 + 1 = 15.

Now, for the group 1010, the calculation is:

  • The first bit is 0, so 0 × 1 = 0
  • The second bit is 1, so 1 × 2 = 2
  • The third bit is 0, so 0 × 4 = 0
  • The fourth bit is 1, so 1 × 8 = 8

Summing the values: 8 + 0 + 2 + 0 = 10.

The value 10 in decimal is represented by the letter A in the hexadecimal system.

Thus, the binary number 1111 1010 can be represented as FA in hexadecimal.

Here is an image showing some values for demonstration: image.png

Byte Order

When data occupies more than one byte in memory, as is the case with 32 or 64-bit integers, an important question arises: in what order should the bytes be stored?

This order is called byte order or endianness and defines how bytes are organized in memory to represent multibyte values.

There are two main conventions:

  • Big-endian: the most significant byte (MSB) is stored first, that is, at the lowest memory address.

    Example: the hexadecimal number 0x12345678 would be stored as: 12 34 56 78

  • Little-endian: the least significant byte (LSB) is stored first, that is, at the lowest memory address.

    Example: the same number 0x12345678 would be stored as: 78 56 34 12

The choice between big-endian and little-endian depends on the processor architecture. For example:

  • x86 processors (Intel, AMD) use little-endian.
  • ARM architectures can operate in big-endian, little-endian, or even support both modes.
  • Computer networks typically follow the big-endian convention, also known as network byte order.

This difference is important when interpreting data stored or transmitted between systems with different architectures. If the byte order is not considered, the value read may be completely incorrect.

Data Alignment

Data alignment is the way data is organized in memory so that the processor can access it efficiently.

Data must be stored at addresses that are multiples of their size. For example, a 4-byte (32-bit) data must be at an address that is a multiple of 4.

When a 4-byte data is stored at address 0x1000, it is aligned. If it is at address 0x1003, it is unaligned.

The processor may need to perform more operations to access unaligned data, which reduces efficiency.

To ensure alignment, the compiler can insert extra spaces called padding between data in structures, causing them to occupy more memory than the simple sum of their fields.

Consider the following structure in C:

struct Example {
  char a;      // 1 byte
  int b;       // 4 bytes
};

Without padding, the char occupies 1 byte and the int starts at the next address, but this could be an unaligned address for the int (which needs to be at multiples of 4).

So, the compiler inserts 3 bytes of padding after the char, so that the int starts at an address multiple of 4, ensuring alignment.

Thus, the structure occupies 8 bytes (1 + 3 padding + 4) instead of 5 bytes, optimizing processor access.

Volatile and Non-Volatile Memory

Memories can be classified according to their ability to retain or not the stored data in the absence of electrical power.

Volatile memory: loses its content when the computer is turned off. A classic example is RAM (Random Access Memory). This type of memory is used to temporarily store data and programs while they are being used, as it offers high access speed.

Non-volatile memory: retains information even without power. Examples include hard drives (HDs), SSDs, and flash memory. These memories are used to store data permanently or for long periods, such as the operating system, documents, and applications.

Each type of memory has specific characteristics in terms of speed, capacity, and cost, being used according to the system’s needs.

That’s it for today!

See you next time. 👋