Gnu汇编程序 - 使用Fork()

时间:2014-04-06 12:27:17

标签: shell assembly gnu

我想生成一个shell,例如/ bin / sh的。

所以我在这里查看:http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html 所以fork是系统调用号2。

所以我的代码看起来像是:

.globl  _start

.text
_start:
    movl    **WTF-HERE?!?!** (how I use pt_regs?), %ebx
    movl    $2, %eax
    int $0x80

    movl    $0, %ebx
    movl    $1, %eax
    int $0x80
.data

任何人都有想法?

1 个答案:

答案 0 :(得分:1)

Afaik表是内核中的入口寄存器状态,而不是你怎么称呼它

只需将syscall保留ebx和ecx,并按如下方式处理系统调用结果:

    pushl  ebx          # registers to preserve
    pushl  ecx
    movl    $2, %eax    # system call number for fork.
    int    $0x80        # call int
    popl   ecx          # restore preserved regs.
    popl   ebx
    cmpl  $-4095,%eax   # int returning values between-4095..-1 -> error.
    jb    .LSyscOK
    negl  %eax          # error. Negate value.
    call  seterrno      # call a procedure that sets errno in a PIC safe way.
    movl  $-1,%eax      # set return value in case of error (exactly -1)
 .LSyscOK:

阅读联机帮助页,了解如何确定您是在孩子中还是在父母中。注意事后你可以在父母身上做些什么。关于BSD系统的注意事项,您可能实际上想要调用rfork来生成进程。