Chapter 6

Memory Types

Words and Halfwords

Static Data

Loading/storing memory

OperatorsDescription
lb/sbLoad/Store byte
lh/shLoad/Store half-word
lw/swLoad/Store word
ld/sdLoad/Store double word

lw format

lw Rt, Immediate(Rs) # Rt = Memory[Rs + Immediate]
lw Rt, Rs, label # Rt = Memory[Label] (pseudo op; Rs = 0x10010000)

sw format

sw Rt, Immediate(Rs) # Memory[Rx + Immediate] = Rt
sw Rt, Rs, Label # Memory[label] = Rt (pseudo op; Rs = 0x10010000)

Addressing by Label (When label in .data segment)

lw $t5, a
sw $t6, a
...
.data
a: .word 4

Addressing by direct register

Register directly contains the value to use

li $t5, 5

Addressing by indirect register

Register contains the address of the value to use

lui $t0, 0x1001   # load the starting memory address into $t0
lw $t5, 0($t0)          # $t5 = $t0[0]
addi $t0, $t0, 4        # Increase $t0 by 4 bytes
lw $t6, 0($t0)          $ $t6 = $t0[0]
...
.data
    .word 5
    .word 2
    .word 3
    .word 0
y:  .word 0

Addressing by register offset

Register contains a starting address which then needs to be offset

lui $t0, 0x1001   # load the starting memory address into $t0
lw $t5, 0($t0)          # $t5 = $t0[0]
lw $t6, 4($t0)          $ $t6 = $t0[1], where $t0 would be a 32bit word array
lw $t7, 8($t0)          $ $t7 = $t0[2], where $t0 would be a 32bit word array
...
.data
    .word 5
    .word 2
    .word 3
    .word 0
y:  .word 0

Pointer static variables

In this program the constants_ptr label contains the address of the constants data


# Shows static memory access and a "pointer" variable (constants_ptr)

.text

main:

    # Access the constants memory
    lui $t0, 0x1001 # $t0 = 0x10010000 (MIPS starting static memory address)
    lw $t0, 0($t0)  # $t0 = $t0[0] = constants_ptr
    lw $t1, 0($t0)  # $t1 = $t0[0] = constats_ptr[0]
    lw $t2, 4($t0)  # $t1 = $t0[4] = constats_ptr[1]
    lw $t3, 8($t0)  # $t1 = $t0[8] = constats_ptr[2]
    
    # print the numbers
    li $v0, 1
    move $a0, $t1
    syscall
    move $a0, $t2
    syscall
    move $a0, $t3
    syscall
    
    # call the exit subprogram
    # jal = jump and link
    jal exit

.data
constants_ptr: .word constants # points to constants address down below
y:             .word 0
constants:
    .word 5
    .word 4
    .word 3
    .word 0
        
# Exit subprogram
.text
exit:
    li $v0, 10
    syscall