Program Memory



The memory used by a running program is typically divided into three sections. The code itself lives in one section while the data lives in two others, the call stack and the heap.

The code section is almost always treated as read-only because “self-modifying” code is generally frowned upon.

The call stack is where we store the local variables of the functions we invoke, while the heap is where we store any other data.

The call stack is so called because it is organized into a stack, a LIFO (last-in, first-out) data structure in which we only remove the last thing added. For each function call, we add to the stack a “frame”, which consists of the local variables for that call and the return address (the address to jump back to when the function returns). The parameter variables are given their initial values from the arguments to the function.

Big-endian vs. Little-endian: Whether a system is big-endian or little-endian determines how bytes get copied between memory and registers. In a big-endian system, the most significant byte is copied first, e.g. the instruction which copies address A into register R copies the byte at A into the most-significant byte of R, A+1 into the next byte of R, A+2 into the next byte of R, and so on; in a little-endian system, the least significant byte is copied first. The choice of endianness is generally regarded as arbitrary.