title: Basic nasm shellcode crashes
body: <p>I'm learning assembly, so I'm working on shellcode. I wrote a simple "hello world" program in nasm, but it crashes when run.</p>
  
  <pre><code>; write.asm
  [SECTION .text]
  
  global _start
  
  _start:
      jmp short message;
  
  write:          ; takes two arguments pushed onto stack -&gt; text, textlen
      pop edx     ; pop length into edx
      pop ecx     ; pop ptr to text into ecx
      pushad
      mov al, 4
      mov bl, 1
      int 80h    ; syscall
      popad
      ret
  
  exit:           ; push exit_code onto stack
      mov al, 1
      pop ebx     ; pop exit_code into ebx
      int 80h
  
  main:
      pop eax     ; pop ptr to message into eax
      push 7      ; length of string
      push eax    ; push ptr to message
      call write
      xor ebx, ebx; zero out ebx
      push ebx
      call exit
  message:
      call main
      db 'Hello!', 10
  </code></pre>
  
  <p>I compile it with:</p>
  
  <pre><code>nasm -f elf write.asm
  ld -m elf_i386 -o write write.o
  </code></pre>
  
  <p>and get this:</p>
  
  <pre><code>Segmentation fault (core dumped)
  </code></pre>
  
  <p>I tried debugging it with gdb, but it didn't help.</p>