fadedbee
fadedbee

Reputation: 44765

How can I use Gas ('as') to assemble a 32 bit binary on a 64 bit linux?

How can I use Gas ('as') to assemble source to a 32 bit binary on 64 bit Linux?

This is for the purpose of following 32 bit tutorials without the hassle of having to change all the pointers and lots of instructions to quad words.

Thanks,

Chris.

P.S. I can easily do this in C...

chris@chris-linux-desktop:~$ cat test.c
#include "stdio.h"

int main() {
    printf("hello world");
    return 0;
}

chris@chris-linux-desktop:~$ gcc test.c -o test64
chris@chris-linux-desktop:~$ gcc -m32 test.c -o test32
chris@chris-linux-desktop:~$ file test32
test32: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped
chris@chris-linux-desktop:~$ file test64
test64: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.15, not stripped

Upvotes: 5

Views: 5017

Answers (2)

jim
jim

Reputation: 196

You might also need to link files using the linker -m option to set emulation for different target architectures. ld --help gives list of possible emulation values.

ld -m elf_i386 -o file file.o file2.o ...etc

Upvotes: 2

Gunther Piez
Gunther Piez

Reputation: 30449

Use as with the option "--32", like

as --32 source.s -o objectfile

Or you can just use gcc to assemble and link an assemler source file. gcc recognizes it by the ending.

gcc -m32 source.s -o executable

Upvotes: 6

Related Questions