gpt4 book ai didi

Linux Assembly x86_64 使用命令行参数创建文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:19:43 25 4
gpt4 key购买 nike

我正在尝试自学组装。我找到了一个很好的website ;然而,一切都是为 x86 编写的,我使用的是 64 位机器。

我知道问题出在哪里,但我不知道如何解决。如果我用 strace 运行程序,结果如下:

execve("./file", ["./file", "hello"], [/* 94 vars */]) = 0
creat(NULL, 0) = -1 EINVAL (Invalid argument)
write(0, NULL, 0 <unfinished ...>
+++ exited with 234 +++

所以,我知道当我调用 creat 时,没有传递文件名“hello”,因此我没有文件描述符。

这里是有问题的代码:

section .text
global _start

_start:
pop rbx ; argc
pop rbx ; prog name
pop rbx ; the file name

mov eax,85 ; syscall number for creat()
mov ecx,00644Q ; rw,r,r
int 80h ; call the kernel

我知道我可以使用 syscall 命令;但是,我想使用中断。

任何想法或建议都会有所帮助。另外,我正在使用 nasm 汇编程序。

最佳答案

您试图使用 32 位机制。如果您有 32 位教程,您当然可以创建 32 位程序,这些程序将在兼容模式下按原样运行。但是,如果您想编写 64 位代码,则需要使用 64 位约定和接口(interface)。在这里,这意味着带有适当寄存器的 syscall 指令:

  global _start

_start:
mov eax,85 ; syscall number for creat()
mov rdi,[rsp+16] ; argv[1], the file name
mov esi,00644Q ; rw,r,r
syscall ; call the kernel
xor edi, edi ; exit code 0
mov eax, 60 ; syscall number for exit()
syscall

另请参阅 wikipedia 上的 x86-64 sysv abi或 abi pdf了解更多详情。

关于Linux Assembly x86_64 使用命令行参数创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36071711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com