phyrrus9
phyrrus9

Reputation: 1467

subroutine not reading from stdin

code is as follows

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 dump shows that 0 bytes were read

(gdb) info registers
eax            0x0      0

does anybody know what is going on here?

Upvotes: 1

Views: 48

Answers (1)

rkhb
rkhb

Reputation: 14399

Two errors (assuming you use NASM):

First, int 80h / eax=3 changes eax. Thus, the next call to that function has not the wished eax, but the code 1 for exit. Move the label .loop just before the mov eax, 3 ; syscall read.

Second, test byte [ecx], 0xA doesn't compare the values. It performs an AND and sets the flags accordingly. The zero flag indicates that the result of the AND was zero. Change the line to cmp byte [ecx], 0xA.

Upvotes: 1

Related Questions