; Receive a number up to 4 digits, print back a number greater by 1. If we get 245
; we'll print 246. Limit is 4 digit result, so max input is 9998. 
;
; The algorithm: 1. break the input to digits, add the digit to [num], multiply by 10
;                   until no more digits
;                2. add 1 to [num]
;                3. Divide [num] by 10 and use the remainder from right to left to build
;                   the output (each digit coverted to its ascii representation.  
;
IDEAL
MODEL small
STACK 100h
DATASEG
asknum        db   'Please enter a number of up to 4 digits$'
input         db    7 dup(?)  ; input buffer - we need 3 extra bytes to read a string (4+3=7)
result        db    '    $'   ; prepares the string for the output
base          dw    10
num           dw    0         ; stores the ascii number converted to actual number 
CODESEG
proc          newline
; This procedure provides new line
              push ax
              push dx
    		  mov  ah,2               
			  mov  dl,10
			  int  21h
			  pop  dx
			  pop  ax
			  ret
endp          newline	
start:
              mov ax, @data
              mov ds, ax
;Ask for a number  
              mov  dx, offset asknum   
              mov  ah,9h
              int  21h 
              call newline               				  			  	  
;Receive input from the keyboard 
              mov  dx, offset input
              mov  bx, dx
              mov  [byte ptr bx],5 ;max size of number is 4 plus one for the <enter>
              mov  ah,0Ah
              int  21h   
              xor  cx,cx
              mov  cl,[input+1]  ;actual size of input number (in the second byte of the buffer)                               
; convert ascii characters to number
              add  bx,2          ;point to the first digit of input
              mov si,0
              xor dx,dx          ;accumulates digits times 10
inputl:
              xor  ax,ax
              mov  al,[bx+si]
              sub  al,'0'        ;make ascii digit 'real'
              push ax            ;save the digit on the stack (we could save anywhere)
              mov  ax,[num]
              mul  [base]        ; dx:ax are the result, only ax has meaningful value
              mov  [num],ax
              pop  ax            ; restore the digit
              add  [num],ax
              inc  si
              loop inputl               
; Now actual result is in [num]
              mov  dx,[num]
              add  dx,1
; Convert actual number in dx to ascii, for output.  
              mov  bx, offset result
              add  bx,3          ; place the digits from right to left in result.        
              mov  ax,dx         ; put the number in dx:ax prepare for division
loopnum:      xor  dx,dx         ; loop dividing by 10, until number is zero.        
              div  [base]
              add  dl,'0'
              mov  [bx], dl
              dec  bx
              cmp  ax,0          ; num becomes zero we're done
              je   printnum
              jmp  loopnum
printnum:
              mov  dx, offset result
              mov  ah,9h
              int  21h
              mov ax, 4c00h
              int 21h
END start  