PDA

View Full Version : C/C++ to MIPS assembly



rbprogrammer
October 23rd, 2007, 03:47 AM
does any one know of a program that will convert C/C++ to MIPS assembly??

i know i can do a gcc or a g++ like:

$gcc -S somefile.c
and it will take the C file and convert it to assembly.. but that assembly is the x86 assembly (or whatever it is called).

is there a way to make gcc or g++ make a MIPS assembly file??

i only ask because of a course here that i am taking that makes us look at MIPS assembly and C code. it will also be helpful to see "efficient" code. since i'm still new at the assembly programming, it is still hard to see how to make code efficient since assembly is soooooo much different than any other language that i know.

LaRoza
October 23rd, 2007, 03:52 AM
I don't think so, you can't even get NASM out of it, but the assembly languages can't be that different. What syntax is the assembly you are learning?

rbprogrammer
October 23rd, 2007, 04:07 AM
ok, when you type out this simple C file: (named test.c)


int main()
{
register int i = 5;
register int j = 15;
register int k = i + j;
return 0;
}

then do

$ gcc -S test.c
you get:


.file "test.cpp"
.text
.align 2
.globl main
.type main, @function
main:
.LFB2:
leal 4(%esp), %ecx
.LCFI0:
andl $-16, %esp
pushl -4(%ecx)
.LCFI1:
pushl %ebp
.LCFI2:
movl %esp, %ebp
.LCFI3:
pushl %ecx
.LCFI4:
movl $0, %eax
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.LFE2:
.size main, .-main
.globl __gxx_personality_v0
.ident "GCC: (GNU) 4.1.2 (Ubuntu 4.1.2-0ubuntu4)"
.section .note.GNU-stack,"",@progbits

now i'm not so sure exactly what any of that means, but in MIPS, the equivalent to the simple addition statement would be:


register $t0 contains the i variable
register $t1 contains the j variable
register $t2 contains the k variable



add $t2, $t0, $t1

now, i dont know where that step is taking place in the generated assembly code, because i was thinking about just learning a little bit about the x86 assembly then convert it to the MIPS, and view the "efficient" code that way.

LaRoza
October 23rd, 2007, 04:13 AM
ok, when you type out this simple C file: (named test.c)


int main()
{
register int i = 5;
register int j = 15;
register int k = i + j;
return 0;
}





add $t2, $t0, $t1



These are not the same. In the C version, you have:

A function that starts with the program, and returns an integer, four variables, that **might** be in a register. gcc takes that as a hint, but doesn't have to do it. Also, that operation of adding two integers, and storing them in a variable is a little more complicated in assembly.

There are also other considerations with C, like command line arguments are passed to by the OS.

This is why assembly is good to know, so you can understand the "magic" C and other HLL's.

rbprogrammer
October 23rd, 2007, 04:30 AM
...
A function that starts with the program, and returns an integer, four variables, that **might** be in a register. gcc takes that as a hint, but doesn't have to do it.
...

well, i tried to make them a register so that i can more easily see how to write an assembly program. since we use registers a lot (which is probably a necessity for any assembly language) i thought it would be easer to see how the x86 uses registers.

also, i found that on http://gcc.gnu.org/ they make a MIPS compiler, so would i be able to install something that would allow me to use my compiler now to make a MIPS file?? like a patch or something??

ankursethi
October 23rd, 2007, 04:57 AM
You might want to build yourself a cross compiler, ie, a compiler that produces binaries for another architecture. Now I don't have experience with doing this, so you might want to look around the web a bit. Wikipedia is a good starting point. The GCC mailing lists/IRC channel might be helpful.

mjwood0
October 23rd, 2007, 11:32 AM
This is absolutely correct.

It's not an easy process, but I've successfully created a cross compiler which would target PowerPC from an Intel machine.

Do a google for GCC Cross Compiler and see what you come up with.

psychicist
October 23rd, 2007, 04:00 PM
You can create a cross compiler, which will take some time and effort. If you have a powerful machine a much easier way to get a complete MIPS machine is QEMU. You can find the directions to get a MIPS machine setup at http://www.aurel32.net/info/debian_mips_qemu.php.