Operators | Description |
---|---|
lb/sb | Load/Store byte |
lh/sh | Load/Store half-word |
lw/sw | Load/Store word |
ld/sd | Load/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)
lw $t5, a
sw $t6, a
...
.data
a: .word 4
Register directly contains the value to use
li $t5, 5
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
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
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