2013-11-20 54 views
0

從Python的背景來看,我試圖聯繫自己一些大會。掙扎閱讀用戶輸入並打印它

到目前爲止,我已經相處得很好,但現在我遇到了問題。我所遵循的教程要求我編寫一些問候用戶的代碼,要求他輸入一些內容,然後在控制檯上顯示這些文本。

所以我基本上成功地做到這一點,但腳本隨機切斷輸出的部分一定長度後 - 打字Fine工作很完美,但Fine, thanks!給我回nks!,Finee, thanks!給我回Fineeanks!e。對於一個字符串,這種行爲也總是相同的。

這是我的代碼(對不起張貼的所有代碼,但我不知道在哪裏的錯誤可能是)

.section .data 
hi: .ascii "Hello there!\nHow are you today?\n" 
in: .ascii "" 
inCp: .ascii "Fine" 
nl: .ascii "\n" 
inLen: .long 0 

.section .text 

.globl _start 
_start: 
    Greet: # Print the greeting message 
    movl $4, %eax # sys_write call 
    movl $1, %ebx # stdout 
    movl $hi, %ecx # Print greeting 
    movl $32, %edx # Print 32 bytes 
    int $0x80 # syscall 

    Read: # Read user input 
    movl $3, %eax # sys_read call 
    movl $0, %ebx # stdin 
    movl $in, %ecx # read to "in" 
    movl $10000, %edx # read 10000 bytes (at max) 
    int $0x80 # syscall 

    Length: # Compute length of input 
    movl $in, %edi # EDI should point at the beginning of the string 
    # Set ecx to highest value/-1 
    sub %ecx, %ecx 
    not %ecx 
    movb $10, %al 
    cld # Count from end to beginning 
    repne scasb 
    # ECX got decreased with every scan, so this gets us the length of the string 
    not %ecx 
    dec %ecx 
    mov %ecx, (inLen) 
    jmp Print 

    Print: # Print user input 
    movl $4, %eax 
    movl $1, %ebx 
    movl $in, %ecx 
    movl (inLen), %edx 
    int $0x80 

    Exit: # Exit 
    movl $4, %eax 
    movl $1, %ebx 
    movl $nl, %ecx 
    movl $1, %edx 
    int $0x80 
    movl $1, %eax 
    movl $0, %ebx 
    int $0x80 

我使用的是GNU彙編程序的Debian Linux操作系統(32位),所以這是用AT & T語法編寫的。

有沒有人有一個想法,爲什麼我得到這些奇怪的錯誤?

回答

1
in: .ascii "" 
... 
movl $in, %ecx # read to "in" 
movl $10000, %edx # read 10000 bytes (at max) 

您正在閱讀的用戶輸入到有房間沒有數據在所有的變量,所以你會被搗毀後in無論發生什麼事。

儘量保留一些空間來保存用戶的輸入,例如:

in: .space 256 
+1

這完美地工作,謝謝!我想我太習慣動態內存分配... – jazzpi