Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Linux中的x86_64程序集中创建新文件_Linux_Assembly_X86 64 - Fatal编程技术网

在Linux中的x86_64程序集中创建新文件

在Linux中的x86_64程序集中创建新文件,linux,assembly,x86-64,Linux,Assembly,X86 64,我试图在Linux中使用opensyscall,在手册页面中它包含3个参数,pathname,标志,以及文件模式。我试图用程序集x86_64(Linux)实现它,但它没有创建文件。顺便问一下,这个系统调用的返回值存储在哪里?open()的返回值是文件的文件描述符,然后我要将其写入 section .text global _start section .data pathname db "/home/user/Desktop/myfile.txt" _st

我试图在Linux中使用
open
syscall,在手册页面中它包含3个参数,
pathname
标志
,以及文件
模式
。我试图用程序集x86_64(Linux)实现它,但它没有创建文件。顺便问一下,这个系统调用的返回值存储在哪里?
open()
的返回值是文件的文件描述符,然后我要将其写入

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
副本似乎对我不起作用,它没有创建文件。我的路径是有效的

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

O_create
不是字符串。它是一个扩展为数字的宏。
模式
也是一个包含文件权限模式的数字,而不是像
w
@DuduFaruk那样的字符串:是的,RAX代表呼叫号码,根据x86-64 System V ABI文档,在RDI、RSI、RDX、R10、R8、R9中包含参数。RAX是正确的,这就是为什么我没有提到这是你做错的一部分,只有RBX和RCX。使用
strace./my_prog
查看您传递的参数。@PeterCordes omfg,您提供的链接刚刚回答了我的所有问题,谢谢。最后调用的
\u exit
syscall将关闭所有打开的文件(请参见
man\u exit
)。明确关闭您打开的所有文件是一种很好的做法,除非有很好的理由说明它不合适或不实用,但这并不是绝对必要的。(但这与汇编语言作为一种语言无关;这种语言除了你告诉它什么以外,什么都不做。这是一个操作系统做什么的问题。)