2011-03-28 70 views
1

我做了一個叫程序嵌入爲什麼我的彙編程序運行無限

源代碼如下。

問題:我不知道爲什麼這個程序無限運行。

我的開發環境是Linux,emacs的,裝配,X86,在& 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 
+0

您是否嘗試過使用調試器? – 2011-03-28 14:36:28

+0

對不起,我沒有試過。不幸的是,我不知道如何使用GDB。你有什麼想法如何解決這個問題? – 2011-03-28 14:42:52

+0

「無窮無盡」是什麼意思?它是否寫入任何輸出?它消耗CPU時間還是似乎掛起?如果掛起,如果按Ctrl-D會發生什麼情況? – 2011-03-28 16:16:39

回答

2

rfd_read(和wfd_read也一樣),你裝的buff內容作爲第二個參數的系統調用:

movl buff,%ecx 

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

movl $buff,%ecx 

所以你傳遞一個壞的指針read系統調用,這幾乎肯定會與%eax = -EFAULT(-14)返回 - 但代碼check_EOF不檢查錯誤。

0

有一個條件退出循環。如果你說的是無限循環,這是因爲退出條件永遠不會被滿足!

嘗試使用單個表格作爲輸入數據。當這解決了這個問題時,試着找出系統調用有什麼問題。

相關問題