0

I'm reading a book on how to write assembly for x86 Linux.

I want to assemble the following assembly source code (exit.s) on my x86_x64 Linux OS:

.section .data

.section .text
.globl _start
_start:
    mov1 $1, %eax
    mov1 $0, %ebx
    int $0x80

By calling this command: as exit.s -o exit.o

However, the assembler exits with the following error:

exit.s: Assembler messages:
exit.s: Warning: end of file not at end of a line; newline inserted
exit.s:6: Error: no such instruction: `mov1 $1,%eax'
exit.s:7: Error: no such instruction: `mov1 $0,%ebx'

From what I can gather, the instruction set for x86_x64 Linux is different from x86, hence the error. By replacing mov1 with mov, the assembler succeeds in compiling. However, seeing as the rest of the book is written for x86, I would like to be able to assemble x86 assembly for my OS.

I read somewhere that it's possible to do so by specifying the option --32, like so: as --32 exit.s -o exit.o, but by doing that, I receive the same error as before.

How do I assemble x86 source code on an x86_x64 architecture?

Mike Hawkins
  • 2,144
  • 5
  • 24
  • 42
  • 3
    `mov1` is not valid even for correct 32-bit x86 assembly. You probably want `movl` (last character is an `L`). But that does not solve your question, to use the int 80h API you do want to compile into a 32-bit application. – ecm Aug 28 '20 at 19:27
  • 2
    Try the `gcc` commands given in https://stackoverflow.com/questions/36861903/assembling-32-bit-binaries-on-a-64-bit-system-gnu-toolchain/36901649#36901649 – ecm Aug 28 '20 at 19:28
  • Thanks. Yeah, I mistook the l for a 1. Using the gcc compiler worked. – Mike Hawkins Aug 28 '20 at 19:35
  • 1
    By the way, as that answer lists: "Use `-v` to have gcc show you the commands it runs to assemble and link." – ecm Aug 28 '20 at 19:40

1 Answers1

2

There are two issues with this code:

 mov1 $0, %ebx

It should be movl, (See the difference between 1 and l, in some fonts they are the same.)

The other, a minor one is the missing newline, simply insert one at the end of the file.

To assemble the file use as --32 exit.s -o exit.o (Assuming a 64-bit assembler)

JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
  • I already tried the command you suggest. I'm not sure whether or not it fails because my assembler is not 64-bit (I run x86_x64). Is there any way to check whether or not my assembler is 64-bit? I had to use gcc to compile it to a 32-bit binary can my os could execute. – Mike Hawkins Aug 28 '20 at 19:36
  • 3
    @MikeHawkins What error message did you get with `as --32`? This should work just fine after you fix the `mov1` error. – fuz Aug 28 '20 at 20:46
  • My comment was not correct - what I meant was, the assembler succeeds, but the linker fails with the following message: `ld: i386 architecture of input file `exit.o' is incompatible with i386:x86-64 output` – Mike Hawkins Aug 29 '20 at 10:36
  • 1
    Does this work? `ld -m elf_i386 -o executable file.o` Has `ld` support for 32-bit output? – JCWasmx86 Aug 29 '20 at 10:38