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

Recommended Answers

All 3 Replies

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?

#include <iostream>

using namespace std;

const double TICKET = 124;

const double DISCOUNT = 10;

int main()
 int visitors;
int age;
double total = 0;
double price;

cout << "Please enter the number of visitors: " << endl;
cin >> visitors;

for (int i = 1; i <= visitors; i++)
{   if (i % 5 == 0) // Every 5th ticket is free
    {   cout << "Your ticket is free!" << endl;
        price = 0;
    }
    else 
    {   cout << "Please enter age of visitor: " << endl;
        cin >> age;
        if (age < 5 || age > 65) //find discount is younger than 5 or older than 65
        {   price = TICKET - DISCOUNT;  
            cout << "You get a discount of 10 " << price << endl; // calculate discount                
        }                
        else 
            price = TICKET;
    }
    total += price;
}
cout << "Total of tickets sold = " << total << endl;
return 0;

can someone change my coding to mips

commented: That's what a C compiler for MIPS will do! +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.