Hey guys, I'm trying to work on an assignment for my assembly language class and cannot seem to grasp how to do this.

I am given C++ code and have to implement it into MIPS. The C++ code can be seen in the spoiler. I have also attached two sample outputs from the c++ code.

Here is what I have in MIPS so far. Basically I have just declared the array and set up the strings for output. I also added the function to check if the input is even or odd. Now, I have to perform the multiplying if even and addition if odd and I cannot figure out how to do this. Any help would be appriciated!

I am given C++ code and have to implement it into MIPS. The C++ code can be seen in the spoiler. I have also attached two sample outputs from the c++ code.

**Warning: Spoiler!**(Click to show)#include

using namespace std;

#define Size 16 // Define a constant

int static nArray[Size]; // Declare an array of Size

void static Initial(Size) // Initialize the array. You can use the procedure in question 1.

{

for (int i = 0; i

{

nArray

using namespace std;

#define Size 16 // Define a constant

int static nArray[Size]; // Declare an array of Size

void static Initial(Size) // Initialize the array. You can use the procedure in question 1.

{

for (int i = 0; i

{

nArray

*= i+1;*

}

};

int static MultiplyEven(int n) // Multiply all even numbers in the array

{

cout << "n = " << n << ", nArray[n] = " << nArray[n] << endl;

if (n == 0)

return nArray[0];

else

return nArray[n] * MultiplyEven(n-2); // Recursive call

}

int static addOdd(int n) // Add all numbers in the array

{

cout << "n = " << n << ", nArray[n] = " << nArray[n] << endl;

if (n == 1)

return nArray[1];

else

return nArray[n] + addOdd(n-2); // Recursive call

}

int static addOrMultiply(int n) // Call different procedures depending on the value of n

{

if (n % 2 == 0) // Test if n is an even number

return MultiplyEven(n);

else

return addOdd(n);

}

void main() // main function is the entry point of the program

{

int n, result;

Initial(Size); // initialize the array

cout << "If an even number is entered, even numbers will be multiplied, otherwise, odd numbers

will be added." << endl;

cout << "Please enter a number between 1 and " << Size-1 << endl;

cin >> n;

while ((n < 1) || (n >= Size)) // check the validity of the input

{

cout << "Please enter a number between 1 and " << Size-1 << endl;

cin >> n;

}

result = addOrMultiply(n);

cout << "result = " << result << endl;

};}

};

int static MultiplyEven(int n) // Multiply all even numbers in the array

{

cout << "n = " << n << ", nArray[n] = " << nArray[n] << endl;

if (n == 0)

return nArray[0];

else

return nArray[n] * MultiplyEven(n-2); // Recursive call

}

int static addOdd(int n) // Add all numbers in the array

{

cout << "n = " << n << ", nArray[n] = " << nArray[n] << endl;

if (n == 1)

return nArray[1];

else

return nArray[n] + addOdd(n-2); // Recursive call

}

int static addOrMultiply(int n) // Call different procedures depending on the value of n

{

if (n % 2 == 0) // Test if n is an even number

return MultiplyEven(n);

else

return addOdd(n);

}

void main() // main function is the entry point of the program

{

int n, result;

Initial(Size); // initialize the array

cout << "If an even number is entered, even numbers will be multiplied, otherwise, odd numbers

will be added." << endl;

cout << "Please enter a number between 1 and " << Size-1 << endl;

cin >> n;

while ((n < 1) || (n >= Size)) // check the validity of the input

{

cout << "Please enter a number between 1 and " << Size-1 << endl;

cin >> n;

}

result = addOrMultiply(n);

cout << "result = " << result << endl;

};

Here is what I have in MIPS so far. Basically I have just declared the array and set up the strings for output. I also added the function to check if the input is even or odd. Now, I have to perform the multiplying if even and addition if odd and I cannot figure out how to do this. Any help would be appriciated!

**Warning: Spoiler!**(Click to show).data

arrayi: .word 0:16 #declare array size 16

size: .word 16

prompt1: .asciiz "Even numbers will be multiplied and odd numbers will be added"

prompt2: .asciiz "Enter a number between 1 and "

newline: .asciiz "\n"

.text

lw $s0,size #load size of array to $s0

la $a0,prompt1

li $v0,4 #output prompt1

syscall

la $a0 newline

li $v0,4 #move to new line

syscall

la $a0 prompt2 #output prompt2

li $v0,4

syscall

addi $a0,$s0,-1 #subtract 1 from size to make it 15

li $v0,1 #print 15

syscall

la $a0,newline

li $v0,4 #move to new line

syscall

li $v0,5 #read input

syscall

move $s1,$v0 #move the input to $s1

evenoddcheck: andi $t0,$s1,1 #and the input with 1 to check even or odd

bne $t0,0,addodd #if input is even, continue, if odd move to addodd

addodd:

arrayi: .word 0:16 #declare array size 16

size: .word 16

prompt1: .asciiz "Even numbers will be multiplied and odd numbers will be added"

prompt2: .asciiz "Enter a number between 1 and "

newline: .asciiz "\n"

.text

lw $s0,size #load size of array to $s0

la $a0,prompt1

li $v0,4 #output prompt1

syscall

la $a0 newline

li $v0,4 #move to new line

syscall

la $a0 prompt2 #output prompt2

li $v0,4

syscall

addi $a0,$s0,-1 #subtract 1 from size to make it 15

li $v0,1 #print 15

syscall

la $a0,newline

li $v0,4 #move to new line

syscall

li $v0,5 #read input

syscall

move $s1,$v0 #move the input to $s1

evenoddcheck: andi $t0,$s1,1 #and the input with 1 to check even or odd

bne $t0,0,addodd #if input is even, continue, if odd move to addodd

addodd: