0

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?

Michael Petch
  • 46,082
  • 8
  • 107
  • 198

4 Answers4

3

There is a space missing on the 2nd line

section .text
    global _start

see here

user23573
  • 2,479
  • 1
  • 17
  • 36
3

write section '.text' with quote marks and there is no global afaik, use public _start instead. FASM could build executable ELFs for you with format ELF64 executable as the very first line. Now you can use segment executable and entry _start if you do not want to link it with other object-files.

sivizius
  • 450
  • 2
  • 14
1

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.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
0

I recommend using

section '.text'

For example, You can use the code this way:

section '.text' code readable executable

OR

section '.idata'

Be careful to use '' when defining after section.

Mr. Weirdo
  • 115
  • 1
  • 9