0

I'm trying to use the open syscall in Linux, from the manual page it takes 3 arguments, the pathname, flags, and the file mode. I tried to implement it with assembly x86_64 (Linux), but it doesn't create the file. By the way, where does the return value of this syscall is stored? The return value of open() is the file descriptor of the file, which then I want to write into.

section .text
    global _start

section .data
    pathname db "/home/user/Desktop/myfile.txt"

_start:
    mov rax, 2 ; syscall open
    mov rbx, pathname ; absolute path
    mov rcx, "O_CREAT" ; flag (create)
    mov rdx, "w" ; write (create file if not exists)
    syscall
    
    mov rax, 60
    mov rbx, 0
    syscall

The duplicate seems not to work for me, it doesn't create the file. And my path is valid.

section .text
    global _start

section .data
    pathname db "/home/user/Desktop/myfile.txt"

_start:
    mov rax, 2
    mov rdi, pathname
    mov rsi, 0x441   ; O_CREAT| O _WRONLY | O_APPEND
    syscall
    
    mov r8, rax      ; save the file handle
    
    mov rax, 60 ; exit syscall
    mov rbx, 0 ; 0 successful
    syscall
Dudu Faruk
  • 43
  • 1
  • 5
  • 1
    `O_CREAT` is not a string. It's a macro that expands to a number. – Barmar Sep 11 '20 at 20:15
  • And `mode` is also a number containing the permission modes of the file, not a string like `w` – Barmar Sep 11 '20 at 20:16
  • It seems like you didn't actually read the documentation of `open()`, it shows the datatypes. – Barmar Sep 11 '20 at 20:17
  • The result is in `eax`. – Barmar Sep 11 '20 at 20:19
  • https://www.tutorialspoint.com/assembly_programming/assembly_system_calls.htm – Barmar Sep 11 '20 at 20:20
  • @Barmar Thanks, I didn't know that. By the way, the duplicate's answer uses `rax`, `rdi` and `rsi` as the registers for the syscall, I am using `rax`, `rbx` and `rcx`, is this valid? – Dudu Faruk Sep 11 '20 at 20:25
  • I don't really know assembly programming on Linux. But I have no reason to believe that the duplicate is wrong. – Barmar Sep 11 '20 at 20:26
  • @DuduFaruk: ebx, ecx, edx, etc. are the arg-passing registers for the 32-bit `int 0x80` ABI. So yeah, it looks like you got that wrong, too. Perhaps you were trying to port a 32-bit tutorial to 64-bit mode, even though you don't know assembly? Related: [What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/q/46087730) (But you're not even doing that, you're still using `syscall` so you just have the args in the wrong registers) – Peter Cordes Sep 11 '20 at 20:28
  • @Barmar The duplicate does not work for me, see the edit. You should ask if this helped me before closing my question – Dudu Faruk Sep 11 '20 at 20:28
  • @PeterCordes I'm using and compiling with 64bit, I don't see anything wrong here. AFAIK `rax`.. are used with 64bit asm – Dudu Faruk Sep 11 '20 at 20:31
  • @DuduFaruk: I found that duplicate for you, not Barmar. Your code is in the `.data` section. Put it in `.text`, data in .data. Also, don't forget to 0-terminate your path string. Also, don't forget to pass a `mode` arg for the permissions of the newly-created file, like 0q666. – Peter Cordes Sep 11 '20 at 20:31
  • 1
    @DuduFaruk: Yes, RAX for the call number, with args in RDI, RSI, RDX, R10, R8, R9, as per the x86-64 System V ABI docs. [What are the calling conventions for UNIX & Linux system calls (and user-space functions) on i386 and x86-64](https://stackoverflow.com/q/2535989) RAX is correct, that's why I didn't mention it being part of what you did wrong, only RBX and RCX. Use `strace ./my_prog` to see what args you're passing. – Peter Cordes Sep 11 '20 at 20:33
  • 1
    @PeterCordes omfg, the link you provided just answers all of my questions, thanks. – Dudu Faruk Sep 11 '20 at 20:36
  • @PeterCordes One more question, do I need to close the file after using the `open()` syscall, or asm take care of that? I can guess that I do need to close, but just to be sure – Dudu Faruk Sep 11 '20 at 20:39
  • 1
    The `_exit` syscall which you invoke at the end will close all open files (see `man _exit`). It's good practice to explicitly close all files that you open, unless there's some good reason why it's inappropriate or impractical, but it isn't strictly necessary. (But this has nothing to do with assembly language as a language; the language never does anything except what you tell it. It's a question of what the OS does.) – Nate Eldredge Sep 11 '20 at 20:44
  • @NateEldredge Oh right, I knew that. Thanks and sorry for obvious question – Dudu Faruk Sep 11 '20 at 20:47

0 Answers0