Linux Assembly x86_64 使用命令行参数创建文件 [英] Linux Assembly x86_64 create a file using command line parameters

查看:33
本文介绍了Linux Assembly x86_64 使用命令行参数创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自学组装.我找到了一个不错的网站;然而,一切都是为 x86 编写的,我使用的是 64 位机器.

I'm trying to teach myself assembly. I've found a good website; however, everything is written for x86 and I use a 64-bit machine.

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

I know what the problem is, but I don't know how to fix it. If I run the program with strace, then here is the results:

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

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

So, I know that when I call creat, that the file name "hello" is not being passed and as a result I don't have a file descriptor.

这是有问题的代码:

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 命令;但是,我想使用中断.

I know that I can use the syscall command; however, I want to use interrupt.

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

Any ideas or suggestions would be helpful. Also, I'm using nasm an assembler.

推荐答案

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

You attempted to use the 32 bit mechanism. If you have a 32 bit tutorial, you can of course create 32 bit programs and those will work as-is in compatibility mode. If you want to write 64 bit code however, you will need to use the 64 bit conventions and interfaces. Here, that means the syscall instruction with the appropriate registers:

  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

另请参阅 维基百科abi pdf 了解更多详情.

See also the x86-64 sysv abi on wikipedia or the abi pdf for more details.

这篇关于Linux Assembly x86_64 使用命令行参数创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆