linux - 如何将字符串数组作为参数传递给函数?

标签 linux assembly x86 fasm

如何将字符串数组作为参数传递给汇编程序中的函数? 例如,假设我想调用如下所示的 execve() 函数:

int execve(const char *filename, char *const argv[], char *const envp[]);

所以我这样做:

测试.asm

format elf executable

entry main

main:
    mov eax, 11 ; execve - executes program
    mov ebx, filename   ; label name is address of string variable
    mov ecx, args       ; label name is address of array of strings?
    mov edx, 0          ; NULL
    int 80h

    mov eax, 1  ;exit
    int 80h
    ret

filename db '/bin/ls', 0        ; path to program
args db '/bin/ls', 0, '.', 0, 0 ; array should end with empty string to
                                ; indicate end of array

生成文件

all:
    ~/apps/fasm/fasm ./test.asm

但是当我运行我的程序时 execve() 无法执行请求的程序并且 strace ./test 显示此消息:

execve("/bin/ls", [0x6e69622f, 0x736c2f, 0x2e], [/* 0 vars */]) = -1 EFAULT (Bad address)

如何正确地将“args”变量传递给 execve 函数?

谢谢:)

最佳答案

你知道这在 C 中是如何工作的吗?字符串是指针,字符串数组是指针数组。因此,您需要执行以下操作:

filename db '/bin/ls', 0 
dot db '.', 0
args dd filename, dot, 0

注意argsdd获取指针大小的项,它填充的是字符串的地址。

关于linux - 如何将字符串数组作为参数传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36248176/

相关文章:

linux - 如何检查是否在 Linux 中按下了某个键?

python - Mat plot lib 绘制图像而不是保存(facebook detectron 代码)

c++ - Mac OS X 或 Linux 上的 _wspawnl/_spawnl 等价物

gcc - x86_64 "gcc -S"-> as -> ld -> 执行失败

C++如何使用不支持的调用约定调用代码

linux - 尝试计算 BASH 中的圆的半径和面积

algorithm - 向左旋转的数学等价物是什么?

linux - 从字符串中去除空格

c - x86 中断处理程序在使用无限循环时被阻塞

assembly - 变量应该在寄存器中存储多长时间?