;**********************************************************
;                      HEXADD.ASM
; This program adds two ASCII-coded, single-digit hex
; numbers. If the result is a single-digit hex number,
; it stores its ASCII code; if not, it stores an FFh.
; It uses an a_to_h procedure to convert from ASCII to hex
; and an h_to_a procedure to convert from hex to AXCII.
;**********************************************************
 
stk     segment stack
        dw      16 dup(?)
stk     ends

data    segment
d1      db      '2'         ;The ASCII codes of the...
d2      db      'A'         ;hex digits to be added
sum     db      ?           ;The ASCII code of the sum
data    ends

code    segment
        assume  ds:data,cs:code
;**********************************************************
; The main procedure gets each ASCII code, calls a_to_H for
; each to convert it to a hex value, then adds the values.
; If result is a hex digit, it calls h_to_a to convert it
; to an ASCII code, and stores the result.
;**********************************************************
main    proc    far
        push    ds           ;Bookkeeping
        sub     ax,ax
        push    ax
        mov     ax,data
        mov     ds,ax        ;End bookkeeping
        mov     al,d1        ;Get 1st number
        call    a_to_h       ;Convert to hex (al)
        mov     bl,al        ;Store in bl  
        mov     al,d2        ;Get 2nd number
        call    a_to_h       ;Convert to hex (al)   
        add     al,bl        ;Add two values 
        cmp     al,0fh       ;If OK...
        jbe     conv         ;jump to CONV    
        mov     sum,0ffh     ;Else store 0ffh
        jmp     done         ;All done
conv:   call    h_to_a       ;Convert to ascii (al)   
        mov     sum,al       ;Store ascii code
done:   ret                  ;Return to OS 
main    endp

;***********************************************************
;A_TO_H   
;       Determines hex value of ASCII code in al.
;       Assumes al contains ascii code of valid hex digit.
;       Registers affected: al
;       Flags affected: all
;       Input parameter: ascii code in al
;       Output parameter: hex value in al
;***********************************************************
a_to_h  proc    near
        sub     al,30h
        cmp     al,9    ;It's between 0 & 9
        jbe     done1
        sub     al,7    ;It's between A & F
done1:  ret
a_to_h  endp

;***********************************************************
;H_TO_A   
;       Determines ASCII code of hex digit stored in al.
;       Assumes it's a single hex digit.
;       Registers affected: al
;       Flags affected: all
;       Input parameter: hex digit in al
;       Output parameter: ascii code in al
;***********************************************************
h_to_a  proc    near
        add     al,30h
        cmp     al,39h   ;It's between '0' & '9'
        jbe     done2 
        add     al,7     ;It's between 'A' and 'F'
done2:  ret        
h_to_a  endp

code    ends
end     main