Load and Store


How Do Variables Get from Memory to the Registers?

Example: Assume that obj is a variable of type struct with several data members and that $t0 holds the address of the beginning of obj. How do we load obj.a into $s0, obj.b into $s1, and obj.e into $s2?

CLoad data members into registers
struct s_tag {
    int a;
    int b;
    int c;
    int d;
    int e;
} obj;

lw $s0, 0($t0)   # Load obj.a into $s0
lw $s1, 4($t0)   # Load obj.b into $s1
lw $s2, 16($t0)  # Load obj.e into $s2
Addresses Memory: Addressable Bytes
1012 (obj.a) 00000000000000000000000000000000 ==> $s0
1016 (obj.b) 00000000000000010000001100000111 ==> $s1
1020 (obj.c) --------------------------------
1024 (obj.d) --------------------------------
1028 (obj.e) 00110010000010100110000101010101 ==> $s2

From Registers Back to Memory

What about loading and storing elements from arrays?

Addresses Memory: Addressable Bytes
2012 (A[0]) --------------------------------
2016 (A[1]) --------------------------------
2020 (A[2]) 00110010000010100110000101010101 ==> $t1
2024 (A[3]) --------------------------------
2028 (A[4]) --------------------------------

Handy Trick ...

What's an efficient way to multiply binary numbers by 2? by 4?

Alyce Brady, Kalamazoo College