在交换数组元素在MIPS汇编? (更清晰的) [英] Swapping elements in array in MIPS assembly? (the clearer one)

查看:551
本文介绍了在交换数组元素在MIPS汇编? (更清晰的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有一个类似的问题<一href=\"http://stackoverflow.com/questions/10924662/swapping-two-elements-in-an-array-in-mips-assembly\">here.我希望把它作为这个问题的延续,但更彻底。

I know there is a similar question here. I'd like to think of it as a continuation of that question but more thorough.

下面是C code的相关作品,我想为翻译成MIPS

Here is the relevant pieces of a C code which I want to translate to MIPS:

int a = [100];
...
j = 0;
while (j<50){
    if (a[j] > a[99-j]) {
        tmp = a[99-j];
        a[99-j] = a[j];
        a[j] = tmp;
    }
    j = j+1;
}

(所以基本上就像反向)

(So it's basically works like reverse)

到目前为止,我已经做过中的 MIPS

So far I have done in MIPS:

    .data
array:  .word 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80,
     79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60,
     59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40,
     39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20,
     19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,0
    .text
    .globl main
main:   
    la  $s1, array  # $s1 = array address
    la  $s3, array
    li  $s0, 0      # j = 0
    lw  $t0, 0($s1) # $t0 = a[0]
    lw  $t1, 396($s3)   # $t1 = a[99]

while:  beq $s0, 50, end    # break from while loop once j = 50

if: blt $t0, $t1, iterj # if the if statement not satisfied, branch to iterj
    sw  $t1, 0($s1) # write to a[99-j]
    sw  $t0, 396($s1)   # write to a[j]

iterj:  addi    $s0, $s0, 1 # increment j
    addi    $s1, $s1, 4 # increment source address starting with j = 0
    addi    $s3, $s3, -4    # decrement source address starting with j = 99
    lw  $t0, 0($s1) 
    lw  $t1, 396($s3)   
    j   while
end:

总结我在MIPS确实

基本上,我试图在初始化$ S1阵列并尽我所能,使上互换。然后,我意识到,我还需要在[99]在[0],并在同一时间递减的源地址递增的源地址。所以,我想我不能只用1阵列,然后我在$ S3由相同的阵列来处理它:

Basically, I tried to initialise the array in $s1 and tried my best to make swaps on it. Then I realised that I also need to increment the source address at a[0] and at the same time decrement the source address at a[99]. So I was thinking I can't use just 1 array, which I then made an identical array in $s3 to handle it:

addi    $s1, $s1, 4 # increment source address starting with j = 0
addi    $s3, $s3, -4    # decrement source address starting with j = 99

现在是我需要帮助的一部分:

Now is the part I need help:

在code ++工程(正确地交换)按顺序从0到31(MARS中所示的所有十六进制格式),然后31日至1然后突然63 47.显然事情我做错了。 所有我需要做的是通过交换返回0到63(十六进制)或0到99(以十进制)。任何提示?

The code works (swapping correctly) sequentially from 0 to 31 (all in hex form shown in MARS) then 31 to 1 then suddenly 63 to 47. There's obviously something I'm doing wrong. All I just need it to do is return 0 to 63 (in hex) or 0 to 99 (in dec) via swapping. Any hints?

推荐答案

好了,你错了,需要两个数组,但它是你来到这样的结论。请记住,可以用C写的东西绝对可以用汇编语言编写的,因此您提供的C code必须作为一种模式。

Well, you're wrong about requiring two arrays, however it is that you came to such a conclusion. Remember that anything that can be written in C can certainly be written in assembly, so the C code you provided should serve as a model.

---编辑

什么我建议首先是加载阵列的地址,并计算的结束地址。

What I would suggest first is load the address of the array, and calculate the end address.

la   $t0 array
addi $t1 $t0 396 #last element of array

然后在循环体:

lw  $t2 0($t0)
lw  $t3 0($t1)
sw  $t2 0($t1)
sw  $t3 0($t0)

addi $t0 $t0 4 
addi $t1 $t1 -4
blt  $t0 $t1 loop

这篇关于在交换数组元素在MIPS汇编? (更清晰的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆