How can I use gcc to compile x86 assembly code on an x64 computer - gcc

For a school assignment I have to write x86 assembly code, except I can't use gcc to compile it since my computer is an x64 machine, and gcc is only excpecting x86 code. Is there a command that'll make gcc accept it x86 assembly code? Thanks
P.S. I know my code is valid since it compiles just fine on x86 machines.

Just pass the -m32 option to gcc.

Related

C code to MIPS assembly using llvm

I'd like translate my c codes to mips assembly using llvm. How can i do it? I'm on Mac. So llc command does not work.
Thanks
The clang on your MacOS system won't compile for mips by default, you'll need to build your own.
You can look here: http://llvm.org/docs/GettingStarted.html for directions on building up llvm.
After that you can use clang to compile C to mips assembly by doing something like:
clang -target mipsel-linux-gnu foo.c -S -o -
which will compile the file "foo.c" to 32-bit mips assembly for the linux operating system and output it to the console.

Compiling assembly code for aarch64

I have generated an assembly file try.s with aarch64 instruction set.I want to compile this on an ARM8 (aarch64 processor) running ubuntu.
my native compiler is gcc(4.8) and i use the following command to compile
gcc -o try.o try.s
I am getting the following errors
Error : ARM register expected -- mov x10,x0
It seems like the aarch4 registers are not being recognized although i thought gcc 4.8 supported aarch64. Can someone tell me what am i missing or is there any special option i should include.Or suggest me a native compiler(not cross-compilers) for doing aarch64.I would also like to use gdb to debug this natively.
gcc is for a 32b targets. 'Xn' registers are not defined for a aarch32 instruction set. That's what compiler tells you.
Right toolchain is aarch64-elf-gcc.
PS: that's a good idea to make asm file extention .S (capital s)

MinGW64 cannot compile 32bit code

I've downloaded MinGW from this link x64-4.8.1-posix-sjlj-rev1 but when I try to build for x86 target I've lots of linkage errors... seems that only x64 lib are installed...
I've need to build for x86 and x64 platforms on windows... Have I to download both x64 and x86 or are some simpler ways?
Edit I'm using eclipse keplero as IDE
I've tryed to build myself a simple hello world program with g++ -m32 -std=c++11 test.cpp -o test32.exe and g++ -m64 -std=c++11 test.cpp -o test64.exe. And all is ok... So the problem was with eclipse... After a little a discovered that I need to use MYSY ( set in PATH ) and set -m32 also in the c++ linkage options...
Now all is fine.
I've also tryed to use NetBeans C++ as IDE... seems a gread IDE!!!
It is not multilib enabled. That's why you are not able to compile 32-bit(x86) program. You can get multilib enabled toolchain from following link:
For 64-bit machine: 64-Bit
For 32-bit machine: 32-Bit

Building 32bit binary for ARM on 64bit CentOS

It seems that gcc doesn't accept -m32 option for ARM target. I am not sure how gcc behaves on 64bit Linux, but does it automatically generate 32bit binaries if gcc is of ELF32 running on 64bit Linux?
If so, is there any workaround?
Thanks in advance.
You need to use a cross-compiler to compile for ARM from your host running either x86 or x86_64, the reason being your host and target are 2 totally independent architectures.
The cross compiler would usually be configured to output only a 32-bit or 64-bit binary for ARM (not both). Most ARM device applications make use of only 32-bit and so using an arm cross-compiler without any extra arguments would build 32-bit binaries.
Toolchains have other -m flags to specify machine type such as armv7, arm cortex a-8, etc. for further optimization. You need to look at the documentation of the ARM cross compiler.
As for getting the correct toolchain which works for your target and runs under CentOS, it is better to start at the website of the vendor of the target device.
The -m32 option provided by the x86_64 version of gcc makes gcc compile 32-bit binaries instead of 64-bit since the x86 instruction set and the x86_64 (AMD64 or Intel EMT64) are quite similar. Especially the fact that it allows executing 32-bit instructions in 64-bit mode quite easily.

C/C++ to MIPS Assembly

I know that to compile to assembly, I should use the -Soption with gcc or g++, but how do I get MIPS assembly?
I tried
g++ -march=mips2 dll.c
but that gives the error
dll.c:1:0: error: bad value (mips2) for -march= switch
I saw a suggestion of the compile command mips_gcc, but I can't find how to install that compiler.
I'm using Ubuntu 64-bit, if that helps.
You need a version of gcc that is built as a MIPS cross compiler. You can download the free Mentor/Codesourcery MIPS gnu/gcc cross compilation tool chain from here. This toolchain is available for both Windows and Linux.
After downloading, installing and adding the tool chain to your path you would say:
mips-linux-gnu-g++ -march=mips32r2 -S dll.c
to compile your code to MIPS32R2 assembly.
UPDATE 8/2017:
It looks like Sourcery CodeBench free cross compiler for MIPS is no longer available at Mentor's site.
Try the free toolchain at Imagination's site.

Resources