Problems with accessing command line arguments in linux from x86 asm

前端 未结 3 2090
死守一世寂寞
死守一世寂寞 2021-01-26 13:28

I have a basic asm program that checks if a string is a digit. I was adding in code to read from command line arguements, put it keeps seg faulting.

if what I have read

相关标签:
3条回答
  • 2021-01-26 14:05

    Here is a little tutorial I wrote on the subject: NASM - Linux Getting command line parameters

    0 讨论(0)
  • 2021-01-26 14:18

    You could write this:

    _start:
    b1: movl    0(%ebp), %eax
        cmpl    $1, %eax
        je      load_msg
    b2:    pushl    8(%ebp)
    b4:    call    check
    

    To understand why your previous attempts didn't work, draw stack diagrams.

    0 讨论(0)
  • 2021-01-26 14:27

    Compile a small C program that does something like what you want to do, and compile it to assembly language to find out exactly how to access arguments. The x86_32 code doesn't look at all like any of the above, BTW:

    int main(int argc, char *argv[])
    {
      return argv[1][0];
    }
    

    gives (yes, some is superfluous stack bookkeeping, but anyway):

        .file   "tst.c"
        .text
        .globl  main
        .type   main, @function
    main:
    .LFB0:
        .cfi_startproc
        pushl   %ebp
        .cfi_def_cfa_offset 8
        .cfi_offset 5, -8
        movl    %esp, %ebp
        .cfi_def_cfa_register 5
        movl    12(%ebp), %eax
        addl    $4, %eax
        movl    (%eax), %eax
        movzbl  (%eax), %eax
        movsbl  %al, %eax
        popl    %ebp
        .cfi_restore 5
        .cfi_def_cfa 4, 4
        ret
        .cfi_endproc
    .LFE0:
        .size   main, .-main
        .ident  "GCC: (GNU) 4.7.2 20121109 (Red Hat 4.7.2-8)"
        .section    .note.GNU-stack,"",@progbits
    
    0 讨论(0)
提交回复
热议问题