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 10

Conceptually:

Val_A           -> address, e.g. 0x1000
memory[0x1000]  -> 10

la

la x1, Val_A

loads the address of Val_A into x1.

After this:

x1 = address of Val_A

lw

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] = x3

Why 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, 4

addresses might look like:

V1[0] -> 1000
V1[1] -> 1004
V1[2] -> 1008
V1[3] -> 1012

So advancing a word pointer uses:

addi x1, x1, 4

Common mistake

This is not a normal register-register add:

add x1, Val_A

because Val_A is a label, not a register.