Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不使用临时寄存器的情况下交换数组的mips值_Mips - Fatal编程技术网

在不使用临时寄存器的情况下交换数组的mips值

在不使用临时寄存器的情况下交换数组的mips值,mips,Mips,所以我尝试将其转换为mips void swap (int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; } 我想可能是这样的 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 但我不允许使用临时登记簿 我想我必须使用一些lw/sw命令,但我不知道如何才能做到这一点。

所以我尝试将其转换为mips

void swap (int *px, int *py) {
int temp;
temp = *px;
*px = *py;
*py = temp;
}
我想可能是这样的

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
但我不允许使用临时登记簿


我想我必须使用一些lw/sw命令,但我不知道如何才能做到这一点。

有两种解决方案:

1) 数学

void swap(int &x, int &y)
{
     if(*x != *y)
     {
         x = x - y;
         y = y + x;
         x = y - x;
     }
}
注意:不要使用此方法。在某些情况下会导致溢出。

2) XOR(使用此选项)


我将MIPS代码部分留给您自己实现;)

从技术上讲,您已经使用了3个临时寄存器($t0、$t1、$t2)。你需要更具体地说明你能用什么,不能用什么。
void xorSwap (int *x, int *y) 
{
     if (x != y) 
     {
         *x ^= *y;
         *y ^= *x;
         *x ^= *y;
     }
 }