title: Assembly x86 - counting small letters in a file
body: <p>I am working on an assignment and I got stuck somehow.
  This is the part of the code, that must be wrong. And what is actually wrong is that when the program should print the number (thtough procedure convertNumber, which works fine!), it doesn't do anything, neither it returns. It just doesn't react, I must close it manually.</p>
  
  <pre><code>read:
              mov ah, 3fh                 ; function for reading from file
              mov bx, handle              ; moving file handle to BX
              mov cx, 10000               ; how many bytes will be read
              mov dx, buffer              ; where the string will be saved
              int 21h                     ; system call ready
  
              mov bytesRead, 0            ; nulling my variable bytesRead, which represents how many bytes have been read
              mov bytesRead, ax           ; now moving the real value from AX
  
              cmp ax, 0                   ; if it is equal to 0, then there is nothing left to read                                                     
              je closeFile                ; close file
  
              mov cx, 0                   ; starting position
  
  cycle:
              cmp cx, bytesRead           ; if I need to reload buffer
              jge read
  
              mov bx, 0                   ; nulling position counter
              mov bx, cx                  ; moving CX to BX -&gt; mov ax, buffer[cx] was an illegal operation, so I did it this way
              mov ax, buffer[bx]          ; getting a character from BX. position from buffer
  
              cmp ax, 'a'
              jb next                     ; below 'a', skip
              cmp ax, 'z'
              ja next                     ; above 'z', skip
              inc count_task              ; if it passed, it is between 'a' and 'z'
              jmp next                    ; and moving on
  
  next:
              inc cx                      ; increase position counter (always!)
              jmp cycle                   ; and repeat
  
  finally:
              mov ax, count_task          ; move the final count to AX (needed by the procedure convertNumber)
              call convertNumber          ; convert number to its ASCII value (+48)
              ret
  
  error_fopen:
              print fopenErr
              ret
  
  error_fclose:
              print fcloseErr
              ret
  
  closeFile:
              mov ah, 3eh
              mov bx, handle
              int 21h
  
              jc error_fclose
              jmp finally
  
  convertNumber proc near
      push si
  
      mov buff, '$'
      lea si, buff
      mov cx, 10
  
  conv:
      mov dx, 0
      div cx                          ; AX is being divided by CX, where value 10 is stored (direct dividing by 10 is not legal)
      add dx, 48                      ; getting ASCII value of the number (from 0 to '0')
      dec si                          ; going backwards
      mov [si], dl
      cmp ax, 0
      jz printNumber                  ; if AX is empty (0), print the number
      jmp conv                        ; else loop 
  
  printNumber:
      mov ah, 9                       ; service to print string
      mov dx, si                      ; position of my string (number)
      int 21h
  
      pop si
  
      ret
  convertNumber endp      
  </code></pre>
  
  <p>I hope you can understand what is going on in this code, if not, I can explain you. I would be very grateful for any help!</p>