Question

I have a 64 bit Ubuntu Operating System and I have been learning 32 bit Assembly. I am trying to compile these two files:

square.s:

#square.s

.section .text
.globl sqr
.type sqr, @function
sqr:
    pushl %ebp
    movl %esp, %ebp
    movl 8(%ebp), %eax
    imull %eax, %eax
    popl %ebp
    ret

main.c :

//main.c
#include <stdio.h>
extern long sqr(long);
int main(int argc, char* argv[])
{
    long squared = sqr(10);
    printf("%lu\n", squared);
    return 0;
}

On my 32bit Virtual Machine I compiled them with this command

  gcc main.c square.s -o test

and it worked. The problem I am having is that I would like to compile these files on my 64bit machine. I have tried several ways of compiling these files but none have worked. Can anyone point me in the right direction? Is there an option to do this? I have tried -m32 but that didn't work.

When I do this:

  gcc -m32 -o test main.c square.s

I get this:

  In file included from /usr/include/stdio.h:28:0,
             from main.c:1:
/usr/include/features.h:323:26: fatal error: bits/predefs.h: No such file or directory
compilation terminated.
Was it helpful?

Solution

Compiling / Linking 32-bit program on 64 bit ubuntu need gcc-multilib, try:

sudo apt-get install gcc-multilib libc6-i386 lib6-dev-i386

However, this may have other problem when you try to link other libraries.

You would have better luck using a 32-bit chroot environment (i.e. running a 32-bit root on your 64-bit ubuntu).

OTHER TIPS

It seems like your problem is related to 32-bit compilation in general, regardless of the assembly code. Something is probably misconfigured.

Also, did you consider using inline assembly instead of a .s file? It's much easier to integrate C and assembly this way, and you don't have to worry about calling convention details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top