将地址存储在 MIPS 的寄存器中 [英] Storing addresses in a register for MIPS
问题描述
我已经分配了一定数量的内存,并希望将此内存的位置分配给我在程序的 .data 部分中声明的变量.我知道如何将内存位置分配给变量,但是一旦我这样做了,我如何使用该变量来访问分配的内存?
I have allocated a certain amount of memory and would like to assign the location of this memory to variable I have declared in the .data section of the program. I know how to assign the memory location to the variable, but once I do that how can I use that variable to access the allocated memory?
推荐答案
如果我正确理解你的问题,你会想要使用 la
(加载地址)指令将地址放入寄存器.然后,您将使用 lw
(加载字)和 sw
(存储字)指令来操作数据.例如,考虑以下代码
If I understand your problem correctly, you will want to use the la
(load address) instruction to get the address into a register. You will then use the lw
(load word) and sw
(store word) instructions to manipulate the data. For instance, consider the following piece of code
.data
tmpval: .word 5
__start:
sub $sp, $sp, 12
sw $ra, 0($sp) # Return addy
sw $t0, 4($sp)
sw $t1, 8($sp)
la $t0, tmpval
lw $t1, 0($t0) # $t1 == tmpval == 5
li $t1, $2 # $t1 == 2
sw $t1, 0($t0) # tmpval == 2
lw $ra, 0($sp)
lw $t0, 4($sp)
lw $t1, 8($sp)
add $sp, $sp, 12
jr $ra
因此,在内部代码块中,您可以看到您将 $t0
(或任何其他与此相关的寄存器)视为内存位置并对其进行适当处理.
So in the inner-block of code you can see that you treat $t0
(or any other register for that matter) as a memory location and work with it appropriately.
这篇关于将地址存储在 MIPS 的寄存器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!