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;
}
3
Contributors
3
Replies
56
Views
5 Years
Discussion Span
Last Post by Farah_3
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?

0
#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;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.