【问题标题】:why does my asm program run infinitely为什么我的asm程序无限运行
【发布时间】:2011-03-28 14:22:21
【问题描述】:

我做了一个程序叫embed

源码如下。

问题:我不知道为什么这个程序会无限运行。

我的开发环境是linux,emacs,assembly,x86,at&t语法

    #usage : embed input output message
    #this program embed message to input's text and make an output file
    #example1:
    #input: "abcde"
    #message: dc
    #output: "abcDe"
    #example2:
    #input: "abcde"
    #message: bcd
    #output: "aBCDe"

    .section .data
    .section .bss
        .lcomm buff,1
    .section .text
    .global _start
_start:
initialize:
    movl %esp,%ebp
    movl $0,%edi
    subl $8,%esp    #cleared at the exit_program
open_r:
    movl $5,%eax
    movl 8(%ebp),%ebx
    movl $0,%ecx
    movl $0666,%edx
    int $0x80
save_rfd:   #save to -4(%ebp)
    movl %eax,-4(%ebp)
open_w:
    movl $5,%eax
    movl 12(%ebp),%ebx
    movl $03101,%ecx
    movl $0666,%edx
    int $0x80
save_wfd:   #save to -8(%ebp)
    movl %eax,-8(%ebp)
loop:
rfd_read:
    movl $3,%eax
    movl -4(%ebp),%ebx
    movl buff,%ecx
    movl $1,%edx
    int $0x80
check_EOF:
    cmpl $0,%eax
    je exit_program
call_func:
    pushl 16(%ebp)  #16(%ebp) is message
    call checkNconvert  #this will change buffer
wfd_write:
    movl $4,%eax
    movl -8(%ebp),%ebx
    movl buff,%ecx
    movl $1,%edx
    int $0x80
jump_loop:
    jmp loop
exit_program:
    addl $8,%esp
    movl $1,%eax
    movl $0,%ebx
    int $0x80

checkNconvert:
    pushl %ebp
    movl %esp,%ebp
    movl 8(%ebp),%ebx   #8(%ebp) is message that passed over
    movb (%ebx,%edi,1),%bl  #message's edi'th character to %bl
    cmpb buff,%bl       #compare
    jne end_checkNconvert
    .equ n, 'a' - 'A'   #n is just number should be used as $n
    subb $n,buff
    incl %edi
end_checkNconvert:
    movl %ebp,%esp
    popl %ebp
    ret

【问题讨论】:

  • 你试过使用调试器吗?
  • 对不起,我没试过。不幸的是,我不知道如何使用 GDB。你知道如何解决这个问题吗?
  • “无限运行”是什么意思?它写任何输出吗?它是否消耗 CPU 时间或似乎挂起?如果挂了,按Ctrl-D会怎样?

标签: linux assembly x86 infinite-loop


【解决方案1】:

rfd_read(以及wfd_read)中,您正在加载buff 的内容作为系统调用的第二个参数:

movl buff,%ecx

...但是你想要的是buff地址

movl $buff,%ecx

所以你传递了一个指向read 系统调用的错误指针,它几乎肯定会返回%eax = -EFAULT (-14) - 但check_EOF 处的代码不会检查错误。

【讨论】:

    【解决方案2】:

    退出循环只有一个条件。如果你说得对,它无限循环,这是因为退出条件永远不会满足!

    尝试使用单个表作为输入数据。当这解决了问题时,尝试找出系统调用出了什么问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 2012-04-15
      • 1970-01-01
      • 2012-12-10
      相关资源
      最近更新 更多