如何在程序集中创建具有动态指定文件路径的文件? [英] How do you create a file in assembly with a dynamically specified file path?

查看:80
本文介绍了如何在程序集中创建具有动态指定文件路径的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题主要是如何在没有 db 或类似的帮助程序的情况下,在汇编中处理任意长的字符串的路径名.我看过几个例子,例如,它显示:

This question is mainly how to handle the pathname which is an arbitrarily long string, in assembly, without db or any helpers like that. I have seen several examples such as this which shows:

section .text
   global _start         ;must be declared for using gcc

_start:                  ;tell linker entry point
   ;create the file
   mov  eax, 8
   mov  ebx, file_name
   mov  ecx, 0777        ;read, write and execute by all
   int  0x80             ;call kernel

   section  .data
file_name db 'myfile.txt'

但是,我特别想了解如何动态地执行此操作..我想(1)从汇编的角度更好地理解文件名的要求(是否需要空终止符等),更重要的是(2)指定文件名 without 的用法 db 或任何汇编程序助手.例如,您可以通过命令行指定文件名,而目标文件将不知道其结构.

However, I would specifically like to understand how to do this dynamically. I would like to (1) better understand the requirements of the filename in terms of assembly (does it need a null terminator, etc.), and more importantly (2) specify the file name without the use of db or any assembler helpers. For example, you could specify the file name through the command line, and its structure would be unknown to the object file.

您如何做到的?

推荐答案

采用 const char * 而没有长度arg的系统调用始终采用C字符串:0终止的隐式长度.

System calls that take a const char* without a length arg always take C strings: 0-terminated, implicit length.

类似于 open(const char * path,int flags,int mode),不同于 write(int fd,void * buf,size_t len).

这就是为什么当从C调用 open("input.txt",O_RDONLY) open(argv [1],O_WRONLY | O_CREAT)时它们可以工作的原因.请记住,C字符串文字为您提供了指向带有 0 终止符的静态存储中 char 数组的指针.

This is why they work when called from C like open("input.txt", O_RDONLY), or open(argv[1], O_WRONLY|O_CREAT). Remember that C string literals give you a pointer to an array of char in static storage with a 0 terminator.

顺便说一句,NULL是指针常量.NUL是ASCII '\ 0'.只需将它们称为" 0 终止"字符串即可.

BTW, NULL is a pointer constant. NUL is ASCII '\0'. Just call them "0 terminated" strings.

是的,您应该在 db 的末尾使用,0 .

So yes, you should use , 0 at the end of your db.

命令行参数始终采用C字符串格式;这就是Unix如何跨系统调用/进程边界传递字符串数据,以及传递给ISO C标准库函数的方法.这包括所有路径名.

Command-line args are always in this C-string format; it's how Unix passes around string data across system-call / process boundaries, as well as to ISO C standard library functions. This includes all pathnames.

在Linux中,进入 _start 时,堆栈指针指向 argc .上面是 char * argv [] 数组的元素.(不是 char ** argv 指针,只是堆栈中从ESP + 4到ESP + argc * 4的一系列值.还以NULL指针终止( 0 )).在i386和x86-64 System V ABI文档中对此进行了记录.

In Linux, on entry to _start, the stack pointer points at argc. Above that are the elements of the char *argv[] array. (Not a char **argv pointer, just an array of values right there on the stack, from ESP+4 to ESP+argc*4. Also terminated by a NULL pointer (0)). This is documented in the i386 and x86-64 System V ABI docs.

Linux Assembly x86_64使用以下命令创建文件命令行参数显示了将argv[1]加载到系统调用的pathname参数中的示例.

Linux Assembly x86_64 create a file using command line parameters shows an example of loading argv[1] into the pathname arg of a system call.

从汇编中的文件读取是一个32位示例.

这篇关于如何在程序集中创建具有动态指定文件路径的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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