title: Reading from a file in assembly
body: <p>I'm trying to learn assembly -- x86 in a Linux environment.  The most useful tutorial I can find is <a href="http://leto.net/writing/nasm.php" rel="noreferrer">Writing A Useful Program With NASM</a>.  The task I'm setting myself is simple: read a file and write it to stdout.</p>
  
  <p>This is what I have:</p>
  
  <pre><code>section  .text              ; declaring our .text segment
    global  _start            ; telling where program execution should start
  
  _start:                     ; this is where code starts getting exec'ed
  
    ; get the filename in ebx
      pop   ebx               ; argc
      pop   ebx               ; argv[0]
      pop   ebx               ; the first real arg, a filename
  
    ; open the file
      mov   eax,  5           ; open(
      mov   ecx,  0           ;   read-only mode
      int   80h               ; );
  
    ; read the file
      mov     eax,  3         ; read(
      mov     ebx,  eax       ;   file_descriptor,
      mov     ecx,  buf       ;   *buf,
      mov     edx,  bufsize   ;   *bufsize
      int     80h             ; );
  
    ; write to STDOUT
      mov     eax,  4         ; write(
      mov     ebx,  1         ;   STDOUT,
    ; mov     ecx,  buf       ;   *buf
      int     80h             ; );
  
    ; exit
      mov   eax,  1           ; exit(
      mov   ebx,  0           ;   0
      int   80h               ; );
  </code></pre>
  
  <p>A crucial problem here is that the tutorial never mentions how to create a buffer, the <code>bufsize</code> variable, or indeed variables at all.</p>
  
  <p>How do I do this?</p>
  
  <p>(An aside: after at least an hour of searching, I'm vaguely appalled at the low quality of resources for learning assembly.  How on earth does any computer run when the only documentation is the hearsay traded on the 'net?)</p>