我是否需要从内存中加载数组的基地址到此 MIPS 代码中的临时寄存器?
Do I need to load from memory a base address of an array to a temporary register in this MIPS code?
我正在做家庭作业,我必须翻译一些 C 代码
进入 MIPS(32 位)。
我可以将 3 个寄存器 $s0
用于一个变量 x
,'$s1' 用于一个变量 y
和 '$s3' 为数组 'A[]'.
的基址
我的目标是能够执行一些条件 if
逻辑,将数组 'A' 的某个索引与变量 'y' 进行比较(在本例中为 A[x] < y
)
到目前为止我的代码:
sll $t0, $s0,2 # t0 = x *4
add $t0,$t0,$s3 # t0 = x*4 + &A[0] = A[x]
我的问题是:寄存器 $t0 是否已经指向 'A[x]' 的内存地址,因此我可以继续使用目前的内容并开始比较值:
slt $t1,$t0,$s1 # if A[x] < y then $t1 = 1
或者我是否需要在代码开头使用 'lw' 伪指令从内存中加载单词:
lw $t0, 0($s3) #temp reg $t0 gets A[0]
sll $t1, $s0,2 # t0 = x *4
add $t0,$t1,$t0 # t0 = x*4 + &A[0] = A[x]
rest of code goes here
您的 $t0 将包含存储 A[x] 的内存位置。
"does register $t0 already points to memory address of 'A[x]' hence I can move forward with what I have so far and start comparing values:"
是的,$t0 指向 'A[x]' 的内存地址是正确的。但
您还不能比较,因为您有存储 A[x] 的地址,而不是该地址的值。因此,您需要执行加载字指令。
"I need to load the word from memory using the 'lw' pseudoinstruction at the beginning of my code:"
您是正确的,您需要从内存中加载单词。请记住,$t0 包含您想要从内存中加载的感兴趣的地址。 ($t0 = A[x] 的地址)
你可以这样做:
lw $t0, 0($t0) # temp reg. $t0 gets A[x]
那你就可以做对比了
注意:回想一下,寄存器与内存是分开的。为了比较寄存器中的值和内存中的值,您需要使用 lw
和适当的地址将内存中的值带入特定寄存器。
我正在做家庭作业,我必须翻译一些 C 代码 进入 MIPS(32 位)。
我可以将 3 个寄存器 $s0
用于一个变量 x
,'$s1' 用于一个变量 y
和 '$s3' 为数组 'A[]'.
我的目标是能够执行一些条件 if
逻辑,将数组 'A' 的某个索引与变量 'y' 进行比较(在本例中为 A[x] < y
)
到目前为止我的代码:
sll $t0, $s0,2 # t0 = x *4
add $t0,$t0,$s3 # t0 = x*4 + &A[0] = A[x]
我的问题是:寄存器 $t0 是否已经指向 'A[x]' 的内存地址,因此我可以继续使用目前的内容并开始比较值:
slt $t1,$t0,$s1 # if A[x] < y then $t1 = 1
或者我是否需要在代码开头使用 'lw' 伪指令从内存中加载单词:
lw $t0, 0($s3) #temp reg $t0 gets A[0]
sll $t1, $s0,2 # t0 = x *4
add $t0,$t1,$t0 # t0 = x*4 + &A[0] = A[x]
rest of code goes here
您的 $t0 将包含存储 A[x] 的内存位置。
"does register $t0 already points to memory address of 'A[x]' hence I can move forward with what I have so far and start comparing values:"
是的,$t0 指向 'A[x]' 的内存地址是正确的。但 您还不能比较,因为您有存储 A[x] 的地址,而不是该地址的值。因此,您需要执行加载字指令。
"I need to load the word from memory using the 'lw' pseudoinstruction at the beginning of my code:" 您是正确的,您需要从内存中加载单词。请记住,$t0 包含您想要从内存中加载的感兴趣的地址。 ($t0 = A[x] 的地址)
你可以这样做:
lw $t0, 0($t0) # temp reg. $t0 gets A[x]
那你就可以做对比了
注意:回想一下,寄存器与内存是分开的。为了比较寄存器中的值和内存中的值,您需要使用 lw
和适当的地址将内存中的值带入特定寄存器。