Saturn
Saturn

Reputation: 18149

How can I compile a 32-bit .o file with gcc in my 64-bit machine?

Trying to learn NASM Assembly. I have a 64-bit machine, with Ubuntu. Recently I decided to test the push and pop instructions. I do this:

nasm -felf64 Test.asm

Apparently they are not supported in 64-bit mode. Alright, no problem, I'll just do it for 32 then:

nasm -felf Test.asm

And now, as always,

gcc Test.o

But it now tells me

i386 architecture of input file 'Test.o' is incompatible with i386:x86-64 output

I don't quite grasp the error here. How can I test push and pop in my 64-bit machine, if apparently I can't compile 32-bit programs?

Upvotes: 3

Views: 3841

Answers (3)

Frank Kotler
Frank Kotler

Reputation: 3119

First, you can use push and pop in 64-bit code, just not with 32-bit registers. If you push and pop 64-bit registers, it'll work fine. In most cases, you can use 32-bit registers in 64-bit code, just not push and pop. There may be other exceptions, but I'm not aware of 'em.

64-bit code uses different system call numbers, puts the parameters in different registers, and uses syscall instead of int 0x80. However, the old int 0x80 interface with the old system call numbers and parameters in the old registers still works. This gives you kind of "mixed" code and may not be a Good Idea, but it works. How long it will continue to work in future kernels is anybody's guess. You may be better off to learn "proper" 64-bit code.

But there are (still!) a lot more 32-bit examples out there. You can tell Nasm -f elf32 (just -f elf is an alias, but I'd use the "full name" just for clarity). If you're using gcc, tell it -m32. If you're using ld directly, tell it -m elf_i386. You do have choices, but they have to be compatible with each other.

Upvotes: 2

suma
suma

Reputation: 46

How about "-m32"? And I think you need to care dependent library(e.g libc), see: Use 32bit shared library from 64bit application?

Upvotes: 3

guozhu cheng
guozhu cheng

Reputation: 151

how about the "-march=i386" ? see: http://gcc.gnu.org/onlinedocs/gcc/i386-and-x86_002d64-Options.html

Upvotes: 0

Related Questions