title: What values should be set to registers when calling pthread_create in assembly code?
body: <p>For learning purposes, I am trying to create a toy assembly program that runs a function in a separate thread. My machine has the x86-64 architecture and runs the Ubuntu OS. I use the <code>pthread</code> library for this purpose:</p>
  <pre><code>extern pthread_create
  extern pthread_join
  
  section .data
      ; a separate thread will print the &quot;Hello&quot; message
      text: db &quot;Hello&quot;, 10
      text_len: equ $-text
      thread_id: dq 0
  
  section .text
  global main
  main:
      mov eax, 0
      mov ecx, 0
      mov edx, thread ; the function address to be executed in a thread
      
      ; With the &quot;mov esi, 0&quot; instruction, the call to pthread_create
      ; throws a segfault. Without this instruction, the call to
      ; pthread_create returns, but produces no observable effect.
      mov esi, 0
  
      mov rdi, thread_id
      call pthread_create
  
      mov eax, 0
      mov rsi, 0
      mov rdi, [thread_id]
      ; this call should block until the thread terminates, shouldn't it?
      call pthread_join
      ret
  
  thread:
      ; printing &quot;Hello&quot; to the screen
      mov eax, 4
      mov ebx, 1
      mov ecx, text
      mov edx, text_len
      int 80h
      ret
  </code></pre>
  <p>I compile the executable <code>a.out</code> file with the following commands:</p>
  <pre><code>nasm -f elf64 code.s
  gcc -no-pie code.o -lpthread
  </code></pre>
  <p>The executable fails to run properly, with the errors indicated in the code.
  I suspect the failure reason is the wrong set up of the register arguments before calling the <code>pthread_create</code>.
  <strong>How to set up the arguments correctly?</strong></p>
  <p>UPDATE:</p>
  <p>I changed the <code>pthread_create</code> arguments to</p>
  <pre><code>mov eax, 0
  mov ecx, 0
  mov edx, thread_0
  mov rdi, thread0_id
  </code></pre>
  <p>and the <code>pthread_join</code> arguments to</p>
  <pre><code>mov eax, 0
  mov ecx, 0
  mov rdi, thread0_id
  </code></pre>
  <p>Now the code throws a <code>bus error</code>. Could it be that the <code>pthread_t</code> does not have a permission to print the message?</p>