gpt4 book ai didi

linux - nasm 上的 execvp 用法

转载 作者:太空狗 更新时间:2023-10-29 11:18:59 27 4
gpt4 key购买 nike

我现在正在学习 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 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"

关于linux - nasm 上的 execvp 用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26568629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com