.data


stringbuf db    300 dup(0)
stringctr dd    ?


.code

asc2num proc                    ;call with esi = offset to 0 terminated string
        push    ebx             ; eax returns value
        push    ecx
        push    edx

        xor     ebx,ebx
        mov     ecx,10
asc2num1:
        mov     bl,[esi]                ;get a digit
        inc     esi                     ;ptr to next char
        cmp     bl,0                    ;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

num2asc proc                    ;call with eax=number to convert to string
        push    eax             ; edi = offset to a string buffer
        push    ebx             ;  string counter used to count length
        push    ecx
        push    edx

        cmp     eax,0
        jnl     num2asc0
        neg     eax
        inc     stringctr
        mov     cl,'-'
        mov     [edi],cl
        inc     edi

num2asc0:

        mov     ecx,0
num2asc1:
        mov     edx,0
        mov     ebx,10
        div     ebx

        inc     ecx

        add     dl,48
        push    edx

        cmp     eax,0
        je      num2asc2

        jmp     num2asc1

num2asc2:
        cmp     ecx,0
        je      num2asc3

        pop     eax
        stosb
        inc     stringctr
        dec     ecx
        jmp     num2asc2

num2asc3:
        mov     al,32
        stosb
        inc     stringctr
        mov     al,32
        stosb
        inc     stringctr

        pop     edx
        pop     ecx
        pop     ebx
        pop     eax
        ret
num2asc endp

