windaoo吧 关注:12贴子:446
  • 5回复贴,共1

命令行参数与环境变量(c & asm)

收藏回复

  • 125.34.70.*


1楼2008-11-23 21:56回复
    • 125.34.70.*
    #include <stdio.h>

    int main(int argc, char * argv[])
    {
     int i = 0;
     while (argv[i]) puts(argv[i++]);
     i++;
     while (argv[i]) puts(argv[i++]);
     return 0;
    }


    2楼2008-11-23 21:56
    回复
      • 125.34.70.*
      #argc.s 虽然写得乱七八糟的,但也算是第一个有点样子的汇编程序:
      .data
              fmt:    .string "There are %d parameters:\n"
              str:    .string "================== shell =================\n"
              flag:   .int    0
      .text
      .globl _start
      _start:
              movl    (%esp), %ecx
              pushl   %ecx
              pushl   $fmt
              call    printf
              addl    $8, %esp

              movl    %esp, %ecx
      s:
              addl    $4, %ecx
              pushl   %ecx
      _loop_print_argv:
              pushl   (%ecx)
              call    puts
              addl    $4, %esp

              popl    %ecx
              addl    $4, %ecx
              pushl   %ecx
              cmpl    $0, (%ecx)
              je      shell_var
              jmp     _loop_print_argv
      shell_var:
              movl    flag, %eax
              cmpl    $0, %eax
              jne     break
              addl    $1, %eax
              movl    %eax, flag
              pushl   $str
              call    puts
              addl    $4, %esp

              popl    %ecx

              jmp     s

      break:
              movl    $1, %eax
              movl    $0, %ebx
              int     $0x80

      #------------------------------


      3楼2008-11-23 22:01
      回复
        • 125.34.70.*
        $mk arg.s
        $cat /usr/bin/mk
        #!/bin/sh

        file=`echo $1 | sed 's/\..*$//g'`
        as -g -o $file.o $file.s && ld -I /lib/ld-linux.so.2 -o $file -lc $file.o


        4楼2008-11-23 22:02
        回复
          • 125.34.70.*
          重发一次 c 代码:
          #include <stdio.h>

          int main(int argc, char * argv[])
          {
           int i = 0;
           while (argv[i]) puts(argv[i++]);
           i++;
           while (argv[i]) puts(argv[i++]);
           return 0;
          }


          5楼2008-11-23 22:02
          回复
            • 125.34.70.*
            #include <stdio.h>

            int main(int argc, char * argv[])
            {
                    int i = 0;
                    while (argv[i]) puts(argv[i++]);
                    i++;
                    while (argv[i]) puts(argv[i++]);
                    return 0;
            }


            6楼2008-11-23 22:03
            回复