Question

In: Computer Science

I need assistance translating a custom C++ program to MIPS. My C++ code is the following:...

I need assistance translating a custom C++ program to MIPS. My C++ code is the following: I have made numerous attempts on my own to no avail, any assistance is appreciated. Also, template code for this solution is provided below:

#include

int moveRobots(int *, int *, int, int );

int getNew(int, int);

int main()

{

int x[4], y[4], i, j, myX = 25, myY = 25, move, status = 1;

// initialize positions of four robots

x[0] = 0; y[0] = 0;

x[1] = 0; y[1] = 50;

x[2] = 50; y[2] = 0;

x[3] = 50; y[3] = 50;

cout << "Your coordinates: 25 25\n";

while (status == 1) {

    cout << "Enter move (1 for +x, -1 for -x, 2 for + y, -2 for -y):";

    cin >> move;

    // process user's move

    if (move == 1)

      myX++;

    else if (move == -1)

      myX--;

    else if (move == 2)

      myY++;

    else if (move == -2)

      myY--;

    // update robot positions

    status = moveRobots(&x[0],&y[0],myX,myY);

    cout << "Your coordinates: " << myX << " " << myY << endl;

   

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

      cout << "Robot at " << x[i] << " " << y[i] << endl;

}

cout << "AAAARRRRGHHHHH... Game over\n";

}

int moveRobots(int *arg0, int *arg1, int arg2, int arg3)

{

int i, *ptrX, *ptrY, alive = 1;

ptrX = arg0;

ptrY = arg1;

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

    *ptrX = getNew(*ptrX,arg2); // update x-coordinate of robot i

    *ptrY = getNew(*ptrY,arg3); // update y-coordinate of robot i

    // check if robot caught user

    if ((*ptrX == arg2) && (*ptrY == arg3)) {

      alive = 0;

      break;

    }

    ptrX++;

    ptrY++;

}

return alive;

}

// move coordinate of robot closer to coordinate of user

int getNew(int arg0, int arg1)

{

int temp, result;

temp = arg0 - arg1;

if (temp >= 10)

    result = arg0 - 10;

else if (temp > 0)

    result = arg0 - 1;

else if (temp == 0)

    result = arg0;

else if (temp > -10)

    result = arg0 + 1;

else if (temp <= -10)

    result = arg0 + 10;

return result;

}

The following template code is given:

#
#   A proper program header goes here...
#
#
   .data      
x:   .word   0:4   # x-coordinates of 4 robots
y:   .word   0:4   # y-coordinates of 4 robots

str1:   .asciiz   "Your coordinates: 25 25\n"
str2:   .asciiz   "Enter move (1 for +x, -1 for -x, 2 for + y, -2 for -y):"
str3:   .asciiz   "Your coordinates: "
sp:   .asciiz   " "
endl:   .asciiz   "\n"
str4:   .asciiz   "Robot at "
str5:   .asciiz   "AAAARRRRGHHHHH... Game over\n"
  
#i   $s0
#myX   $s1
#myY   $s2
#move   $s3
#status   $s4
#temp,pointers   $s5,$s6
   .text
#   .globl   inc
#   .globl   getNew

main:   li   $s1,25       # myX = 25
   li   $s2,25       # myY = 25
   li   $s4,1       # status = 1

   la   $s5,x
   la   $s6,y

   sw   $0,($s5)   # x[0] = 0; y[0] = 0;
   sw   $0,($s6)
   sw   $0,4($s5)   # x[1] = 0; y[1] = 50;
   li   $s7,50
   sw   $s7,4($s6)
   sw   $s7,8($s5)   # x[2] = 50; y[2] = 0;
   sw   $0,8($s6)
   sw   $s7,12($s5)   # x[3] = 50; y[3] = 50;
   sw   $s7,12($s6)

   la   $a0,str1   # cout << "Your coordinates: 25 25\n";
   li   $v0,4
   syscall
  
   bne   $s4,1,main_exitw   # while (status == 1) {
main_while:
   la   $a0,str2   # cout << "Enter move (1 for +x,
   li   $v0,4       #   -1 for -x, 2 for + y, -2 for -y):";
   syscall
  
   li   $v0,5       # cin >> move;
   syscall
   move   $s3,$v0

   bne   $s3,1,main_else1# if (move == 1)
   add   $s1,$s1,1   # myX++;
   b   main_exitif
main_else1:
   bne   $s3,-1,main_else2   # else if (move == -1)
   add   $s1,$s1,-1   # myX--;
   b   main_exitif
main_else2:
   bne   $s3,2,main_else3   # else if (move == 2)
   add   $s2,$s2,1   # myY++;
   b   main_exitif
main_else3:   bne   $s3,-2,main_exitif   # else if (move == -2)
   add   $s2,$s2,-1   # myY--;
  
main_exitif:   la   $a0,x       # status = moveRobots(&x[0],&y[0],myX,myY);
   la   $a1,y
   move   $a2,$s1
   move   $a3,$s2
   jal   moveRobots
   move   $s4,$v0

   la   $a0,str3   # cout << "Your coordinates: " << myX
   li   $v0,4       # << " " << myY << endl;
   syscall
   move   $a0,$s1
   li   $v0,1
   syscall
   la   $a0,sp
   li   $v0,4
   syscall
   move   $a0,$s2
   li   $v0,1
   syscall
   la   $a0,endl
   li   $v0,4
   syscall

   la   $s5,x
   la   $s6,y
   li   $s0,0       # for (i=0;i<4;i++)
main_for:   la   $a0,str4   # cout << "Robot at " << x[i] << " "
   li   $v0,4       # << y[i] << endl;
   syscall
   lw   $a0,($s5)
   li   $v0,1
   syscall
   la   $a0,sp
   li   $v0,4
   syscall
   lw   $a0,($s6)
   li   $v0,1
   syscall
   la   $a0,endl
   li   $v0,4
   syscall
   add   $s5,$s5,4
   add   $s6,$s6,4
   add   $s0,$s0,1
   blt   $s0,4,main_for

   beq   $s4,1,main_while
               # }          
main_exitw:   la   $a0,str5   # cout << "AAAARRRRGHHHHH... Game over\n";
   li   $v0,4
   syscall
   li   $v0,10       #}
   syscall

Solutions

Expert Solution

addiu $sp,$sp,-88

sw $31,84($sp)

sw $fp,80($sp)

move $fp,$sp

li $2,25 # 0x19

sw $2,28($fp)

li $2,25 # 0x19

sw $2,32($fp)

li $2,1 # 0x1

sw $2,36($fp)

sw $0,40($fp)

sw $0,56($fp)

sw $0,44($fp)

li $2,50 # 0x32

sw $2,60($fp)

li $2,50 # 0x32

sw $2,48($fp)

sw $0,64($fp)

li $2,50 # 0x32

sw $2,52($fp)

li $2,50 # 0x32

sw $2,68($fp)

lui $2,%hi($LC0)

addiu $5,$2,%lo($LC0)

lui $2,%hi(_ZSt4cout)

addiu $4,$2,%lo(_ZSt4cout)

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

$L9:

lw $3,36($fp)

li $2,1 # 0x1

bne $3,$2,$L2

nop

lui $2,%hi($LC1)

addiu $5,$2,%lo($LC1)

lui $2,%hi(_ZSt4cout)

addiu $4,$2,%lo(_ZSt4cout)

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

addiu $2,$fp,72

move $5,$2

lui $2,%hi(_ZSt3cin)

addiu $4,$2,%lo(_ZSt3cin)

jal std::basic_istream<char, std::char_traits<char> >::operator>>(int&)

nop

lw $3,72($fp)

li $2,1 # 0x1

bne $3,$2,$L3

nop

lw $2,28($fp)

nop

addiu $2,$2,1

sw $2,28($fp)

b $L4

nop

$L3:

lw $3,72($fp)

li $2,-1 # 0xffffffffffffffff

bne $3,$2,$L5

nop

lw $2,28($fp)

nop

addiu $2,$2,-1

sw $2,28($fp)

b $L4

nop

$L5:

lw $3,72($fp)

li $2,2 # 0x2

bne $3,$2,$L6

nop

lw $2,32($fp)

nop

addiu $2,$2,1

sw $2,32($fp)

b $L4

nop

$L6:

lw $3,72($fp)

li $2,-2 # 0xfffffffffffffffe

bne $3,$2,$L4

nop

lw $2,32($fp)

nop

addiu $2,$2,-1

sw $2,32($fp)

$L4:

addiu $3,$fp,56

addiu $2,$fp,40

lw $7,32($fp)

lw $6,28($fp)

move $5,$3

move $4,$2

jal moveRobots(int*, int*, int, int)

nop

sw $2,36($fp)

lui $2,%hi($LC2)

addiu $5,$2,%lo($LC2)

lui $2,%hi(_ZSt4cout)

addiu $4,$2,%lo(_ZSt4cout)

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

lw $5,28($fp)

move $4,$2

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

nop

move $3,$2

lui $2,%hi($LC3)

addiu $5,$2,%lo($LC3)

move $4,$3

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

lw $5,32($fp)

move $4,$2

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

nop

move $3,$2

lui $2,%hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)

addiu $5,$2,%lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)

move $4,$3

jal 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> >&))

nop

sw $0,24($fp)

$L8:

lw $2,24($fp)

nop

slt $2,$2,4

beq $2,$0,$L9

nop

lui $2,%hi($LC4)

addiu $5,$2,%lo($LC4)

lui $2,%hi(_ZSt4cout)

addiu $4,$2,%lo(_ZSt4cout)

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

move $4,$2

lw $2,24($fp)

nop

sll $2,$2,2

addiu $3,$fp,24

addu $2,$3,$2

lw $2,16($2)

nop

move $5,$2

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

nop

move $3,$2

lui $2,%hi($LC3)

addiu $5,$2,%lo($LC3)

move $4,$3

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

move $4,$2

lw $2,24($fp)

nop

sll $2,$2,2

addiu $3,$fp,24

addu $2,$3,$2

lw $2,32($2)

nop

move $5,$2

jal std::basic_ostream<char, std::char_traits<char> >::operator<<(int)

nop

move $3,$2

lui $2,%hi(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)

addiu $5,$2,%lo(_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_)

move $4,$3

jal 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> >&))

nop

lw $2,24($fp)

nop

addiu $2,$2,1

sw $2,24($fp)

b $L8

nop

$L2:

lui $2,%hi($LC5)

addiu $5,$2,%lo($LC5)

lui $2,%hi(_ZSt4cout)

addiu $4,$2,%lo(_ZSt4cout)

jal std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)

nop

move $2,$0

move $sp,$fp

lw $31,84($sp)

lw $fp,80($sp)

addiu $sp,$sp,88

j $31

nop

moveRobots(int*, int*, int, int):

addiu $sp,$sp,-48

sw $31,44($sp)

sw $fp,40($sp)

move $fp,$sp

sw $4,48($fp)

sw $5,52($fp)

sw $6,56($fp)

sw $7,60($fp)

li $2,1 # 0x1

sw $2,36($fp)

lw $2,48($fp)

nop

sw $2,28($fp)

lw $2,52($fp)

nop

sw $2,32($fp)

sw $0,24($fp)

$L14:

lw $2,24($fp)

nop

slt $2,$2,4

beq $2,$0,$L12

nop

lw $2,28($fp)

nop

lw $2,0($2)

lw $5,56($fp)

move $4,$2

jal getNew(int, int)

nop

move $3,$2

lw $2,28($fp)

nop

sw $3,0($2)

lw $2,32($fp)

nop

lw $2,0($2)

lw $5,60($fp)

move $4,$2

jal getNew(int, int)

nop

move $3,$2

lw $2,32($fp)

nop

sw $3,0($2)

lw $2,28($fp)

nop

lw $3,0($2)

lw $2,56($fp)

nop

bne $3,$2,$L13

nop

lw $2,32($fp)

nop

lw $3,0($2)

lw $2,60($fp)

nop

bne $3,$2,$L13

nop

sw $0,36($fp)

b $L12

nop

$L13:

lw $2,28($fp)

nop

addiu $2,$2,4

sw $2,28($fp)

lw $2,32($fp)

nop

addiu $2,$2,4

sw $2,32($fp)

lw $2,24($fp)

nop

addiu $2,$2,1

sw $2,24($fp)

b $L14

nop

$L12:

lw $2,36($fp)

move $sp,$fp

lw $31,44($sp)

lw $fp,40($sp)

addiu $sp,$sp,48

j $31

nop

getNew(int, int):

addiu $sp,$sp,-24

sw $fp,20($sp)

move $fp,$sp

sw $4,24($fp)

sw $5,28($fp)

lw $3,24($fp)

lw $2,28($fp)

nop

subu $2,$3,$2

sw $2,12($fp)

lw $2,12($fp)

nop

slt $2,$2,10

bne $2,$0,$L17

nop

lw $2,24($fp)

nop

addiu $2,$2,-10

sw $2,8($fp)

b $L18

nop

$L17:

lw $2,12($fp)

nop

blez $2,$L19

nop

lw $2,24($fp)

nop

addiu $2,$2,-1

sw $2,8($fp)

b $L18

nop

$L19:

lw $2,12($fp)

nop

bne $2,$0,$L20

nop

lw $2,24($fp)

nop

sw $2,8($fp)

b $L18

nop

$L20:

lw $2,12($fp)

nop

slt $2,$2,-9

bne $2,$0,$L21

nop

lw $2,24($fp)

nop

addiu $2,$2,1

sw $2,8($fp)

b $L18

nop

$L21:

lw $2,12($fp)

nop

slt $2,$2,-9

beq $2,$0,$L18

nop

lw $2,24($fp)

nop

addiu $2,$2,10

sw $2,8($fp)

$L18:

lw $2,8($fp)

move $sp,$fp

lw $fp,20($sp)

addiu $sp,$sp,24

j $31

nop

__static_initialization_and_destruction_0(int, int):

addiu $sp,$sp,-32

sw $31,28($sp)

sw $fp,24($sp)

move $fp,$sp

sw $4,32($fp)

sw $5,36($fp)

lw $3,32($fp)

li $2,1 # 0x1

bne $3,$2,$L25

nop

lw $3,36($fp)

li $2,65535 # 0xffff

bne $3,$2,$L25

nop

lui $2,%hi(_ZStL8__ioinit)

addiu $4,$2,%lo(_ZStL8__ioinit)

jal std::ios_base::Init::Init() [complete object constructor]

nop

lui $2,%hi(__dso_handle)

addiu $6,$2,%lo(__dso_handle)

lui $2,%hi(_ZStL8__ioinit)

addiu $5,$2,%lo(_ZStL8__ioinit)

lui $2,%hi(_ZNSt8ios_base4InitD1Ev)

addiu $4,$2,%lo(_ZNSt8ios_base4InitD1Ev)

jal __cxa_atexit

nop

$L25:

nop

move $sp,$fp

lw $31,28($sp)

lw $fp,24($sp)

addiu $sp,$sp,32

j $31

nop

_GLOBAL__sub_I_main:

addiu $sp,$sp,-32

sw $31,28($sp)

sw $fp,24($sp)

move $fp,$sp

li $5,65535 # 0xffff

li $4,1 # 0x1

jal __static_initialization_and_destruction_0(int, int)

nop

move $sp,$fp

lw $31,28($sp)

lw $fp,24($sp)

addiu $sp,$sp,32

j $31

nop

PLEASE GIVE A THUMBS UP!!!! DONT GIVE A THUMBS DOWN IF YOU HAVE ANY QUERY SO COMMENT DOWN I WILL CLEAR IT FOR YOU


Related Solutions

I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a...
I need assistance on this problem in Pseudocode and in C++ Program Program 3: Give a baby $5,000! Did you know that, over the last century, the stock market has returned an average of 10%? You may not care, but you’d better pay attention to this one. If you were to give a newborn baby $5000, put that money in the stock market and NOT add any additional money per year, that money would grow to over $2.9 million by...
Write MIPS assembly code for the following C code. for (i = 10; i < 30;...
Write MIPS assembly code for the following C code. for (i = 10; i < 30; i ++) { if ((ar[i] > b) || (ar[i] <= c)) ar[i] = 0; else ar[i] = a; }
I need assistance on this problem in Pseudocode and in C++ Program 1: Stay on the...
I need assistance on this problem in Pseudocode and in C++ Program 1: Stay on the Screen! Animation in video games is just like animation in movies – it’s drawn image by image (called “frames”). Before the game can draw a frame, it needs to update the position of the objects based on their velocities (among other things). To do that is relatively simple: add the velocity to the position of the object each frame. For this program, imagine we...
Hello, I need some assistance on completing this program in Pseudocode and in C++ Program 2:...
Hello, I need some assistance on completing this program in Pseudocode and in C++ Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is...
4.Translate the following C code to MIPS assembly code. Assume that the value of i is in register $t0, and $s0 holds the base address of the integer MemArray if (i > 10) MemArray[i] = 0; else MemArray[i] = -MemArray[i]; 6.Translate the following C code to MIPS assembly code. Use a minimum number of instructions. Assume that the values of a, b, i, and j are in registers $s0, $s1, $t0, and $t1, respectively. Also, assume that register $s2 holds...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored...
Compile the following C code to MIPS assembly. a. Assume that i and j are stored in register $s1 and $s2, respectively. i = i – 2 + j; b. Assume base address of Array B and the variable i are stored in registers $s3 and $s1, respectively. B[1] = (B[0] x 4) - I; Explain each step in detail! my professor has the following answer: 1) addi $t0, $s1, -2 add $s1, $$t0, $s2 2) lw $t0, 0 ($s3)...
Convert the following C++ code into MIPS assembely. For testing I will be change the values...
Convert the following C++ code into MIPS assembely. For testing I will be change the values for q,y,x with few different values. //q -> $s0 //y -> $s1 //x -> $s2 int main(){ int q = 5; int y = 17; int x = 77; if ( q < 10){ cout << "inside if"; } elseif ( x > 0 || y < 10) { cout << "inside elseif"; } else { cout << "inside else"; } }
This problem needs to be solved with source code. I need a C++ program that will...
This problem needs to be solved with source code. I need a C++ program that will help me solve this question. I need it in C++, please. Writing with comments so it maybe cleared. 1.2. We received the following ciphertext which was encoded with a shift cipher: xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds. 1. Perform an attack against the cipher based on a letter frequency count: How many letters do you have to identify through a frequency count to recover the key? What is...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
Need the correct line of code for c program to solve  this equation . i is a...
Need the correct line of code for c program to solve  this equation . i is a value that the program all ready calculates from user imput. t i = (x*1000)+(y*500)+(z*250
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT