1,105,556 Community Members

C++ to MIPS conversion?

jalanderful
Newbie Poster
1 post since Feb 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

Hello, I am supposed to write a code in MIPS to do the following below. Basically, you enter an integer,
and it should check to see if it is a palindrome, and if not it will output the number, and keep adding the
numer backwards to it, outputting it, and stopping when it is a palindrome. It will output 10 numbers
and if the 10th number isnt a palindrome, it stops.

I'm having trouble writing this code in MIPS, i was hoping someone could offer up some ideas?
My main trouble is the loop in the Palindrome function. I believe the code I wrote for the
reverse function works well.

Below is something I quickly wrote as a guideline while writing it into MIPS.

int Reverse(int num)
{
    int remainder = 0;
    int rev = 0;

    while (num != 0)
        {
        remainder = num % 10;
        num = num/10;
        rev = rev*10 + remainder;
        }
    return rev;
}

void Palindrome()
{
    int num;

    cin >> num;
    Reverse(num);   
    cout << num;
for (int i = 0; i < 10; i++)
    {
        if ( num != Reverse(num))
        {
        num = num + Reverse(num);
        cout << " " << num;
        }
        if (num == Reverse(num))
        {
        cout << " ";
        }
    }
}


int main()
{
    cout << "Please enter an integer : " << endl;
    Palindrome();

    return 0;
}
Member Avatar
DarkLightning7
Junior Poster
158 posts since Jan 2011
Reputation Points: 12 [?]
Q&As Helped to Solve: 14 [?]
Skill Endorsements: 0 [?]
 
0
 

to make loops in assembly you need to use something similar to the goto function in c.

create a point to "goto". example:

move $t0, $zero  #set $t0 to 0 for your counter.
pos1:
beq $t0, 10, pos2  #terminate the loop if $t0 equals 10
addi $t0, $t0, 1

#do your calculations here

j pos1            #start the next iteration of the loop
pos2:

here are a couple pdfs on converting from c to assembly:

http://www.cs.washington.edu/education/courses/cse378/00au/ctomips2.pdf
http://jjc.hydrus.net/cs61c/handouts/loops4.pdf

as far as reading the number and reversing it it will be much easier to do as a string although the program will use a lot more memory than reading the number as an integer. also one question do you need to find if the number is a palidrome in binary or decimal?

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article