title: Making/opening/writing files in nasm assembly mac x86_64, 64 bit not working
body: <p>I am new to assembly and I wanted to get into writing files. I could not find anything for my conditions (64 bit on mac using nasm). Here is my code so far. Its not doing anything.</p>
  <pre><code>global start
  ; default rel
  section .text
      start:
          mov rax, 0x2000005
          mov rdi, 2     
          mov rsi, file
          mov rdx, lenfile
          syscall
  
          mov rax, 0x2000001       ;Exiting
          xor rdi, rdi         
          syscall          
  
  section .data
      
      file db &quot;test.txt&quot;
      lenfile equ $ - file
  </code></pre>
  <p>What can I change or add to make a file and write hello world on it?</p>
  <p>EDIT 1</p>
  <p>I know this will not work but this is what I have so far. I don't know how to write to the file. The file is not being created either.</p>
  <pre><code>global start
  ; default rel
  section .text
      start:
          ;open
          mov rax, 0x2000005
          mov rdi, 2     
          mov rsi, file
          mov rdx, lenfile
          syscall
          ;write
          mov rax, 0x2000004
          mov rdi, 1     
          mov rsi, str
          mov rdx, strlen
          syscall
          ;close
          mov rax, 0x2000006
          mov rdi, 3     
          mov rsi, file
          ; mov rdx, lenfile
          syscall
  
          mov rax, 0x2000001       ;Exiting
          xor rdi, rdi         
          syscall          
  
  section .data
      
      str: db &quot;Hello world&quot;, 0
      strlen: equ $ - str
      file: db &quot;test.txt&quot;
      lenfile: equ $ - file
      
  </code></pre>
  <p>Edit 2</p>
  <p>I think that I have properly opened it but now I need to properly close it. How would I do that. Forget the writing part for now. Here is what I have so far.</p>
  <pre><code>global start
  ; default rel
  section .text
      start:
          ;open
          mov rax, 0x2000005
          mov rdi, file     
          mov rsi, 1   
          syscall
          
          ;close
          mov rax, 0x2000006
          mov rdi, file     
          mov rsi, 1
          
          syscall
  
          mov rax, 0x2000001       ;Exiting
          xor rdi, rdi         
          syscall          
  
  section .data
      
      str: db &quot;Hello world&quot;, 0
      strlen: equ $ - str
      file: db &quot;test.txt&quot;
      lenfile: equ $ - file
  </code></pre>
  <p>While I was writing edit 2, I looked at the c version on the closing part. It's just a simple function. <code>fclose(file);</code> How could I do that in assembly?</p>