I want to convert two C++ programs into MIPS assembly. Both programs compute if a given integer is prime or not. They both contain the same main() pretty much, but one uses an iterative function while one uses a recursive function. I am not too good with MIPS so I wanted to know if anyone here could offer any help.

I want to use the MIPS command "syscall" for the C++ "cout" and "cin" statements. I also want to use the "rem" MIPS instruction for the C++ mod operation.

I will paste the two C++ programs below:

Iterative:

#include <iostream>



using namespace std;



bool isPrime(int n);



int main()

{

    int n;

	

	cout << "Please enter a positive integer to be tested: ";

	cin >> n;

	cout << "\nYou entered n = " << n << endl;

	

	if ( isPrime(n) ) 
		cout << "\nIterative isPrime retuned TRUE\n\n";
	else
		cout << "\nIterative isPrime retuned FALSE\n\n";	
	
	return ( EXIT_SUCCESS );

}



// Iterative Primality Tester Algorithm

// Input: Positive integer n >=2

// Output: true if n is prime
//         false if n is composite

bool isPrime(int n)

{

	int i;

	

	if ( n < 2 )
	{
		cout << "The number entered is less than 2!\n"; 
	    exit(EXIT_FAILURE);
	} 

	
	for (i = 2; i < n; i++)
		if ( ( n % i ) == 0 ) return false;

	return true;

}

And the recursive program:

#include <iostream>



using namespace std;



bool isPrime(int n);
bool searchFactor(int range, int n);



int main()

{

    int n;

	

	cout << "Please enter a positive integer to be tested: ";

	cin >> n;

	cout << "\nYou entered n = " << n << endl;

	

	if ( isPrime(n) ) 
		cout << "\nRecursive isPrime retuned TRUE\n\n";
	else
		cout << "\nRecursive isPrime retuned FALSE\n\n";
	
	return ( EXIT_SUCCESS );

}



// Recursive Primality Tester Algorithm

// Input: Positive integer n >=2

// Output: true if n is prime
//         false if n is composite

bool isPrime(int n)

{

	int i;

	

	if ( n < 2 )
	{
		cout << "The number entered is less than 2!\n"; 
	    exit(EXIT_FAILURE);
	} 

	
	return !searchFactor(2, n);;

}



// Recursive search for factors 

// Input: Positive integer n >=2
//        Positive integer range

// Output: true if exists an integer in [range, n-1] that is a factor of n (i.e., n is not prime)
//         false if there is no such integer
bool searchFactor(int range, int n)
{
	if ( range >= n ) return false;
	
	if ( ( n % range ) == 0 ) return true;
	else return searchFactor(range+1, n);
}

I appreciate it guys! :)

Since you missed both of these, then no.

Announcement: Please use BB Code and Inlinecode tags
Announcement: We only give homework help to those who show effort

Besides, get a C++ compiler and do it yourself.
g++ -S prog.cpp
will get you the asm you seek.

Besides, get a C++ compiler and do it yourself.
g++ -S prog.cpp
will get you the asm you seek.

The Assembly that is outputted from doing this is not MIPS. It's like very confusing to look at and work with and I'm not sure how it really relates to MIPS code at all.

I did attempt this already myself, but my MIPS did not work in SPIM, so I thought there was no reason to put it here.

Is there really no advice that anyone can give me?

Well yeah, you need a cross-compiler which runs on your machine AND outputs MIPS assembly.

Well yeah, you need a cross-compiler which runs on your machine AND outputs MIPS assembly.

Is there any place that has instructions for me to be able to set this up? I know running g++ on a MIPS platform would give me the code easy, but setting up a cross platform compiler seems very confusing without some sort of direction. Have you done this?

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.