Assembly MIPS:将C代码转换为程序集
我正在尝试将下面的C代码翻译成MIPS汇编语言,我有点理解其中的大部分内容,但是,我不知道第一行在汇编中的等价物是什么Assembly MIPS:将C代码转换为程序集,assembly,mips,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; } 如果这是一个本地数组
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;
}
如果这是一个本地数组,您可以在堆栈上为它分配空间,然后从代码中初始化它。 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版本是:
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
您的代码中有许多错误
addi $t1, baseAddress, 8 #cut off point to stop the loop; array[2]
addi $t1, $zero, $zero #initialize i=0
在第一行中,没有这样的指令,除非baseAddress是寄存器。第二行应该是add
,而不是addi
,因为$zero不是立即数
Start:
lw $t2, base(offset)
sll $t2, $t0, 1 #mutiply $t2 by 2
sw $t2, base(offset)
上面的线路也有问题。您只需将一个单词加载到$t2,然后立即将另一个值存储到$t2,因此之前的加载没有意义
#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;
}
使用名称空间std;
//原型
int-maxIs(int*x,int-n);
int mini(int*x,int n);
void avgIs(int*x,int*n,int*theAvg,int*theRem);
内部主(空)
{
int n=8;
int x[]={1,2,3,4,5,6,7,8};
int theMax、theMin、THAVG、theRem;
最大值=最大值(x,n);
theMin=mini(x,n);
avgIs(x、n、THAVG和theRem);
您好,谢谢您的回复,我很感激。我想知道为什么lw和sw指令中没有结束语句。还有,为什么lw和sw指令中没有基址?不确定您所说的结束语句是什么,可能是一个
jr$ra
?因为您的C代码不是完整的函数或程序,我只是在一个simi中提供了asm代码至于缺少的基址:这是因为地址已经通过添加$sp
(数组地址)计算出来,所以基址仅为0。如果需要,可以将其写出。请注意,基址不能是另一个寄存器,lw$t2,$sp($t1)
是非法的。您是否在错误的位置发布了此内容?这与问题无关,也无法回答问题。
Start:
lw $t2, base(offset)
sll $t2, $t0, 1 #mutiply $t2 by 2
sw $t2, base(offset)
#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;
}