title: Compiling 32-bit assembly on 64-bit Ubuntu on Windows
body: <p>EDIT: It's looking like the issue is that the Windows Subsystem for Linux can't deal with 32-bit. </p>
  
  <p>Assembly and linux newbie here trying to compile 32-bit assembly (AT&amp;T syntax) on 64-bit Ubuntu on Windows. My situation is practically an exact replica of <a href="https://stackoverflow.com/questions/18429901/compiling-32-bit-assembly-on-64bit-system-ubuntu">this other stackoverflow question</a> 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.</p>
  
  <p>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 <code>movl 8(%ebp), %eax</code> in my source file (I read that it's probably due to setting up a 32-bit stack in a 64-bit environment).</p>
  
  <p>Without <code>.32code</code> as the first line of my program and with a <code>--32</code> option for <code>as</code>, no attempts with <code>ld</code> produce anything helpful: <a href="https://i.stack.imgur.com/GL3HC.png" rel="nofollow noreferrer">without .32code and with --32</a> (can't embed images in my posts yet so putting link instead).</p>
  
  <p>Still without <code>.32code</code> and now with <code>--x32</code> (<a href="https://i.imgur.com/9aSgq1P.png" rel="nofollow noreferrer">without .32code and with --x32</a>) I found out it has trouble with push, pop, etc. if you don't include the <code>.32code</code> so here I include it in my source file: <a href="https://i.imgur.com/HTaWGTi.png" rel="nofollow noreferrer">with .32code and with --x32</a>.</p>
  
  <p>Then, there are similar attempts I can make with <code>gcc</code> instead of <code>as</code> and <code>ld</code> 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 <code>gcc -m32 -nostdlib factorial.s -o factorial</code> I get the "cannot execute the binary file: Exec format error" message, with or without the <code>.32code</code>. Trying things such as <code>sudo apt-get install lib32gcc-4.8-dev</code> and other attempts, nothing seems to work and a lot of times I get issues with <code>qemu-user-static</code> maybe because I at one point installed it and now it's all f%^#!d. </p>
  
  <p>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 <code>4(%esp)</code> would become <code>8(%esp)</code>) but also maybe change some <code>movl</code> to <code>movq</code>, for example, or some <code>%eax</code> to <code>%rax</code>, etc. But I feel there should be some solution to this, especially <a href="https://stackoverflow.com/questions/18429901/compiling-32-bit-assembly-on-64bit-system-ubuntu">how the poster in 2013</a> seemed to have the exact issue solved. Maybe I need to try again with stuff like <code>-march=CPU[+EXTENSION...]</code> or <code>-mtune=CPU</code>? Maybe I'm just lacking basic knowledge on architectures, or linux, or as/gcc?</p>