EDIT: It's looking like the issue is that the Windows Subsystem for Linux can't deal with 32-bit.
Assembly and linux newbie here trying to compile 32-bit assembly (AT&T syntax) on 64-bit Ubuntu on Windows. My situation is practically an exact replica of this other stackoverflow question but the solutions there (which I see repeated in many other posts) does not work for me, for some reason. I am learning x86 assembly with the book Programming from the Ground Up and I can't get the programs to compile on Bash on Ubuntu on Windows.
I've tried multiple combinations of options in my commands, some manage to apparently compile, but I either can't execute the binary file (format error) or I can but I get a segmentation fault when I get to movl 8(%ebp), %eax
in my source file (I read that it's probably due to setting up a 32-bit stack in a 64-bit environment).
Without .32code
as the first line of my program and with a --32
option for as
, no attempts with ld
produce anything helpful: without .32code and with --32 (can't embed images in my posts yet so putting link instead).
Still without .32code
and now with --x32
(without .32code and with --x32) I found out it has trouble with push, pop, etc. if you don't include the .32code
so here I include it in my source file: with .32code and with --x32.
Then, there are similar attempts I can make with gcc
instead of as
and ld
but I'm not sure if it makes sense to include all the different combinations of failures and open up that can of worms because then we get into whether gcc has the libraries it needs (?). For example, with gcc -m32 -nostdlib factorial.s -o factorial
I get the "cannot execute the binary file: Exec format error" message, with or without the .32code
. Trying things such as sudo apt-get install lib32gcc-4.8-dev
and other attempts, nothing seems to work and a lot of times I get issues with qemu-user-static
maybe because I at one point installed it and now it's all f%^#!d.
One answer recommended just installing 32-bit gcc but that didn't seem to do anything for me. Another solution could be to manually convert all the examples in the book to 64-bit and forget about all of this. I imagine that would imply re-envisioning the stack to contain of 8 instead of 4 (ie. things like 4(%esp)
would become 8(%esp)
) but also maybe change some movl
to movq
, for example, or some %eax
to %rax
, etc. But I feel there should be some solution to this, especially how the poster in 2013 seemed to have the exact issue solved. Maybe I need to try again with stuff like -march=CPU[+EXTENSION...]
or -mtune=CPU
? Maybe I'm just lacking basic knowledge on architectures, or linux, or as/gcc?