| | |
Need help converting C++ code to MIPS Assembly
Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 0
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:
And the recursive program:
I appreciate it guys!
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:
Assembly Syntax (Toggle Plain Text)
#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:
Assembly Syntax (Toggle Plain Text)
#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); }
Last edited by Ancient Dragon; Oct 23rd, 2008 at 7:01 am. Reason: add code tags
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 0
•
•
•
•
Besides, get a C++ compiler and do it yourself.
g++ -S prog.cpp
will get you the asm you seek.
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?
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 0
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?
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.
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.
![]() |
Similar Threads
- Conversion from C++ to MIPS Assembly (Assembly)
- Converting Decimal Value to Binary (MIPS) (Assembly)
- MIPS Help (Assembly)
- Converting C to MIPS(SPIM) (Assembly)
Other Threads in the Assembly Forum
- Previous Thread: GNU as Question
- Next Thread: Anyone use visual studio to compile assembly language?
| Thread Tools | Search this Thread |






