MIPS Programming Address Storing and Loading

2019-08-01 17:58发布

问题:

I'm having trouble understanding how to load and store specific addresses within MIPS. I need to take the address of an item and store it within a word in another item. How do I load the address of the item as a word so it can be stored as a word? And how would I go about loading the word and turning it into an address? Is this possible? I need to be able to link items like a doubly linked list where each item points to the next.

回答1:

Sure, use la to get the address of a label, and lw/sw to load/store that address from/to somewhere.

An example:

.data
list: .space 8*4

item1: .word 123

.text
.globl main
main:

la $a1,list     # a1 = address of list

la $t0,item1    # t0 = address of item1
sw $t0,($a1)    # store item1's address in the first word of list

# ...

lw $t0,($a1)    # t0 = address of item1
lw $a0,($t0)    # a0 = item1
li $v0,1        # syscall 1 (print_int)
syscall

li $v0, 10      # syscall 10 (exit)
syscall


标签: mips