convert the c++ code into mips// #include<iostream> using namespace std; int rep
ID: 3703208 • Letter: C
Question
convert the c++ code into mips//
#include<iostream>
using namespace std;
int replace(int,int,int,int[]);
int main() {
int a[5]; //declare an array
a[0]=2; //array int
a[1]=3;
a[2]=4;
a[3]=4;
a[4]=5;
cout << a[] << endl; //show initial values
replace(4,10,5,a); //replace all instances of "4" in the
// array with "10"
//array length is 5, and passing array a
cout << a << endl; //show new values
}
int replace (int existing, int newitem, int size, int a[])
{
int i;
for (i=0; i < size; i++)
{
if (a[i]==existing)
a[i] = newitem;
}
return 0;
}
Explanation / Answer
main:
daddiu $sp,$sp,-64
sd $31,56($sp)
sd $fp,48($sp)
sd $28,40($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(main)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(main)))
li $2,2 # 0x2
sw $2,8($fp)
li $2,3 # 0x3
sw $2,12($fp)
li $2,4 # 0x4
sw $2,16($fp)
li $2,4 # 0x4
sw $2,20($fp)
li $2,5 # 0x5
sw $2,24($fp)
sw $0,0($fp)
.L3:
lw $2,0($fp)
slt $2,$2,5
beq $2,$0,.L2
nop
lw $2,0($fp)
dsll $2,$2,2
daddu $2,$fp,$2
lw $2,8($2)
move $5,$2
ld $4,%got_disp(std::cout)($28)
ld $2,%call16(std::basic_ostream<char, std::char_traits<char> >::operator<<(int))($28)
move $25,$2
1: jalr $25
nop
ld $5,%got_disp(std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&))($28)
move $4,$2
ld $2,%call16(std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&)))($28)
move $25,$2
1: jalr $25
nop
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
b .L3
nop
.L2:
daddiu $2,$fp,8
move $7,$2
li $6,5 # 0x5
li $5,10 # 0xa
li $4,4 # 0x4
ld $2,%got_disp(replace(int, int, int, int*))($28)
move $25,$2
1: jalr $25
nop
daddiu $2,$fp,8
move $5,$2
ld $4,%got_disp(std::cout)($28)
ld $2,%call16(std::basic_ostream<char, std::char_traits<char> >::operator<<(void const*))($28)
move $25,$2
1: jalr $25
nop
ld $5,%got_disp(std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&))($28)
move $4,$2
ld $2,%call16(std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&)))($28)
move $25,$2
1: jalr $25
nop
move $2,$0
move $sp,$fp
ld $31,56($sp)
ld $fp,48($sp)
ld $28,40($sp)
daddiu $sp,$sp,64
j $31
nop
replace(int, int, int, int*):
daddiu $sp,$sp,-64
sd $fp,56($sp)
move $fp,$sp
move $3,$5
move $2,$6
sd $7,32($fp)
sll $4,$4,0
sw $4,16($fp)
sll $3,$3,0
sw $3,20($fp)
sll $2,$2,0
sw $2,24($fp)
sw $0,0($fp)
.L8:
lw $3,0($fp)
lw $2,24($fp)
slt $2,$3,$2
beq $2,$0,.L6
nop
lw $2,0($fp)
dsll $2,$2,2
ld $3,32($fp)
daddu $2,$3,$2
lw $3,0($2)
lw $2,16($fp)
bne $2,$3,.L7
nop
lw $2,0($fp)
dsll $2,$2,2
ld $3,32($fp)
daddu $2,$3,$2
lw $3,20($fp)
sw $3,0($2)
.L7:
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
b .L8
nop
.L6:
move $2,$0
move $sp,$fp
ld $fp,56($sp)
daddiu $sp,$sp,64
j $31
nop
__static_initialization_and_destruction_0(int, int):
daddiu $sp,$sp,-48
sd $31,40($sp)
sd $fp,32($sp)
sd $28,24($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(__static_initialization_and_destruction_0(int, int))))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(__static_initialization_and_destruction_0(int, int))))
move $3,$4
move $2,$5
sll $3,$3,0
sw $3,0($fp)
sll $2,$2,0
sw $2,4($fp)
lw $3,0($fp)
li $2,1 # 0x1
bne $3,$2,.L12
nop
lw $3,4($fp)
li $2,65535 # 0xffff
bne $3,$2,.L12
nop
ld $2,%got_page(std::__ioinit)($28)
daddiu $4,$2,%got_ofst(std::__ioinit)
ld $2,%call16(std::ios_base::Init::Init())($28)
move $25,$2
1: jalr $25
nop
ld $6,%got_disp(__dso_handle)($28)
ld $2,%got_page(std::__ioinit)($28)
daddiu $5,$2,%got_ofst(std::__ioinit)
ld $4,%got_disp(std::ios_base::Init::~Init())($28)
ld $2,%call16(__cxa_atexit)($28)
move $25,$2
1: jalr $25
nop
.L12:
nop
move $sp,$fp
ld $31,40($sp)
ld $fp,32($sp)
ld $28,24($sp)
daddiu $sp,$sp,48
j $31
nop
_GLOBAL__sub_I_main:
daddiu $sp,$sp,-32
sd $31,24($sp)
sd $fp,16($sp)
sd $28,8($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(_GLOBAL__sub_I_main)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(_GLOBAL__sub_I_main)))
li $5,65535 # 0xffff
li $4,1 # 0x1
ld $2,%got_page(__static_initialization_and_destruction_0(int, int))($28)
daddiu $2,$2,%got_ofst(__static_initialization_and_destruction_0(int, int))
move $25,$2
1: jalr $25
nop
move $sp,$fp
ld $31,24($sp)
ld $fp,16($sp)
ld $28,8($sp)
daddiu $sp,$sp,32
j $31
nop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.