Linux 在nasm上使用execvp
我现在正在学习NASM,可能是linux系统调用。我正试图复制一个进程并调用linux实用程序,但execvp也有同样的问题,我不知道如何向它传递参数。我怎样才能做到这一点Linux 在nasm上使用execvp,linux,assembly,nasm,cpu-registers,execve,Linux,Assembly,Nasm,Cpu Registers,Execve,我现在正在学习NASM,可能是linux系统调用。我正试图复制一个进程并调用linux实用程序,但execvp也有同样的问题,我不知道如何向它传递参数。我怎样才能做到这一点 SECTION .data cmd_cat: db '/bin/cat', 0 arg_cat: db 'log.txt', 0 cat: dd cmd_cat, arg_cat, 0 fd1: dw 0, 0 pipe_error_message: db 'pipe error
SECTION .data
cmd_cat: db '/bin/cat', 0
arg_cat: db 'log.txt', 0
cat: dd cmd_cat, arg_cat, 0
fd1: dw 0, 0
pipe_error_message: db 'pipe error occured', 0xa
pipe_error_message_length: equ $ - pipe_error_message
fork_error_message: db 'fork error occured', 0xa
fork_error_message_length: equ $ - fork_error_message
SECTION .text
GLOBAL _start:
_start:
;call pipe(fd1)
;42 - pipe system call number
mov eax, 42
mov ebx, fd1
;call kernel to execute
int 080h
cmp eax, 0
jne pipe_error
;call pipe(fd2)
mov eax, 42
mov ebx, fd1
int 080h
cmp eax, 0
jne pipe_error
;fork()
mov eax, 2
int 080h
cmp eax, -1
je fork_error
jnz child_cat
call exit
;displays error message and finishes the programm when something is wrong with pipe
pipe_error:
mov edx, pipe_error_message_length
mov ecx, pipe_error_message
call sys_write
call exit
;displays error message and finishes the programm when something is wrong with fork
fork_error:
mov edx, fork_error_message_length
mov ecx, fork_error_message
call sys_write
call exit
;sys_write(unsigned int fd, const char __user *buf, size_t count);
sys_write:
mov ebx, 1
mov eax, 4
int 080h
;exit(0)
exit:
mov eax,1
mov ebx,0
int 080h
child_cat:
mov ebx, [fd1]
mov eax, 6
int 080h
;dup2(fds[1],1)
mov ecx, 1
mov ebx, [fds + 4]
mov eax, 63
int 080h
mov eax, 11
mov ebx, cmd_cat
mov ecx, cat
int 080h
下面的代码在gas汇编程序中适用于我。我已经解释了它的功能,所以希望其他人能够提供nasm翻译
.text
.global _start
_start:
movl $0xb, %eax # system call 0xb (execve) goes in eax
movl $arg0, %ebx # put the _address_ of the command string
# in ebx (we are providing a pointer)
movl $ptrarray, %ecx # put the _address_ of the array of pointers
# to arguments in ecx (again, a pointer)
movl $0, %edx # put a literal zero in edx (we don't have
# environment variables to pass, so we give
# a null pointer)
int $0x80 # run the system call
.data
ptrarray: # This is the array of pointers to command line
# arguments
.long arg0, arg1, 0 # The first element is a _pointer_ to the command
# The second element is a _pointer_ to an argument
# The third is a null pointer to indicate no more
arg0: # This is the command string
.asciz "/bin/cat"
arg1: # This is the argument string
.asciz "file.txt"
execve系统调用(eax=0xb)将运行一个文件,查找指向ebx中文件名(以null结尾的字符串)的指针、指向ecx中参数指针数组的指针以及指向edx中环境变量指针数组的指针。每个数组都必须由空指针终止,而其他每个元素都是指向以空结尾的字符串(参数或环境变量,视情况而定)的指针。(越来越多)。这就是你想要的吗?是的,谢谢,但我不知道如何在NASM中声明它们(指针数组)。你知道怎么做吗?或者你知道一些文档吗?在x86系统上,指针的大小是4字节。要在初始化为NUL的4字节存储器的.data部分声明数组,请使用:
nameofptr times N dd 0
。(其中N
是所需的指针数)。它与name of ptr dd 0,0,0,0,0…..
参见NASM手册第3.2.5节。零终止字符串!然后数组dd arg\u cat,cmd\u cat,0