Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 941
fadedbee Avatar asked Oct 28 '11 09:10

fadedbee


People also ask

Is GCC 32 or 64 bit?

Mostly compiler(gcc or clang) of C and C++, nowadays come with default 64-bit version.

What is 32bit compiler?

the 32bit compiler can compile into 32bit machine code which can be run only on 32bit and 64bit microprocessor. But not less than 32 bit. Follow this answer to receive notifications.

What is GCC m32?

The -m32 flag tells GCC to compile in 32-bit mode.


1 Answers

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

like image 99
Gunther Piez Avatar answered Sep 23 '22 05:09

Gunther Piez