Solved

Convert from C to MIPS

Posted on 2010-11-09
14
1,416 Views
Last Modified: 2013-11-17
What is the MIPS conversion of this C code?

#include<stdio.h>
#include<math.h>
void hanoi(int x, char from,char to,char aux)
{
 
if(x==1)
{
printf("Move Disk From %c to %c\n",from,to);
}
else
{
hanoi(x-1,from,aux,to);
printf("Move Disk From %c to %c\n",from,to);
hanoi(x-1,aux,to,from);
}
 
}
int main()
{
 int disk;
 int moves;
 printf("Enter the number of disks you want to play with:");
 scanf("%d",&disk);
 moves=pow(2,disk)-1;
 printf("\nThe No of moves required is=%d \n",moves);
 hanoi(disk,'A','C','B');
return 0;
 }

Open in new window

0
Comment
Question by:jralexander137
14 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 34099678
I assume that this question is superseded by your other question (http://www.experts-exchange.com/Programming/Languages/C/Q_26604107.html), so I'll ask that this one gets deleted (since duplicate questions aren't allowed).
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34099681
(or better yet, you can delete it yourself)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34099823
Since it seems you decided to delete the other question instead, I'll re-post my response here :
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 500 total points
ID: 34099824
You'll need a MIPS cross compiler, and then you can use the -S command line option for gcc to generate the assembly code.

I've never done cross compilation for MIPS under Cygwin, so I can't give you specific instructions, but I'm sure that you can find some via Google.

The -S command line option is as simple to use as :

        gcc -S code.c
0
 
LVL 9

Expert Comment

by:masheik
ID: 34101249
Hi,

    The below gcc mips doc contains list of options that you can use with ,

 http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/MIPS-Options.html

    gcc -S --march=mips1 Example.c
0
 

Author Comment

by:jralexander137
ID: 34102874
I have no idea how to set up the cross compiler for MIPS...I've been trying for several hours now with no luck. Any chance you could just help me manually convert it unless you know how to get this cross compiler set up?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 34103291
Setting up a cross compiler is not a trivial thing, but it's not very complicated either.

A straightforward way, is to build gcc from source (after configuring it to generate MIPS code). Depending on your needs, you might also need to build a cross-assembler and cross-linker (as well as probably some runtime libraries).

As I said, I didn't do any cross compiling with Cygwin yet, so I can't help you with specifics.

Here's some short overviews of how to build a cross-compiler with gcc :

        http://gcc.gnu.org/wiki/Building_Cross_Toolchains_with_gcc
        http://wiki.osdev.org/GCC_Cross-Compiler

Also have a look at crosstool, which will probably make life a little easier for you :

        http://kegel.com/crosstool/
0
 

Author Comment

by:jralexander137
ID: 34105742
Well I'm in the process of trying to install a cross compiler but in the mean time any links/information on possibly converting each line to MIPs by hand?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 34106681
That's just a matter of writing the code in MIPS assembly from scratch (following the same outline as in the C code). How good are you with MIPS assembly ?
0
 

Author Comment

by:jralexander137
ID: 34107209
Not good at all to be honest....I have little to no idea on even where to begin. I do know how to read MIPs relatively well but writing based on C code I have pretty much no idea.
0
 

Author Comment

by:jralexander137
ID: 34107647
If you know of any websites or something that would help me that would be great.
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 500 total points
ID: 34109581
You will probably have a big help with SPIM (a simulator/debugger for MIPS) :

        http://pages.cs.wisc.edu/~larus/spim.html

Note that that page also contains links to further information that will probably be useful :

        http://pages.cs.wisc.edu/~larus/spim.html#information

There are also plenty of books and tutorials to be found on writing assembly for MIPS.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 34824211
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question