TITLE   Add individual digits of a number   ADDIGITS.ASM
COMMENT |
        Objective: To find the sum of individual digits of 
                   a given number. Shows character to binary
                   conversion of digits.
            Input: Requests a number from keyboard.
|          Output: Prints the sum of the individual digits.
DOSSEG
.MODEL SMALL
.STACK 100H
.DATA
number_prompt  DB  'Please type a number (<10 digits): ',0
out_msg        DB  'The sum of individual digits is: ',0
number         DB  11 DUP (?)
.CODE
INCLUDE io.mac
main    PROC
        mov     AX,@DATA     ; initialize DS   
        mov     DS,AX
        PutStr  number_prompt  ; request an input number
        GetStr  number,11    ; read input number as a string
        nwln
        mov     BX,OFFSET number  ; BX := address of number
        sub     DX,DX        ; DX := 0 -- DL keeps the sum
repeat_add:
        mov     AL,[BX]      ; move the digit to AL
        cmp     AL,0         ; if it is the NULL character
        je      done         ;  sum is done
        and     AL,0FH       ; mask off the upper 4 bits
        add     DL,AL        ; add the digit to sum
        inc     BX           ; increment BX to point to next digit
        jmp     repeat_add   ;  and jump back
done:
        PutStr  out_msg
        PutInt  DX           ; write sum
        nwln
        mov     AX,4C00H     ; return to DOS
        int     21H
main    ENDP
        END   main
