Ruschisb
Ruschisb

Reputation: 55

How to convert an ASCII char to a decimal representation?

Hello I need some help with this problem, I need to get an input from the user an ASCII character and print back its decimal value so for example:

Input A --> Output 65
Input B --> Output 66

.model small
.stack 100h
.data
msg1 db 10,13,"Enter a number: $"
msg2 db 10,13,"in decimal is : $"

.code
main proc
    
    mov ax,@data
    mov ds,ax
input:
    lea dx,msg1
    mov ah,09h
    int 21h
    
    mov ah,1h
    int 21h
    
    mov bl,al
    cmp bl,0dh
    je end
    
    
    lea dx,msg2
    mov ah,09h
    int 21h
    
    mov cx,4
convert:
    mov dl,bh
    shr dl,1
    shr dl,1
    shr dl,1
    shr dl,1

    cmp dl,9
    jbe num
    add dl,55d

    jmp display

num:
    add dl,30h

display:
    mov ah,02h
    int 21h
    rcl bx,1
    rcl bx,1
    rcl bx,1
    rcl bx,1

loop convert
    
end:
    mov ah,4ch
    int 21h
main endp
end main; 

This is my code but it prints the result in hex and I want to show it in decimal. How can I have it print the result in decimal?

Upvotes: 0

Views: 1041

Answers (1)

vitsoft
vitsoft

Reputation: 5775

You have to modify your convert routine to display decimal value of the character in BL. For instance by repeated division by 100, 10, 1 and using the remainder after the division:

 convert:        ; Display the ASCII character value in BL as a decimal number.
    MOVZX AX,BL ;
    MOV CL,100
    DIV CL      ; Unsigned divide AX by 100, with result stored in AL=Quotient, AH=Remainder.
    XCHG AX,DX  ; DL=Quotient, DH=Remainder.
    TEST DL,DL
    JZ Two      ; Skip the first leading digit if its 0.
    ADD DL,'0'  ; Convert binary digit 0..2 to character '0'..'2'.
    MOV AH,2
    INT 21h     ; Display the character from DL.
Two:MOVZX AX,DH ; Let AX=Remainder after the first division.
    MOV CL,10
    DIV CL      ; AL=Quotient, AH=Remainder.
    XCHG AX,DX  ; DL=Quotient, DH=Remainder.
    ADD DL,'0'  ; Convert binary digit 0..9 to character '0'..'9'.
    MOV AH,2
    INT 21h     ; Display the character from DL.
    MOV DL,DH   ; Remainder after the second division.
    ADD DL,'0'  ; Convert binary digit 0..9 do character '0'..'9'.
    INT 21h     ; Display the character from DL.
end:

Upvotes: 1

Related Questions