2 Data Organisation in Memory

2.1 Memory Hierarchy

  1. Registers Very fast access, but limited numbers within the CPU (2-128 registers). Operates at CPU clock rate.

  2. Cache memory Fast access static RAM close to CPU. Typical access time 3-20ns, size up to 512kB.

  3. Main memory Dynamic RAM or ROM. Typical access time 30-70nS, size up to 16GB.

  4. Secondary memory Not always random access but non-volatile. Maybe be based on magnetic or flash technology. Typical access time 0.03-100mS, size: up to 4TB.

2.1.1 Main Memory

Main memory

2.2 Number representation

Type Bytes Bits Range
signed char 1 8 −128→+127-128 \to +127
unsigned char 1 8 0→+2550 \to +255
short int 2 16 −32,768→+32,767(±32KB)-32,768 \to +32,767 \quad (\pm 32\text{KB})
unsigned short int 2 16 0→+65,535(64KB)0 \to +65,535 \quad\qquad (64\text{KB})
unsigned int 4 32 0→+4,294,967,295(4GB)0 \to +4,294,967,295 \quad (4\text{GB})
int 4 32 −2,147,483,648→+2,147,483,647(±2GB)-2,147,483,648 \to +2,147,483,647 \ (\pm 2\text{GB})
long int 4 32 −2,147,483,648→+2,147,483,647(±2GB)-2,147,483,648 \to +2,147,483,647 \ (\pm 2\text{GB})
long long int 8 64 −(263)→(263)−1-(2^{63}) \to (2^{63})-1
float 4 32
double 8 64
long double 12 96

Note:

2.2.1 Two ways of storing a multi-byte number

Little vs Big Endian format

2.3 Character Representation

A char requires one byte (8 bits) of memory. The binary data is transformed to some representative characters through some encoding standard.
Some encoding standards:

7-bit ASCII table

Note that lower and upper case characters and digits are contiguous.

Boolean variables have 2 states:

  1. False = 0

  2. True = non-zero

Most implementations use one byte (8 bits) to store a 1-bit Boolean value (inefficient). Some processors like the 8051 support a small portion of bit-addressable memory.

8051 processor supports bit-addressable memory.

2.4 Array, String and Structure Representations

2.4.1 Arrays

A linear array is a consecutive area in memory storing a homogenous data type.
Elements are accessed through some offset from the base address.

Offset calculated from number of elements and size of the data type.
Nested arrays

2.4.2 Strings

Strings are initialised as array of characters.
C string terminates with null character, Pascal string stores the length of the string at the start of the string.

2.4.3 Structures

Each data type is ordered according to declaration, and allocated their own data size based on their type.

2.5 Pointer representation

Initialising pointer
Assuming addresses are 16 bits.
Size of pointer is fixed regardless of data type.

Use *ptr to dereference a pointer.

2.6 Data Alignment

Multi-byte data should be aligned to addresses that are multiples of their data type size.
Data Type Size (Byte) Example of allowable start addresses due to alignment
char 1 0x..0000, 0x..0001, 0x..0002
short 2 0x..0000, 0x..0002, 0x..0004
int 4 0x..0000, 0x..0004, 0x..0008
float 4 0x..0000, 0x..0004, 0x..0008
double 8 0x..0000, 0x..0008, 0x..0010
pointer 8 0x..0000, 0x..0008, 0x..0010

Assuming a 64-bit processor that uses 64-bits to represent an address.

Intel Pentium 4
8-byte data should be aligned to addresses that are multiples of 8.

2.6.1 Data alignment in structures

Defining struct in C
Padded bytes are added to maintain alignment for each data type.
Rearranging order of data objects
New struct takes 8 bytes instead of 12 previously.