Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello there, I\'m working on translating C++ to MIPS but having some difficultie

ID: 3543384 • Letter: H

Question

Hello there,

I'm working on translating C++ to MIPS but having some difficulties. Followes is both the c++ code and  the MIPS code I have started working on.

I'M NOT LOOKING FOR AN AUTOMATED TRANSLATION FOR THE CODE. I CAN RUN A SINGLE COMMMAND AND GENERATE SUCH RESULT.


------------------------------------------------------------------------------

#include <iostream>

using namespace std;


int byteToDec(int arg0, char *arg1);

void IPtoDD(int arg0, char *arg1);


int main()

{

int num = 0x8234000a;

char str[16];


IPtoDD(num, str);

cout << str << endl;


num = 0xab0932ba;


cout << num << endl;

IPtoDD(num, str);

cout << str << endl;


system("PAUSE");

}


int byteToDec(int arg0, char *arg1)

{

int temp, flag = 0, count = 0;


if (arg0==0)

{

*arg1 = '0';

return 1;

}

else

{

temp = arg0/100;


if (temp != 0)

{

*arg1++ = (char) temp + 0x30;

count++;

flag = 1;

}


temp = (arg0 % 100) / 10;


if ((flag!=0) || (temp != 0))

{

*arg1++ = (char) temp + 0x30;

count++;

}


temp = arg0 % 10;

*arg1 = (char) temp + 0x30;

count++;


return count;

}

}


void IPtoDD(int arg0, char *arg1)

{

int temp, numChar, shift = 24;


for (int i=0; i<4; i++)

{

temp = arg0 >> shift;

temp = temp & 0x000000ff;

numChar = byteToDec(temp,arg1);

arg1 += numChar;

*arg1++ = '.';

shift -= 8;

}


arg1--;

*arg1 = 0;


return;

}



-------------------------------------------------------------------

Explanation / Answer

please rate - thanks


took a while, but your code, made to work

any questions ask


.data


endl: .asciiz " " # new line
str: .byte 0:16 # char str[16]
# $s0 num

.text
.globl main

main:
li $s0,0x8234000a
move $a0, $s0 # IPtoDD(num, str);
la $a1, str
jal IPtoDD

li $v0, 4 # cout << str << endl;
la $a0, str
syscall
li $v0, 4
la $a0, endl
syscall

li $s0,0xab0932ba
move $a0, $s0 # IPtoDD(num, str);
la $a1, str
jal IPtoDD

li $v0, 4 # cout << str << endl;
la $a0, str
syscall

li $v0, 4
la $a0, endl
syscall

li $v0, 10
syscall

#int byteToDec(int arg0, char *arg1)
#arg0 in $a0, arg1 in $a1
#return result in $v0
#ptr in $t0
#returns the number of characters in the string that it created
#
#copy of arg0 $s7
#copy of arg1 $s1
#temp in $s2
#flag in $s3
#count $s4

byteToDec:
move $s1,$a1
addi $s3, $0, 0 # int temp, flag=0, count =0;
addi $s4, $0, 0
bne $t3, 0, L1 # if (arg0==0) {
li $t0, '0' # *arg1 = '0';
sb $t0, ($s1)
addi $s4, $s4, 1 # count++;
jr $ra # return 1;
# }
L1: # {
li $t8,100
div $t3,$t8 # temp=arg0/100;
mflo $s2
beq $s2,0,L2 # if (temp != 0) {
addi $t4, $s2,0x30 # *arg1++ = (char) temp + 0x30;
sb $t4, 0($s1)
addi $s1,$s1,1
addi $s4, $s4, 1 # count++;
li $s3,1 # flag = 1;
# }

L2:

mfhi $s2
div $s2, $s2, 10
or $s7,$s2,$s3   # if ((flag!=0) || (temp != 0)) {
beq $s7, 0, L3
addi $t5, $s2, 0x30 # *arg1++ = (char) temp + 0x30;
sb $t5, ($s1)
addi $s1, $s1, 1
addi $s4, $s4, 1 # count++;
# }

L3:
rem $s2, $t3, 10 # temp=arg0;
addi $t2, $s2, 0x30 # *arg1 = (char) temp + 0x30;
sb $t2, ($s1)
addi $s1,$s1,1
addi $s4, $s4, 1 # count++;
jr $ra # return count
# }
# }

#copy of arg0 $s7 in function IPtoDD
#copy of arg1 $s1  
#temp in $s2
#flag in $s3
#count in $s4
#numchar in $s5
#shift in $s6
#i in $s8

IPtoDD:
li $s6, 24 # int temp, numChar, shift = 24;
li $t1,4

move $a3,$ra;

loop:
move $t2,$a0
blez $t1, out # for (int i=0; i<4; i++) {             
srlv $t3, $t2, $s6 # temp = arg0 >> shift;
andi $t3,$t3,0x000000ff # temp = temp & 0x000000ff;
jal byteToDec
add $s5, $0, $s4 # numChar = byteToDec(temp,arg1); # numChar = byteToDec(temp,arg1);
add $a1, $a1, $s5 # arg1 += numChar;
li $t4, '.' # *arg1++ = '.';
sb $t4, ($a1)
addi $a1, $a1, 1
add $s6, $s6, -8 # shift -= 8;
addi $t1,$t1,-1
b loop
out: # }
sub $a1, $a1, 1 # arg1--;
li $s1, 0
sb $s1, ($a1); # *arg1 = 0;
move $ra,$a3
jr $ra # return
# }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote