I am lost on this and would appreciate any help! Translate the following C code
ID: 3884512 • Letter: I
Question
I am lost on this and would appreciate any help!
Translate the following C code into MIPS Assembly Language:
int x, y;
y = 0;
if( x < 10 ) y = 1;
Translate the following C code into MIPS Assembly Language:
int i, n, *a, *b, *c;
for( i=0; i<n; i++ )
c[i] = a[i] + b[i];
Translate the following C code into MIPS Assembly Language:
void three( int n, int *a )
{
int i;
if ( 1 > n ) return;
a[0] = 1;
if( 2 > n ) return;
for( i=2, a[1] = 1; i<n; i++ )
{
a[i] = a[i-1] + a[i-2];
}
}
Translate the following C code into MIPS Assembly Language:
int four( int n )
{
int m = n;
if( 0 == n ) return 0;
if( 0 > m ) m = -m;
if( 1 == m ) return n;
return n * four( m - 1 );
}
Translate the following C code into MIPS Assembly Language:
void main( void )
{
printf( “three(5) = %d “, three (5) );
printf( “four(7) = %d ”, four(7) );
}
Explanation / Answer
NOTE:- I converted the two function three() and four() in MIPS ; but in main you have used three() in printf which causes two errors; first of all,it doesn't return any value as return type of three is void so you can't use three() in printf and second error is you are calling three(5) which is also wrong, as three() function requires two parameters but not one,I did changed that error but still you can't use in printf yet.
3)
C code:-
void three(int n,int *a){
int i;
if(1>n) return;
a[0]=1;
if(2 > n) return;
for(i=2,a[1]=1;i<n;i++){
a[i]=a[i-1] + a[i-2];
}
}
MIPS code:-
three(int, int*):
addiu $sp,$sp,-24
sw $fp,20($sp)
move $fp,$sp
sw $4,24($fp)
sw $5,28($fp)
lw $2,24($fp)
nop
blez $2,$L6
nop
lw $2,28($fp)
li $3,1 # 0x1
sw $3,0($2)
lw $2,24($fp)
nop
slt $2,$2,2
bne $2,$0,$L7
nop
li $2,2 # 0x2
sw $2,8($fp)
lw $2,28($fp)
nop
addiu $2,$2,4
li $3,1 # 0x1
sw $3,0($2)
lw $3,8($fp)
lw $2,24($fp)
nop
slt $2,$3,$2
beq $2,$0,$L1
nop
lw $2,8($fp)
nop
sll $2,$2,2
lw $3,28($fp)
nop
addu $2,$3,$2
lw $4,8($fp)
li $3,1073676288 # 0x3fff0000
ori $3,$3,0xffff
addu $3,$4,$3
sll $3,$3,2
lw $4,28($fp)
nop
addu $3,$4,$3
lw $4,0($3)
lw $5,8($fp)
li $3,1073676288 # 0x3fff0000
ori $3,$3,0xfffe
addu $3,$5,$3
sll $3,$3,2
lw $5,28($fp)
nop
addu $3,$5,$3
lw $3,0($3)
nop
addu $3,$4,$3
sw $3,0($2)
lw $2,8($fp)
nop
addiu $2,$2,1
sw $2,8($fp)
b $L5
nop
nop
b $L1
nop
nop
move $sp,$fp
lw $fp,20($sp)
addiu $sp,$sp,24
j $31
nop
4)
C code:-
int four(int n){
int m = n;
if( 0 == n) return 0;
if(0 > m) m = -m;
if(1 == m) return n;
return n * four(m-1);
}
MIPS code:-
four(int):
addiu $sp,$sp,-40
sw $31,36($sp)
sw $fp,32($sp)
move $fp,$sp
sw $4,40($fp)
lw $2,40($fp)
nop
sw $2,24($fp)
lw $2,40($fp)
nop
bne $2,$0,$L2
nop
move $2,$0
b $L3
nop
lw $2,24($fp)
nop
bgez $2,$L4
nop
lw $2,24($fp)
nop
subu $2,$0,$2
sw $2,24($fp)
lw $3,24($fp)
li $2,1 # 0x1
bne $3,$2,$L5
nop
lw $2,40($fp)
b $L3
nop
lw $2,24($fp)
nop
addiu $2,$2,-1
move $4,$2
jal four(int)
nop
move $3,$2
lw $2,40($fp)
nop
mult $3,$2
mflo $2
move $sp,$fp
lw $31,36($sp)
lw $fp,32($sp)
addiu $sp,$sp,40
j $31
nop
5)
c code:-
int main(void){
printf("three(3)=%d ",three(5,b));
printf("four(7)=%d ",four(7));
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.