.model small
.386

n       equ     254

.stack

.data

crlf    db      10,13,36


string  db      n,?,n+1 dup (36)


p1      db      'x: ',36
p2      db      'y: ',36
p3      db      'z: ',36

s1      db      'x + y + z = ',36

total   dd      0

.code


main    proc

        mov     ax,@data
        mov     ds,ax                   ;set default data segment


        mov     ah,9                    ;print the input prompt
        mov     dx,offset p1
        int     21h

        mov     ah,0Ah                  ;input string
        mov     dx,offset string
        int     21h

        mov     ah,9                    ;next line
        mov     dx,offset crlf
        int     21h

        mov     si,offset string+2      ;convert the string to a number
        call    asc2num
        add     total,eax               ;add to total



        mov     ah,9                    ;print the input prompt
        mov     dx,offset p2
        int     21h

        mov     ah,0Ah                  ;input string
        mov     dx,offset string
        int     21h

        mov     ah,9                    ;next line
        mov     dx,offset crlf
        int     21h

        mov     si,offset string+2      ;convert the string to a number
        call    asc2num
        add     total,eax               ;add to total



        mov     ah,9                    ;print the input prompt
        mov     dx,offset p3
        int     21h

        mov     ah,0Ah                  ;input string
        mov     dx,offset string
        int     21h

        mov     ah,9                    ;next line
        mov     dx,offset crlf
        int     21h

        mov     si,offset string+2      ;convert the string to a number
        call    asc2num
        add     total,eax               ;add to total



        mov     ah,9                    ;print result string
        mov     dx,offset s1
        int     21h

        mov     eax,total               ;print total
        call    var

xit:
        mov     ax,4C00h                ;to dos
        int     21h
main    endp

asc2num proc    ;call with si = offset to string eax returns value
        push    ebx
        push    ecx
        push    edx

        xor     eax,eax
        xor     ebx,ebx
        mov     ecx,10
asc2num1:
        mov     bl,[si]                 ;get a digit
        inc     si                      ;ptr to next char
        cmp     bl,13                   ;end string?
        je      asc2numx                ;yeah, number convered
        sub     bl,48                   ;make digit binary

        mul     ecx                     ;total * 10
        add     eax,ebx                 ;+ digit
        jmp     asc2num1                ;next digit

asc2numx:
        pop     edx
        pop     ecx
        pop     ebx
        ret
asc2num endp




var     proc                            ;bin-->dec, call with eax=var
        push    eax
        push    ebx
        push    ecx
        push    edi
        push    es


        mov     ecx,0
var1:
        mov     edx,0
        mov     ebx,10
        div     ebx

        inc     ecx

        add     dl,48
        push    edx

        cmp     eax,0
        je      var2

        jmp     var1

var2:
        cmp     ecx,0
        je      var3

        pop     edx
        mov     ah,2
        int     21h
        dec     ecx
        jmp     var2

var3:
        mov     ah,2
        mov     dl,32
        int     21h


        pop     es
        pop     edi
        pop     ecx
        pop     ebx
        pop     eax
        ret
var     endp


        end     main

