Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help translating C++ to Assembly (MIPS)
New Posts  All Forums:Forum Nav:

Help translating C++ to Assembly (MIPS)

post #1 of 5
Thread Starter 
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.

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 = 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;
};




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:
post #2 of 5
What are you confused about? How to access the array? You would use the load procedures (for yours I believe it's lw). It's been a while since I've done this but this should help you:

http://logos.cs.uic.edu/366/notes/MIPS%20Quick%20Tutorial.htm

Scroll down to "Indirect and Based Addressing".

So to start out at your maximum index, I think you would just shift left logical by 2*[program input].

For recursion, that's a bit more complicated (well this example is rather overcomplicated compared to what you're doing, but if you do grasp it you won't have to worry too much about recursion):

http://www.cs.nott.ac.uk/~txa/g51csa/l10-hand.pdf

Honestly that's a bit evil to do on such a basic assignment, but whatever.

Anyway, you'll need to allocate room for one temporary variable and then the return address onto the stack per function call. I think your temporary will simply be the value of the array at that point. Then you just keep whatever "n" is for that function call in a register and go down by 2 per call. (and remember to shift right by 2*that number to get actual array address).


Edit: Actually the whole shifting to get the next array address thing might be a bit off (I'm kinda tired right now so fully possible). For safety just go ahead and do subtract/add additions with increments like they describe in that first link.

Considering you're confused about this it's obvious your professor didn't go into recursion for MIPS much.
Edited by stolemyowncar - 10/4/13 at 12:02am
post #3 of 5
Thread Starter 
Thank you for the response, I really appreciate it.

I guess where I'm stuck is how to get started on the loops for adding and multiplying of the array elements.

And yeah my professor isn't great at explaining so i've had to learn MIPS pretty much exclusively online.

I'm going to attempt to get further so wish me luck...
post #4 of 5
Just remember that it's not a "loop". It's a recursive call. You'll need to allocate space on the stack for where to keep going with your execution, and then any variable you want placed there.
I suppose from such a low level it might kind of "seem" like a loop (because they're both implement with a "jump" when you're at this level), but it's a bit more complicated.

Just make sure to look over the example in that set of slides I gave you and understand it (perhaps even ask a TA to explain a few steps if you need to), and then you should be able to tackle this entire assignment with no issues.
post #5 of 5
Thread Starter 
Yeah they seem fairly similar to me but I understand the difference now. I ended up implementing the code without using the array or recursion because I just couldn't figure it out and I don't have much time left to finish this assignment. I probably won't get full points but hey it works! haha

Here's what I ended up doing
Warning: Spoiler! (Click to show)
prompt1: .asciiz "Even numbers will be multiplied and odd numbers will be added"
prompt2: .asciiz "Enter a number between 1 and "
newline: .asciiz "\n"
result: .asciiz "Result = "
.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
addi $s1,$s1,1 #add 1 to input before computations begin

multeven: subi $t0,$s1,2 #subtract two from input
ble $t0,0,output #go to output if input-2 is 0 or less
mul $s2,$t0,$s1 # input*(input-2)
subi $t0,$t0,2 # (input-2)-2
ble $t0,0,output #go to output if (input-2)-2 is 0 or less
mul $s2,$s2,$t0 # (input-2)*((input-2)-2)
subi $t0,$t0,2 #repeat the above steps 8 times to account for the maximum even input = 14
ble $t0,0,output
mul $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
mul $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
mul $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
mul $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
mul $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output



addodd:
addi $s1,$s1,1 # add one to input before computations begin
subi $t0,$s1,2 # increment input by -2
ble $t0,0,output # go to output if less than or = 0
add $s2,$t0,$s1 # add input and input-2
subi $t0,$t0,2 # increment by -2 again
ble $t0,0,output # go to output if less than or = 0
add $s2,$s2,$t0 # add input-2 and input-4
subi $t0,$t0,2 #repeat above steps to allow for maximum odd input = 15
ble $t0,0,output
add $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
add $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
add $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
add $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
add $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
add $s2,$s2,$t0
subi $t0,$t0,2
ble $t0,0,output
add $s2,$s2,$t0


output:
la $a0,result #output result string
li $v0,4
syscall
la $a0,($s2)
li $v0,1 # output result value
syscall

Thanks again for the responses!
Edited by Cole S - 10/4/13 at 7:13pm
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Help translating C++ to Assembly (MIPS)