title: Transform string into number (array size), input of that array and output it back. ASM
body: <p>In this programm i struggle to convert string into number. I have spent around 10 hours with this code already (yeah, I'm a novice) and I have a strong feeling I'm so close to the point when it's gonna work... Please, prove me right or prove me wrong! =) I tried to add as many comments as possible in the most complicated (as for me) parts of the code so you can save your time in case you have a mood to help me out a little. I'll be glad to explaine code even more if neccessary. Sorry for my English and let's see the code. </p>
  
  <pre><code>model tiny
     .code
      org 0100h
      start:
         mov ax,cs
         mov ds,ax
  
         mov dx,offset text
         mov ah,09h
         int 21h
  
         mov dx,offset x
         mov ah,3fh
         int 21h
  
         mov dh,byte ptr[x+di]   ; Loading first digit into elem.
         sub dh,48 
         mov ah,2
  
  CH2INT:mov elem,dh
  
  _TEST: push di
  
  OFFST1:mov cl,ah
         dec cl
         add di,di
         loop OFFST1
         mov dh,byte ptr[x+di]   ; Loading next (x+di*2) digit into dh.
         pop di                  ; Inc ah for the next iteration.
         inc ah                  ; Here we see if next char is a digit or not,
         cmp dh,"0"              ; if it is (meaning that current digit isn't in the lowest position) -  
         jb INVAL                ; multiplying current digit by 10 (our base),
         cmp dh,"9"              ; if not - jump to INVAL where we go on to the next digit (if there is any).
         ja INVAL                ; Push-pop ax just for convinience, I'm almost out of free-to-use registers.
         push ax                 
         mov al,elem            
         mov ten,10
         imul ten
         mov elem,al
         pop ax
         jmp _TEST
  
  INVAL: mov al,elem             ; Adding number we got to the sum which in the end (after jump to NEXT) 
         add sum,al              ; will containe inputt size of array as a number, not a char.
         push di
  
  OFFST2:mov cl,ah
         dec cl
         add di,di
         loop OFFST2
         mov dh,byte ptr[x+di]   ; Move next digit into dh (x+ah(wich was incremented earlier)+di).
         pop di                  ; See if there is a next digit in x or previous one was the last one
         cmp dh,"0"              ; (I just hope that next value in memory after the inputt will not be a digit...
         jb NEXT                 ; I understand that it's not nice and a luck game, but I do not yet come up with smthn better).
         cmp dh,"9"              
         ja NEXT
         push di                 ; Next few lines write next char into dh (but with index less by 1 than in previous
                             ; few lines, where we checked is there any more digits in the inputt value left.
  OFFST3:mov cl,ah               
         sub cl,2
         add di,di
         loop OFFST3
         mov dh,byte ptr[x+di]
         pop di
         sub dh,48
         jmp CH2INT              ; Jump to next iteration to work with next digit in the inputt value
  
  NEXT:  mov dx,offset array
         mov ah,3fh
         int 21h
         mov cl,sum
  
  L:     mov dh,byte ptr[array+di]
         inc di
         mov dl,dh
         mov ah,02h
         int 21h
         loop L
  
         mov ah,4Ch              ; Service 4Ch - Terminate with Error Code
         mov al,0                ; Error code
         int 21h                 ; Interrupt 21h - DOS General Interrupts
  
       ret
  
       array db 256 dup (?)      ; Array
       x     db ?                ; Size of the array
       ten   db ?
       text  db "Enter number of elements in array: $"
       elem  db ?                ; A single digit from the inputt value  
       sum   db 0                ; Inputt value transformed into number
  
       end start
  </code></pre>
  
  <p>Code written in TASM as a .com executable. It has some irrational things here and there but it's just for the clarity and due to the fact it's just a draft. Hugely appreciate your help! Thanks!</p>
  
  <p>P.S. I'll be modifying the code over time in case you'll give me some advise.</p>