Comparison Between Memory and Registers
Learn about the structure of memory and registers in a computer.
Memory and registers inside an idealized computer
Computer memory consists of a sequence of memory cells, where each cell has a unique address (location). Every cell contains a number. We refer to the contents of a cell as a number, though the contents may not be an actual number but rather a program’s instructions or data.
Since memory accesses are slower than arithmetic instructions, registers are part of the CPU and they speed up complex operations that require memory to store temporary results. We can also think about them as standalone memory cells. The name of a register is its address.
Memory and registers inside the ARM64-bit computer
The addresses for memory locations in the illustration below contain integer values, usually differing by 4 or 8, and we also show two registers called x0
and x1
. The first halves of these registers are called w0
and w1
.
Since memory cells contain numbers, we start with simple arithmetic and ask a processor to compute the sum of two numbers to see how memory and registers change their values.
In the illustrations above, we use decimal addresses to demonstrate the difference between an idealized computer and an ARM64-bit computer. Generally, we use hexadecimal addresses to represent memory addresses.
Arithmetic example: Memory layout and registers
For our example, we have two memory addresses (locations) that we call a
and b
. We can think about a
and b
as names of their respective addresses (locations). Now, we introduce a special notation, where (a)
means “contents at the memory address (location) a
.” If we use C / C++ language to write our example, we declare and define memory locations a
and b
as the following:
static int a, b;
By default, when we load a program, static memory locations are filled with zeroes, and we can depict our initial memory layout after loading the program, as shown below:
Arithmetic example: A computer program
We can think of a computer program as a sequence of instructions for the manipulation of contents of memory cells and registers. For example, in the addition operation, we add the contents of the memory cell to the contents of the memory cell In our pseudocode, we can write:
[14] <- [14] + [12]
Our first program in pseudocode is shown on the left of the table:
In the pseudocodes, we use the following conventional symbols:
<—
means moving (assigning) the new value to the contents of a memory location (address).//
is a comment sign and the rest of line is a comment.=
shows the current value at a memory location (address).
A code written in a high-level programming language is translated to a machine language using a compiler. However, the machine language can be readable if its digital codes are represented in some mnemonic system called assembly language:
ADD x1, x1, #1
The above syntax increments what is stored in the register memory cell, x1
, by one.