When we save a file, it is saved to the computer memory.
When we perform an arithmetic operation, its operands and outputs are stored in the memory
When we press a key, a flag in the memory is changed.
When we launch a program, that program is loaded into memory!
Multi-byte values (i.e. values greater than 255) can be stored to the memory in two different ways.
287
is stored as \x01\x1f
287
is stored as \x1f\x01
Most systems use the Little Endian address order, due to its inherent properties
int
(4 bytes) at 0x11223344
is even, only that address needs to be checked0x11223347
Most systems use the Little Endian address order, due to its inherent properties
int
(4 bytes) to a char
(1 byte) is trivial, as the LSB is already at the right addressThe only common modern-day use for Big Endian is for storing strings
Random Access Memory is a form of computer memory that allows data to be quickly written and read. They are (relatively) cheap, and complement the CPU registers by holding large amounts of data that is frequently accessed.
It is called Primary Storage - as it is connected directly to the CPU. Secondary storage devices (i.e. hard disks) are connected indirectly.
RAM is considered volatile / non-persistent memory - data is only kept when the RAM is powered on.
Registers | Primary Storage (RAM) | Secondary Storage (SSD/HDD) | |
---|---|---|---|
Location | CPU | On-board | External |
Speed | Fast | Slow | Slower |
Price | - | $$ | $ |
Capacity | bytes | gigabytes | a lot |
Registers are fast but they cannot store a lot of data!
Both registers and RAM modules store data, however due to their different locations - they are accessed in different ways.
Some CPU architectures allow “Memory Mapped IO” operation, which can map various I/O devices (including registers!) to virtual locations in the memory.
Therefore, the access methods to register data and RAM data can be unified, making interoperability better.
Like memory mapped I/O, output ports of devices in a system can also be mapped to virtual addresses in the memory
As mentioned earlier, when a program is executed, its instructions are copied into the memory.
By standard, the instructions are loaded near the start (lower portion) of the available memory space.
The stack contains the temporary memory and data used by the functions of a program, and is located near the top of the memory address space.
It is called a stack due to how it is accessed.
The stack frame contains the parameters and temporary variables that a function will use to complete its execution.
The stack frame also contains data related to what the CPU should do after the function finishes, such as values of the CPU registers prior to the function being called.
This includes the “return address” that the CPU will go to after the function is finished.
The stack pointer register contains the address of the end of the stack.
Depending on the CPU architecture, this address may be the address to the last stored value, or the address after the last value.
When memory is allocated/released in the stack (either a variable, or an entire stack frame), the address in the stack pointer changes.
The heap contains long-life data that persists during the lifetime of the program (rather than the lifetime of an instance of a specific function).
This memory space can be considered as the ‘global scope’ region, where all functions can access the data in the heap
The heap is located after the text region of the memory (where the program instructions are loaded) - i.e. near the start of the memory address space.