[英]Access value at a specific memory address in MIPS
I am trying to write a program in mips that creates a word array of memory addresses.我正在尝试在 mips 中编写一个程序,该程序创建一个 memory 地址的字数组。
Array: .word 0x10010008,0x1001000C, 0x1001000D, 0x10010007, 0x1001000A数组:.word 0x10010008,0x1001000C, 0x1001000D, 0x10010007, 0x1001000A
I then need to iterate through the array and retrieve the value at each memory address, specified in the array.然后我需要遍历数组并检索数组中指定的每个 memory 地址的值。
I have a few questions:我有几个问题:
How do I get the array to initially store the memory address not at 0x10010000 but at let's say 0x10010080?如何让数组最初存储 memory 地址而不是 0x10010000 而是假设 0x10010080?
How do I then treat the memory address declared in the array as memory addresses and not values.So that the program can then go to 0x10010008 and get the value stored there?然后我如何将数组中声明的 memory 地址视为 memory 地址而不是值。这样程序就可以 go 将值存储在 0x10010 那里?
How do I get the array to initially store the memory address not at 0x10010000 but at let's say 0x10010080?
如何让数组最初存储 memory 地址而不是 0x10010000 而是假设 0x10010080?
Since you're talking about an initialized global data array, and in an area of memory commonly used for global data storage, you have several options.由于您正在谈论一个初始化的全局数据数组,并且在 memory 通常用于全局数据存储的区域中,您有多种选择。 By default,
.data
on MIPS starts at 0x10010000.默认情况下,MIPS 上的
.data
从 0x10010000 开始。 So, if you want your array at 0x10010080, you can ask for 0x80 bytes of padding as follows:因此,如果您希望您的数组位于 0x10010080,您可以要求 0x80 字节的填充,如下所示:
.data
.space 0x80
array:
.word ...
Next, some assemblers will allow a number placed after the .data
, as in接下来,一些汇编器将允许在
.data
之后放置一个数字,如
.data 0x10010080
array:
.word ...
How do I then treat the memory address declared in the array as memory addresses and not values.So that the program can then go to 0x10010008 and get the value stored there?
然后我如何将数组中声明的 memory 地址视为 memory 地址而不是值。这样程序就可以 go 将值存储在 0x10010 那里?
It doesn't make sense to attempt to access the address 0x10010008 to get values store there if you have moved them to 0x10010080.如果您已将它们移至 0x10010080,则尝试访问地址 0x10010008 以获取存储在那里的值是没有意义的。
Addresses are numeric constants, and labels are used to equate names to the numeric values of addresses.地址是数字常量,标签用于将名称等同于地址的数值。
If you want content at a memory location you have to dereference a pointer — form a pointer (eg using a label) or use a pointer passed as a parameter and use a lw
or sw
to dereference it.如果您想要 memory 位置的内容,则必须取消引用指针 — 形成指针(例如使用标签)或使用作为参数传递的指针并使用
lw
或sw
取消引用它。
To form a pointer from a label, use the la
pseudo instruction (it will make a 32-bit address to a label and) can put that in a register, which you can then use as a pointer to the base of the array and access elements by using that base + constant or do further addressing/indexing to access elements of the array.要从 label 形成一个指针,请使用
la
伪指令(它将为 label 创建一个 32 位地址,并且)可以将其放入寄存器中,然后您可以将其用作指向数组基址的指针并访问通过使用该基数 + 常量或进一步寻址/索引来访问数组的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.