子程序没有从标准输入读取

时间:2015-03-12 16:40:34

标签: linux assembly x86 stdin

代码如下

getstr:
    ; get a LF terminated string from stdin
    ; in: EAX = dest buffer
    ; out: ax = bytes read
    ; EAX NOT preserved, all other registers preserved
    ;op     mod     opr1    opr2    comment
    ;--------------------------------------------------------
    push            ebx
    push            ecx
    push            edx
    sub             esp,    2       ; allocate memory
    mov     word    [esp],  0x0000  ; zero memory
    mov             ecx,    eax     ; set the correct buffer
    mov             ebx,    0       ; stdin = 0
    mov             edx,    1       ; 1 byte reads
    mov             eax,    3       ; syscall read
    .loop:
    int             0x80            ; do read
    test    byte    [ecx],  0xA
    je              .done
    inc             ecx
    add     word    [esp],  1       ; increment the count
    jmp             .loop
    .done:
    mov     byte    [ecx],0x0
    pop             ax
    pop             edx
    pop             ecx
    pop             ebx
    ret

gdb转储显示已读取0个字节

(gdb) info registers
eax            0x0      0

有人知道这里发生了什么吗?

1 个答案:

答案 0 :(得分:1)

两个错误(假设您使用NASM):

首先,int 80h / eax=3更改eax。因此,对该函数的下一次调用不是希望eax,而是退出的代码1。将标签.loop移到mov eax, 3 ; syscall read

之前

其次,test byte [ecx], 0xA 不会比较这些值。它执行AND并相应地设置标志。零标志表示AND的结果为零。将行更改为cmp byte [ecx], 0xA

相关问题