以 Mips 为单位的递归

发布于 2024-10-06 06:36:17 字数 1563 浏览 0 评论 0原文

我目前正在尝试弄清楚如何编写一个函数,以按照该算法在 MIPS 中查找最小整数...

int Min( int[] A, int low, int high)
{   if (low== high) return A[low];
     int mid = (low+high)/2;
     int  min1 = Min( int[] A, low, mid);
     int   min2 =Min( int[] A, mid +1, high);
     if(min1>min2) return min2;
     return min1;
}

当我尝试在 MIPS 中对此进行编码时,我遇到了问题。这是我当前的 MIPS 代码。用户最多输入 6 个整数,这些整数存储在数组中。寄存器 $a0、$a1 和 $a2 用作函数的参数。

  • $a0 = int[]A
  • $a1 = int low //index
  • $a2 = int high //index

这是递归函数...

min:
bne $a1, $a2, recur
mul $t4, $a1, 4
add $a0, $a0, $t4
lw $v1, 0($a0) 
jr $ra
# recursion start
recur:
addiu $sp, $sp, -12 #reserve 12 bytes on stack
sw $ra, 0($sp) #push return address
# mid = (low+high)/2 t0 = mid t1= min1 t2=min2 t3 = mid+1
add $t0, $a1, $a2 # t0 = low + high
div $t0, $t0, 2 # t0 = (low+high)/2


# mid1 = min(int[]A,low,mid)
min1:
sw $a2, 4($sp) #push high
addi $t3, $t0, 1 # mid+1
sw $t3, 8($sp) #store mid+1
move $a2, $t0 #change high to mid
jal min #compute
# check
move $t1, $v1 #set up the min1 = return value

# mid2 = min(int[]A,mid+1,high)
min2:
lw $a2, 4($sp) #reload high prior call
lw $a1, 8($sp) #change low to mid+1
jal min #compute
move $t2, $v1 #set as the min2 = return value

confirm:
# return mid2 if mid1 > mid2
bgt $t1, $t2, returnMid2
# else return mid1
move $v1, $t1
j minFinal
returnMid2:
move $v1, $t2
minFinal:
lw $ra, 0($sp)
addiu $sp, $sp, 12 #release stack
jr $ra

问题是我在程序中输入的任何整数组合,我永远不会得到最小值而是数字“543976553”。我一直在查看我的代码和注释,但我仍然没有任何线索。

I'm currently trying to figure out how to code a function of finding the lowest integer in MIPS following this algorithm...

int Min( int[] A, int low, int high)
{   if (low== high) return A[low];
     int mid = (low+high)/2;
     int  min1 = Min( int[] A, low, mid);
     int   min2 =Min( int[] A, mid +1, high);
     if(min1>min2) return min2;
     return min1;
}

I'm receiving problems as I attempt to code this in MIPS. Here is my current MIPS code. The user inputs up to 6 integers which are stored in an array. The registers $a0, $a1, and $a2 are used as arguments for the function.

  • $a0 = int[]A
  • $a1 = int low //index
  • $a2 = int high //index

Here is the recursion function...

min:
bne $a1, $a2, recur
mul $t4, $a1, 4
add $a0, $a0, $t4
lw $v1, 0($a0) 
jr $ra
# recursion start
recur:
addiu $sp, $sp, -12 #reserve 12 bytes on stack
sw $ra, 0($sp) #push return address
# mid = (low+high)/2 t0 = mid t1= min1 t2=min2 t3 = mid+1
add $t0, $a1, $a2 # t0 = low + high
div $t0, $t0, 2 # t0 = (low+high)/2


# mid1 = min(int[]A,low,mid)
min1:
sw $a2, 4($sp) #push high
addi $t3, $t0, 1 # mid+1
sw $t3, 8($sp) #store mid+1
move $a2, $t0 #change high to mid
jal min #compute
# check
move $t1, $v1 #set up the min1 = return value

# mid2 = min(int[]A,mid+1,high)
min2:
lw $a2, 4($sp) #reload high prior call
lw $a1, 8($sp) #change low to mid+1
jal min #compute
move $t2, $v1 #set as the min2 = return value

confirm:
# return mid2 if mid1 > mid2
bgt $t1, $t2, returnMid2
# else return mid1
move $v1, $t1
j minFinal
returnMid2:
move $v1, $t2
minFinal:
lw $ra, 0($sp)
addiu $sp, $sp, 12 #release stack
jr $ra

The problem is whatever combination of integers I input during the program, I never get the minimum value but rather the number "543976553". I've been looking over my code and notes and I still don't have a clue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名的好友 2024-10-13 06:36:17

对于 mid1,尝试将返回值放入堆栈,然后在调用 mid2 后将其移至 $t1。然后将 $v0 与 $t1 进行比较,而不是将 $t1 与 $t2 进行比较

For mid1, try putting the return value on the stack and then moving it to $t1 AFTER mid2 is called. Then compare $v0 with $t1 instead of comparing $t1 with $t2

痴梦一场 2024-10-13 06:36:17

在MIPS中使用div命令时,不需要从$LO获取商吗?

因此,(high+low)/2 的逻辑可能看起来像

add $t0, $a1, $a2 # t0 = low + high
addi $t5, $zero, 2 # t5=2
div $t0, $t5 # LO = t0/t5, HI = t0%t5
addi $t0, $LO, 0 # t0 = LO (t0/t5)

其中一些行是按照我学习 MIPS 的方式编写的,但您可能有不同的样式将立即值加载到寄存器中。

我也不能保证这会解决所有问题,但乍一看,这是我注意到的。

When using the div command in MIPS, doesn't one need to acquire the quotient from $LO ?

thus your logic for (high+low)/2 might look something like

add $t0, $a1, $a2 # t0 = low + high
addi $t5, $zero, 2 # t5=2
div $t0, $t5 # LO = t0/t5, HI = t0%t5
addi $t0, $LO, 0 # t0 = LO (t0/t5)

some of those lines are written just how I learned to do MIPS but you may have a different style for loading immediate values into a register.

I also can't promise this will fix everything, but at first glance, that is something I noticed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文