Linux NASM Detects EOF

I am trying to learn the basics of asm for linux and I cannot find a very good link. NASM docs seem to suggest that you already know masm ... I have not found any examples in the documentation cmp(outside of the Intel instruction help).

I wrote a program that reads one byte from stdin and writes it to stdout. Below is my modification to try to detect EOF on stdin and exit when EOF is reached. The problem is that he never goes out. I just print the last char read from stdin. The problem is my detection of EOF ( cmp ecx, EOF) and / or my jump to label _exit( je _exit), I think.

What am I doing wrong?

%define EOF     -1

section .bss
        char:   resb    1

section .text
        global  _start

_exit:
        mov     eax,    1       ; exit
        mov     ebx,    0       ; exit status
        int     80h

_start:
        mov     eax,    3       ; sys_read
        mov     ebx,    0       ; stdin
        mov     ecx,    char    ; buffer
        cmp     ecx,    EOF     ; EOF?
        je      _exit
        mov     edx,    1       ; read byte count
        int     80h

        mov     eax,    4       ; sys_write
        mov     ebx,    1       ; stdout
        mov     ecx,    char    ; buffer
        mov     edx,    1       ; write byte count
        int     80h

        jmp     _start

For the sake of sanity, I checked EOF -1 with this C:

#include <stdio.h>
int main() { printf("%d\n", EOF); }
+5
source
1

EOF (-1) , .

, read EOF, , (. man 2 read). , eax read:

section .bss
    buf:   resb    1

section .text
    global  _start

_exit:
    mov     eax,    1       ; exit
    mov     ebx,    0       ; exit status
    int     80h

_start:
    mov     eax,    3       ; sys_read
    mov     ebx,    0       ; stdin
    mov     ecx,    buf    ; buffer
    mov     edx,    1       ; read byte count
    int     80h

    cmp     eax, 0
    je      _exit

    mov     eax,    4       ; sys_write
    mov     ebx,    1       ; stdout
    mov     ecx,    buf    ; buffer
    mov     edx,    1       ; write byte count
    int     80h

    jmp     _start

, :

cmp byte [buf], VALUE

, char buf. char - C .

+6

All Articles