In: Computer Science
Translate c++ code into mips assembly:
int main()
{
cout << "Numbers:" << endl;
int x[] = {18, 12, 6, 500, 54, 3, 2, 122};
int i;
for (i=0; i<8; i++)
{
cout << x[i] << endl;
}
return 0;
}
below is my code:
.data
str1: .ascii "Numbers:" str2:
.ascii "\n" x: .word 18,12,6,500,54,3,2,122
.text
.global main
main:
la $a0, str1
ori $v0, $0, 1
syscall
Exit:
ori $v0, $0, 10
syscall
Converted Code: (MIPS)
main:
daddiu $sp,$sp,-80
sd $31,72($sp)
sd $fp,64($sp)
sd $28,56($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(main)))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(main)))
ld $2,%got_page(.LC1)($28)
daddiu $5,$2,%got_ofst(.LC1)
ld $4,%got_disp(_ZSt4cout)($28)
ld $2,%call16(_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc)($28)
move $25,$2
nop
ld $5,%got_disp(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)($28)
move $4,$2
ld $2,%call16(_ZNSolsEPFRSoS_E)($28)
move $25,$2
nop
ld $2,%got_page(.LC0)($28)
daddiu $3,$2,%got_ofst(.LC0)
ldl $3,7($3)
ldr $3,%got_ofst(.LC0)($2)
move $5,$3
daddiu $3,$2,%got_ofst(.LC0)
ldl $4,15($3)
ldr $4,8($3)
daddiu $3,$2,%got_ofst(.LC0)
ldl $6,23($3)
ldr $6,16($3)
move $3,$6
daddiu $2,$2,%got_ofst(.LC0)
ldl $6,31($2)
ldr $6,24($2)
move $2,$6
sd $5,8($fp)
sd $4,16($fp)
sd $3,24($fp)
sd $2,32($fp)
sw $0,0($fp)
.L3:
lw $2,0($fp)
slt $2,$2,8
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(_ZSt4cout)($28)
ld $2,%call16(_ZNSolsEi)($28)
move $25,$2
nop
ld $5,%got_disp(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)($28)
move $4,$2
ld $2,%call16(_ZNSolsEPFRSoS_E)($28)
move $25,$2
nop
lw $2,0($fp)
addiu $2,$2,1
sw $2,0($fp)
b .L3
nop
.L2:
move $2,$0
move $sp,$fp
ld $31,72($sp)
ld $fp,64($sp)
ld $28,56($sp)
daddiu $sp,$sp,80
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,.L7
nop
lw $3,4($fp)
li $2,65535 # 0xffff
bne $3,$2,.L7
nop
ld $2,%got_page(_ZStL8__ioinit)($28)
daddiu $4,$2,%got_ofst(_ZStL8__ioinit)
ld $2,%call16(_ZNSt8ios_base4InitC1Ev)($28)
move $25,$2
nop
ld $6,%got_disp(__dso_handle)($28)
ld $2,%got_page(_ZStL8__ioinit)($28)
daddiu $5,$2,%got_ofst(_ZStL8__ioinit)
ld $4,%got_disp(_ZNSt8ios_base4InitD1Ev)($28)
ld $2,%call16(__cxa_atexit)($28)
move $25,$2
nop
.L7:
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
nop
move $sp,$fp
ld $31,24($sp)
ld $fp,16($sp)
ld $28,8($sp)
daddiu $sp,$sp,32
j $31
nop
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================