Need help converting C++ code to MIPS Assembly

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 33
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

Need help converting C++ code to MIPS Assembly

 
0
  #1
Oct 22nd, 2008
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:
  1. #include <iostream>
  2.  
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. bool isPrime(int n);
  10.  
  11.  
  12.  
  13. int main()
  14.  
  15. {
  16.  
  17. int n;
  18.  
  19.  
  20.  
  21. cout << "Please enter a positive integer to be tested: ";
  22.  
  23. cin >> n;
  24.  
  25. cout << "\nYou entered n = " << n << endl;
  26.  
  27.  
  28.  
  29. if ( isPrime(n) )
  30. cout << "\nIterative isPrime retuned TRUE\n\n";
  31. else
  32. cout << "\nIterative isPrime retuned FALSE\n\n";
  33.  
  34. return ( EXIT_SUCCESS );
  35.  
  36. }
  37.  
  38.  
  39.  
  40. // Iterative Primality Tester Algorithm
  41.  
  42. // Input: Positive integer n >=2
  43.  
  44. // Output: true if n is prime
  45. // false if n is composite
  46.  
  47. bool isPrime(int n)
  48.  
  49. {
  50.  
  51. int i;
  52.  
  53.  
  54.  
  55. if ( n < 2 )
  56. {
  57. cout << "The number entered is less than 2!\n";
  58. exit(EXIT_FAILURE);
  59. }
  60.  
  61.  
  62. for (i = 2; i < n; i++)
  63. if ( ( n % i ) == 0 ) return false;
  64.  
  65. return true;
  66.  
  67. }


And the recursive program:
  1. #include <iostream>
  2.  
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. bool isPrime(int n);
  10. bool searchFactor(int range, int n);
  11.  
  12.  
  13.  
  14. int main()
  15.  
  16. {
  17.  
  18. int n;
  19.  
  20.  
  21.  
  22. cout << "Please enter a positive integer to be tested: ";
  23.  
  24. cin >> n;
  25.  
  26. cout << "\nYou entered n = " << n << endl;
  27.  
  28.  
  29.  
  30. if ( isPrime(n) )
  31. cout << "\nRecursive isPrime retuned TRUE\n\n";
  32. else
  33. cout << "\nRecursive isPrime retuned FALSE\n\n";
  34.  
  35. return ( EXIT_SUCCESS );
  36.  
  37. }
  38.  
  39.  
  40.  
  41. // Recursive Primality Tester Algorithm
  42.  
  43. // Input: Positive integer n >=2
  44.  
  45. // Output: true if n is prime
  46. // false if n is composite
  47.  
  48. bool isPrime(int n)
  49.  
  50. {
  51.  
  52. int i;
  53.  
  54.  
  55.  
  56. if ( n < 2 )
  57. {
  58. cout << "The number entered is less than 2!\n";
  59. exit(EXIT_FAILURE);
  60. }
  61.  
  62.  
  63. return !searchFactor(2, n);;
  64.  
  65. }
  66.  
  67.  
  68.  
  69. // Recursive search for factors
  70.  
  71. // Input: Positive integer n >=2
  72. // Positive integer range
  73.  
  74. // Output: true if exists an integer in [range, n-1] that is a factor of n (i.e., n is not prime)
  75. // false if there is no such integer
  76. bool searchFactor(int range, int n)
  77. {
  78. if ( range >= n ) return false;
  79.  
  80. if ( ( n % range ) == 0 ) return true;
  81. else return searchFactor(range+1, n);
  82. }
I appreciate it guys!
Last edited by Ancient Dragon; Oct 23rd, 2008 at 7:01 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Need help converting C++ code to MIPS Assembly

 
0
  #2
Oct 23rd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

Re: Need help converting C++ code to MIPS Assembly

 
0
  #3
Oct 23rd, 2008
Originally Posted by Salem View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Need help converting C++ code to MIPS Assembly

 
0
  #4
Oct 23rd, 2008
Well yeah, you need a cross-compiler which runs on your machine AND outputs MIPS assembly.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 33
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

Re: Need help converting C++ code to MIPS Assembly

 
0
  #5
Oct 23rd, 2008
Originally Posted by Salem View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Need help converting C++ code to MIPS Assembly

 
0
  #6
Oct 23rd, 2008
There's plenty of info
http://clusty.com/search?query=gcc+c...Mozilla-search

And no, it's not something I've ever done. I use cross-compilers all the time, but never actually installed a GCC cross compiler from scratch as it were.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC