Memory Addressing
Course: Computer Architectures
Type: Concept
Definition
A label such as Val_A names a memory location.
The address of a variable and the value stored at that address are different things.
Example:
Val_A: .word 10Conceptually:
Val_A -> address, e.g. 0x1000
memory[0x1000] -> 10la
la x1, Val_Aloads the address of Val_A into x1.
After this:
x1 = address of Val_Alw
lw x1, 0(x1)means:
x1 = memory[x1 + 0]So if x1 contained the address of Val_A, it now contains the actual value stored there.
sw
sw x3, 0(x4)means:
memory[x4 + 0] = x3Why 0(x1)?
Loads and stores use:
offset(base_register)Examples:
lw x5, 0(x1)
lw x6, 4(x1)
lw x7, 8(x1)If x1 points to the first element of an array of 4-byte words, these access consecutive elements.
Arrays
For:
V1: .word 1, 2, 3, 4addresses might look like:
V1[0] -> 1000
V1[1] -> 1004
V1[2] -> 1008
V1[3] -> 1012So advancing a word pointer uses:
addi x1, x1, 4Common mistake
This is not a normal register-register add:
add x1, Val_Abecause Val_A is a label, not a register.