Assembly and System Calls

前端 未结 2 1230
情歌与酒
情歌与酒 2021-01-24 06:03

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

         


        
相关标签:
2条回答
  • 2021-01-24 06:14

    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.

    Example:

    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);
    
    } 
    

    then:

    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
    
    0 讨论(0)
  • 2021-01-24 06:14

    You don't put strings in a register. You should pass a pointer (the address) to a null (0) terminated string (C style) in the register for this function. Some system calls (like write) take a pointer (not necessarily terminated by '\0') and length in two registers.

    # somewhere in the data section:
    myString:
       .asciz "/bin/bash"
    

    and pass $myString using the register.

    0 讨论(0)
提交回复
热议问题