在MIPS组合件中交换阵列中的两个元素 [英] Swapping two elements in an array in MIPS assembly

查看:82
本文介绍了在MIPS组合件中交换阵列中的两个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习MIPS ISA.如果要在MIPS中执行此功能:

I'm trying to learns the MIPS ISA. If I want to do this function in MIPS:

A[2*i]=A[2*k+j];

我将如何处理?我还要感谢我能阅读的其他任何内容,即可以阅读有关如何解决此类问题的链接.

How would I go about this? I would also appreciate any other content that I can read, i.e links where I can read up on how to solve this kind of problem.

推荐答案

我们可以将其细分为2部分:

We can break this down to 2 parts:

  1. 计算A [2 * i]和A [2 * k + j]的地址
  2. 将第二个地址处的值分配给第一个地址处的内存

我只想向#1讲话(啊).

I'm only going to address (ahem) #1.

要计算数组元素的地址,您需要了解三件事:

To calculate the address of an array element, you need to know 3 things:

  1. 数组开头的地址
  2. 所需元素的索引
  3. 数组元素的大小

我假设您知道如何计算或仅知道#1& #3.剩下#2,其中涉及简单的算术运算. (由于您尚未说明i,j& k的表示方式,因此我对此无能为力.)

I assume you know how to compute, or just know, #1 & #3. That leaves #2, which involves simple arithmetic. (Since you haven't indicated how i, j & k are represented, I can't help too much there).

然后,最后一步是将索引乘以数组元素的大小;这使您可以从数组的开始偏移所需元素.将其添加到数组开头的地址,即可获得元素的地址.

The final step, then, is to multiply the index by the size of an array element; this gives you the offset of your desired element from the start of the array. Add that to the address of the start of the array, and you have your element's address.

P.S.您要翻译的代码不会交换两个元素.它将一个复制到另一个.

P.S. The code you're translating doesn't swap two elements; it copies one over the other.

这篇关于在MIPS组合件中交换阵列中的两个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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