title: How do I open and read from a file in x86_64 GAS Assembly in Ubuntu?
body: <p>I'm trying to replicate a lower-to-upper string program, which will open a file, run the code, and print it on screen. but i want to try and open a test file first - without the hassle of trying to read what the user typed. </p>
  
  <p>I don't understand what I'm doing wrong, %rax's value is 3 after the first system call, which I understand is an error. </p>
  
  <pre><code>    .data
  
  filename:
      .asciz "test.txt"
  
  inputInFile:
      .string ""
  
  .text
  .global _start
  
  _start:
  
  #TO-DO: ASK FOR FILENAME
  
  #open file syscall
  mov $2,%rax
  mov $filename,%rdi
  mov $0,%rsi
  mov $0666,%rdx
  syscall
  
  
  #TO-DO: ERROR FOR FILE NOT FOUND
  
  
  .Loop:
  #reading from file syscall, into buffer
  mov $0, %rax
  mov $0, %rdi
  mov $inputInFile, %rsi
  mov $500,%rdx
  syscall
  #input is in rax
  
  #in case of end of file,  close file
  cmp $0,%rax
  je .exit
  
  #append input into buffer
  mov %rax,%rdx
  mov $1,%rax
  mov $1,%rdi
  mov $inputInFile,%rsi
  syscall
  
  jmp .Loop
  
  #close file syscall
  mov $3,%rax
  mov $1,%rdi
  syscall
  
  
  .exit:
  #close file
  mov $3,%rax
  mov $1,%rdi
  syscall
  
  #quit
  mov $60,%rax
  mov $0,%rdi
  syscall
  </code></pre>