LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1188|回复: 1

关于段错误的一个问题?

[复制链接]
发表于 2008-3-14 22:05:07 | 显示全部楼层 |阅读模式
我想实现一个程序,把命令行参数显示到屏幕上,具体的汇编程序如下:
#comm.s
.section .data
        cmd_tb1:     #分配内存,存放执行命令行参数的指针,最多有可存放十个
                .rept 10
                        .long 0
                .endr
.section .text
        .globl _start
_start:
        movl (%esp), %ecx
        cmpl $10, %ecx               #如果命令行参数大于10,则退出。
        jg _exit

        movl $1, %esi
        movl $0, %edi

store_loop:                             #复制命令行参数指针到cmd_tb1
        movl  (%esp, %esi, 4), %eax
        movl  %eax, cmd_tb1( , %edi, 4)
        incl  %esi
        incl  %edi
        loop store_loop


        movl %edi, %ecx
        movl  $0, %esi
print_loop:
        movl cmd_tb1(, %esi, 4), %eax
        pushl %eax
        call puts                         #调用C函数显示
        popl %eax
        incl %esi
        loop print_loop
_exit:
        pushl $0
        call exit

编译和联接都没有出现问题,具体的命令如下:
as -o comm.o comm.s
ld -o comm -dynamic-linker /lib/ld-linux.so.2 -lc comm.o
可是在运行的时候确提示段错误,不知道错在哪里,望高手指点。
发表于 2008-3-15 14:36:35 | 显示全部楼层
贴一个别处得来代码, 自己看看吧, 我懒
  1.         ## stack-param.s ###############################################

  2.         ## Robin Miyagi ################################################
  3.         ## http://www.geocities.com/SiliconValley/Ridge/2544/ ##########

  4.         ## This file  shows how one  can access command  line parameters
  5.         ## via the stack at process  start up.  This behavior is defined
  6.         ## in the ELF specification.

  7.         ## Compile Instructions:
  8.         ## -------------------------------------------------------------
  9.         ## as -o stack-param.o stack-param.s
  10.         ## ld -O0 -o stack-param stack-param.o
  11. ########################################################################
  12.         .section .data

  13. new_line_char:
  14.         .byte 0x0a
  15. ########################################################################
  16.         .section .text

  17.         .globl _start

  18.         .align 4
  19. _start:
  20.         movl %esp, %ebp                # store %esp in %ebp
  21. again:
  22.         addl $4, %esp                # %esp ---> next parameter on stack
  23.         movl (%esp), %eax        # move next parameter into %eax
  24.         testl %eax, %eax        # %eax (parameter) == NULL pointer?
  25.         jz end_again                # get out of loop if yes
  26.         call putstring                # output parameter to stdout.
  27.         jmp again                # repeat loop
  28. end_again:
  29.         xorl %eax, %eax                # %eax = 0
  30.         incl %eax                # %eax = 1, system call _exit ()
  31.         xorl %ebx, %ebx                # %ebx = 0, normal program exit.
  32.         int $0x80                # execute _exit () system call

  33.         ## prints string to stdout
  34. putstring:        .type @function
  35.         pushl %ebp
  36.         movl %esp, %ebp
  37.         movl 8(%ebp), %ecx
  38.         xorl %edx, %edx
  39. count_chars:
  40.         movb (%ecx,%edx,1), %al
  41.         testb %al, %al
  42.         jz done_count_chars
  43.         incl %edx
  44.         jmp count_chars
  45. done_count_chars:
  46.         movl $4, %eax
  47.         xorl %ebx, %ebx
  48.         incl %ebx
  49.         int $0x80
  50.         movl $4, %eax
  51.         leal new_line_char, %ecx
  52.         xorl %edx, %edx
  53.         incl %edx
  54.         int $0x80
  55.         movl %ebp, %esp
  56.         popl %ebp
  57.         ret
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表