MIPS:将C代码转换为汇编代码

时间:2012-11-16 14:43:38

标签: assembly mips

我正在尝试将下面的C代码翻译成MIPS汇编语言,但是我很了解它的大部分内容,我对于汇编中第一行的等价物感到遗憾...

int ary[3] = {2,3,4};

如果有人可以看看我的C汇编'翻译'并确认我走在正确的轨道上,我会很感激。

C代码

int ary[3] = {2,3,4};
int i=0;

//loop to double array values
for(i=0; i < 3; i++){
    ary[i] = ary[i]*2;
}

我尝试了什么:

add $t0, $s0, $zero #get base address of the array 'ary' (dont understand this part)
addi $t1, baseAddress, 8 #cut off point to stop the loop; array[2]
addi $t1, $zero, $zero #initialize i=0


Start:
lw $t2, base(offset)
sll $t2, $t0, 1 #mutiply $t2 by 2  
sw $t2, base(offset)
addi $t0, $t0, 4 # Increment the address to the next element
bne $t0, $t1, Start # $t0 will keep increasing until reaches stopping point $t1
Exit:

3 个答案:

答案 0 :(得分:2)

如果是本地数组,则在堆栈上为其分配空间,然后从代码初始化它。 C代码的可能asm转换可能如下所示:

    addi $sp, $sp, -12     # allocate space for 3 words, $sp is now the address of the array
    addi $t0, $zero, 2
    sw $t0, ($sp)          # ary[0]=2
    addi $t0, $zero, 3
    sw $t0, 4($sp)         # ary[1]=3
    addi $t0, $zero, 4
    sw $t0, 8($sp)         # ary[2]=4

    addi $t0, $zero, 0     # initialize i=0

Start:
    sll $t1, $t0, 2        # i*4 for element size
    add $t1, $t1, $sp      # add base address of array, $t1 is now &ary[i]
    lw $t2, ($t1)          # load ary[i]
    sll $t2, $t2, 1        # mutiply by 2
    sw $t2, ($t1)          # store back to ary[i]
    addi $t0, $t0, 1       # i++
    addi $t1, $t0, -3      # check if i<3 by doing (i-3)<0
    bltz $t1, Start
    addi $sp, $sp, 12      # free the array

您的asm代码采用了稍微不同的方法,C版本看起来像:

int* end = &ary[3];
for(int* ptr = ary; ptr != end; ptr++)
{
    *ptr = *ptr * 2;
}

固定的asm版本是:

    addi $t1, $sp, 12      # end=&ary[3]
    addi $t0, $sp, 0       # ptr=ary

Start:
    lw $t2, ($t0)          # load ary[i]
    sll $t2, $t2, 1        # mutiply by 2
    sw $t2, ($t0)          # store back to ary[i]
    addi $t0, $t0, 4       # ptr++ (note it is incremented by 4 due to element size)
    bne $t0, $t1, Start    # ptr!=end

答案 1 :(得分:0)

您的代码中存在许多错误

addi $t1, baseAddress, 8 #cut off point to stop the loop; array[2]
addi $t1, $zero, $zero #initialize i=0

在第一行中,除非baseAddress是寄存器,否则没有这样的指令。第二行应该是add,而不是addi,因为$ 0不是立即的

Start:
lw $t2, base(offset)
sll $t2, $t0, 1 #mutiply $t2 by 2  
sw $t2, base(offset)

以上几行也有问题。您只需将一个单词加载到$ t2然后将另一个值临时存储到$ t2,所以之前的加载是没有意义的

答案 2 :(得分:-2)

  #include <iostream>
  using namespace std;

  //prototypes

  int maxIs (int *x, int n);
  int minIs ( int *x, int n);
  void avgIs (int *x, int n, int *theAvg, int *theRem);

  int main(void)
  {
  int n = 8;
  int x[] = {1,2,3,4,5,6,7,8};
  int theMax, theMin, theAvg, theRem;

  theMax = maxIs(x,n);
  theMin = minIs(x,n);
  avgIs(x,n,&theAvg, &theRem);

  cout << "max = " << theMax << "\n";
  cout << "min = " << theMin << "\n";
  cout << "avg = " << theAvg << " " << theRem << "/" << n << "\n";
  cout << "Bye!\n";
  }

  //functions

 int maxIs (int *x, int n )
 {
int i;
int theMax = 0;
for (i=0; i<n; i++)
{
    if (x[i]>theMax) theMax =x[i];
}
return (theMax);
}

int minIs (int *x, int n )
{
int i;
int theMin = 0x7FFF;
for (i=0; i<n; i++)
{
    if (x[i]>theMin) theMin =x[i];
}
return (theMin);
}

void avgIs (int *x, int n, int *theAvg, int *theRem )
{
int i;
int theSum = 0;
for (i=0; i<n; i++)
{
    theSum += x[i];
}
*theAvg = theSum /n;
*theRem = theSum %n;
}
相关问题