title: Linux NASM detect EOF
body: <p>I'm trying to learn the basics asm on linux and I can't find a very good reference.  The NASM docs seem to assume you already know masm... I found no examples in the documentation of the <code>cmp</code> (outside the Intel instruction reference).</p>
  
  <p>I'd written a program that reads a single 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 issue is it never exits.  I just keeps printing the last char read from stdin.  The issue is either in my EOF detection (<code>cmp     ecx,    EOF</code>) and/or my jump to the <code>_exit</code> label (<code>je      _exit</code>) I think.</p>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>%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
  </code></pre>
  
  <p>For the sake of sanity, I verified EOF is -1 with this C:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  int main() { printf("%d\n", EOF); }
  </code></pre>