MIPS汇编中的C数组索引? [英] C array indexing in MIPS assembly?
问题描述
问题:
void swap (int v[], int k)
{
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}
我的问题是为什么要在 $ t1
中添加int v []
?(无论谁都没有评论,所以我假设 $ a0
是 v []
和 $ a1
是 k
).
My question is why does int v[]
get added $t1
? (whoever did this didn't even comment it so I'm assuming $a0
isv[]
and $a1
is k
).
答案:
swap: sll $t1, $a1, 2
add $t1, $a0, $t1
lw $t0, 0($t1)
lw $t2, 4($t1)
sw $t2, 0($t1)
sw $t0, 4($t1)
jr $ra
我知道这是用来交换变量的,但是它在做什么,为什么要在 v []
和 k
之间添加? v []
是否不是已声明变量的数组,如何将它与整数 k
相加?
I know this is used to swap variables but what is it doing here, why is it adding v[]
with k
? Isn't v[]
a array of declared variables, how can you add it with a integer k
?
推荐答案
谁做的甚至都没有评论,所以我假设$ a0是v []而$ a1是k
whoever did this didn't even comment it so I'm assuming $a0 is v[] and $a1 is k
这些是MIPS 呼叫约定.函数的前4个参数位于 $ a0 .. $ a3
中,返回值(此处不需要)位于 $ v0
中(和 $ v1
>(如果需要).返回地址在寄存器 $ ra
中.
These are the MIPS calling conventions. First 4 arguments of a function are in $a0..$a3
and return value (not required here) is in $v0
(and $v1
if required). Return address is in register $ra
.
我知道这是用来交换变量的,但是它在做什么,为什么要在v []上加上k?isnt v []一个已声明变量的数组,如何将它与整数k相加?
I know this is used to swap variables but what is it doing here, why is it adding v[] with k? isnt v[] a array of declared variables, how can you add it with a integer k?
v []实际上是一个int数组.保持变量v的是数组的地址.向数组地址添加值是访问数组特定元素的方法.
v[] is indeed an array of int. What holds variable v is the address of the array. Adding a value to an array address is the way to go to specific elements of the array.
swap: # void swap (int v[], int k)
; so v[] is in $a0 and k in $a1
sll $t1, $a1, 2 ; k*=4 (ie sizeof(int))
add $t1, $a0, $t1 ; $t1=@v+4*k==@(v[k])
lw $t0, 0($t1) # temp = v[k];
lw $t2, 4($t1) ; 4(t1) is @(v[k])+4==@(v[k+1]
; $t0==temp==v[k], $t2==v[k+1]
sw $t2, 0($t1) # v[k] = v[k+1];
sw $t0, 4($t1) # v[k+1] = temp;
jr $ra ; go back to caller
这篇关于MIPS汇编中的C数组索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!