2013-11-20 85 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