汇编和系统调用 [英] Assembly and System Calls
问题描述
我在理解组装中更复杂的系统调用时遇到了一些麻烦.我写了一个exec系统调用,效果很好
Im having a bit of trouble understanding the more complex system calls in assembly. I wrote a exec system call and it worked great
.bss
.text
.globl _start
_start:
#exit(0) system call
movl $1, %rax
movl $0, %rbx
int $0X80
尽管我有一点保证,但无法找到有关如何将字符串放入寄存器的信息.因此,作为一个示例,我想执行一个exec系统调用,因为它的第一个参数需要运行一个文件名,并且我想运行"/bin/bash",但是如何在rbx中获取它.我什至不知道我必须使用rbx,在X86中我知道我会使用ebx,在amd64 ebx = rbx,ecx = rcs等中是否存在相同的关系?
Though I am a bit insure and have not been able to find info pertaining to how you put strings in a register. So as an example I wanted to do a exec system call and it as its first parameter needs a filename to run and I want to run "/bin/bash", but how do I get that in rbx. How do I even know that I have to use rbx, in X86 I know I would use ebx, is it the same relationship in amd64 ebx=rbx, ecx=rcs, etc.
int execve(const char * filename,char * const argv [], char * const envp []);
int execve(const char *filename, char *const argv[], char *const envp[]);
谢谢
推荐答案
以下是在汇编的这些方面快速取得进展的窍门:让C编译器向您展示它是如何实现的!编写一个可以执行所需操作的C程序,然后键入gcc -S
.
Here's a trick to make progress quickly with these aspects of assembly: ask a C compiler to show you how it does it! Write a C program that does what you want to do and type gcc -S
.
示例:
Manzana:ppc pascal$ cat t.c
#define NULL ((void*)0)
char *args[] = { "foo", NULL } ;
char *env[] = { "PATH=/bin", NULL } ;
int execve(const char *filename, char *const argv[], char *const envp[]);
int main()
{
execve("/bin/bash", args, env);
}
然后:
Manzana:ppc pascal$ gcc -S -fno-PIC t.c # added no-PIC for readability of generated code
Manzana:ppc pascal$ cat t.s
.globl _args
.cstring
LC0:
.ascii "foo\0"
.data
.align 2
_args:
.long LC0
.long 0
.globl _env
.cstring
LC1:
.ascii "PATH=/bin\0"
.data
.align 2
_env:
.long LC1
.long 0
.cstring
LC2:
.ascii "/bin/bash\0"
.text
.globl _main
_main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $_env, 8(%esp)
movl $_args, 4(%esp)
movl $LC2, (%esp)
call _execve
leave
ret
.subsections_via_symbols
这篇关于汇编和系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!