I need to generate MIPS specific code on my machine when I run my C program. When I simply run,
gcc -O2 -S -c hello.c
On my system, I get the hello.s
which seems to generate some assembly code but it doesn't seem to be MIPS specific code. The contents of hello.s
file is as below.
.file "hello.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "Hello world"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB11:
.cfi_startproc
movl $.LC0, %edi
xorl %eax, %eax
jmp printf
.cfi_endproc
.LFE11:
.size main, .-main
.ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)"
.section .note.GNU-stack,"",@progbits
How can I generate the MIPS specific code on my machine?
My machine details are as below.
arch
x86_64
Best Answer
Understanding the Basics
From the wiki entry of MIPS architecture, it is described as,
From the wiki entry of the x86-64, it is described as,
So as per the
arch
output in the question, it is evident that I have a x86_64 machine and I try to produce the MIPS architecture specific code after runninggcc
compiler.This is similar to trying and running a diesel car on a petrol engine. No matter how hard we try, without tweaking the gas engine, we could not run the diesel car on a petrol engine.
To describe it in a technical manner,
gcc
can produce assembly code for a large number of architectures, include MIPS. But what architecture a givengcc
instance targets is decided whengcc
itself is compiled. The precompiled binary you will find in an Ubuntu system knows about x86 (possibly both 32-bit and 64-bit modes) but not MIPS.How to compile a C program to MIPS assembly code
Again quoting from the same answer, compiling
gcc
with a target architecture distinct from the architecture on whichgcc
itself will be running is known as preparing a cross-compilation toolchain. Or in layman's terms, this cross compilation toolchain is similar to tweaking the petrol engine to run the diesel car.However, setting up a cross-compilation toolchain is quite a bit of work, so rather than describe how to set that up, I will describe how to install a native MIPS compiler into a MIPS virtual machine. This involves the additional steps of setting up an emulator for the VM and installing an OS into that environment, but will allow you to use a pre-built native compiler rather than compiling a cross compiler.
We will be first installing
qemu
to make our system run some virtualized operating systems. Again there are several approaches like installing some cross compiled tool chain as discussed here and using a buildroot as suggested in the answer that I earlier linked.After downloading the tar ball, run the following commands.
qemu
on the machine, I tried several methods of netboot for the debian OS as suggested over here and here. But unfortunately I was not able to perform the debian OS installation using the netboot because the correct mirrors were not available.I got an image for debian which targets MIPS architecture from here and I downloaded the
kernel
andqemu
image and from the above link and performed the below steps.I started the
qemu
as below.After the debian system came up, I installed the
gcc
compiler as below.Now, I have a perfectly working native
gcc
compiler inside the MIPS debian virtual machine on qemu, which compiles my C program to MIPS specific assembly code.Testing
Inside my debian machine, I just put in a sample C hello world program and saved it as
hello.c
as below.To generate MIPS architecture code for my hello.c program, I ran the C program using the
gcc
compiler as,The above command generated a
hello.s
file which generated my MIPS architecture code.But how will I know if the above generated code is MIPS assembly code?
The
arch
command's output will tell the machine's architecture. In my debian machine, it produces the output asmips
and I also do not have anybinutils
or cross-compiler tool chains installed in the machine.So, the generated assembly code is MIPS specific.