Search code examples
linuxassemblyx86fasm

FASM gives me 'error: illegal instruction.' during assembly


I just started coding in assembly. I have downloaded the flat assembler and copied code from the internet. When I run this code, however, it says something like:

section .text 
error: illegal instruction.

My question is: what is wrong with this code?

section .text
   global_start     ;must be declared for linker (ld)

_start:             ;tells linker entry point
   mov  edx,len     ;message length
   mov  ecx,msg     ;message to write
   mov  ebx,1       ;file descriptor (stdout)
   mov  eax,4       ;system call number (sys_write)
   int  0x80        ;call kernel

   mov  eax,1       ;system call number (sys_exit)
   int  0x80        ;call kernel

section .data
msg db 'Hello, world!', 0xa  ;our dear string
len equ $ - msg              ;length of our dear string

Can somebody figure out what is going wrong?


Solution

  • The problem is that you are using FASM, but the code you got from the internet is for NASM . If you were to install NASM into your Linux distro your code should work if you use NASM and you fix the bug at this line:

    global_start     ;must be declared for linker (ld)
    

    which should be:

    global _start     ;must be declared for linker (ld)
    

    The global directive needs a space before the label _start

    If you wish to use FASM I recommend finding some examples and tutorial specific to that assembler. I would recommend NASM or GNU assembler (gas) if you are going to do any significant development in assembly.