title: x86 ASM - Read line by line
body: <p>Okay, all I want to do is just read line by line from text and then output it.</p>
  
  <hr>
  
  <p><strong><em>Things I do:</em></strong></p>
  
  <ul>
  <li>Read single char, add to string</li>
  <li>Search for LF</li>
  <li>Output string</li>
  <li>Do all over again until EOF</li>
  </ul>
  
  <hr>
  
  <p><strong><em>Problems:</em></strong></p>
  
  <ul>
  <li>No idea how to count cx for 40h interrupt while It's busy with 3Fh ( tried to make some new variables, but ended up with bunch of errors )</li>
  <li>Some sort of odd result  - few blank lines and the very last one</li>
  </ul>
  
  <hr>
  
  <blockquote>
    <p><strong><em>.txt example</em></strong></p>
    
    <blockquote>
  <pre><code>    some
      random
  
      text
  </code></pre>
    </blockquote>
  </blockquote>
  
  <pre><code>    .model small
  
      .stack  100h 
  
      .data 
              filename    db 255 dup(0)   
              text        db 255 dup(0)
              char        db ?
              line        db 255 dup(0)           
  
              filehandle  dw ?
  
      .code 
              newline macro    ;NEW LINE 
                               ;
              mov dl, 10       ;
              mov ah, 02h      ;                   
              int 21h          ;
                               ;
              mov dl, 13       ;
              mov ah, 02h      ;                     ;
              int 21h          ;                
              endm             ;NEW LINE
      main:   
  
              mov ax, @data    
              mov ds, ax   
  
              lea si, filename
              mov ah, 01h      ;read character
  
      char_input:
  
              int 21h         
  
              cmp al, 0dh      ;enter     
              je zero_terminator
  
              mov [si], al    
              inc si
  
              jmp char_input  
  
      zero_terminator:
  
              mov [si], 0 
  
      open_file:
  
              lea dx, filename         
              mov al, 0          
              mov ah, 3Dh      ;open file
              int 21h  
  
              mov filehandle, ax
  
              lea si, text   
  
              newline
  
      read_line:
              mov ah, 3Fh      ;read file
              mov bx, filehandle  
              lea dx, char         
              mov cx, 1  
  
              int 21h  
  
              cmp ax, 0       ;EOF            
              je EO_file
  
              mov al, char        
  
              cmp al, 0ah     ; line feed
              je LF  
  
              mov [si], al
              inc si      
  
              jmp read_line:
  
      EO_file:
  
              lea dx, text
              mov ah, 40h     ;print 
              mov cx, 255
              mov bx, 1       
              int 21h
  
              mov ah, 4ch     
              int 21h 
  
      LF:       
              lea dx, text
              mov ah, 40h     ;print
              mov cx, 255
              mov bx, 1
  
              int 21h
  
              inc si
              jmp read_line
  
       end main  
  </code></pre>