0

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?

J...
  • 30,968
  • 6
  • 66
  • 143
Ruschisb
  • 45
  • 6
  • 2
    Do you know how this code works? – Erik Eidt Jan 25 '22 at 15:31
  • What do you mean? – Ruschisb Jan 25 '22 at 15:50
  • There's nothing wrong with finding code on the internet as a starting point for something else, but it is somewhat improper to represent found code as your code, especially if you don't yet understand what it does in its current form. As it stands you're here looking for help with an assignment (e.g. please modify this found code for me), and if you represent to your uni that this (found code + modifications authored by strangers on the internet) is "your" work that would be improper. – Erik Eidt Jan 25 '22 at 19:24

1 Answers1

1

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:
vitsoft
  • 5,515
  • 1
  • 18
  • 31
  • Yeah but the instruction MOV AX,BL doesn't work since AX is 16 bit and BL 8 bit register,also i get no output, i also tried to divide by 10 until i reach 0 .. can you post the full code? – Ruschisb Jan 25 '22 at 18:42
  • @Ruschisb https://www.felixcloutier.com/x86/movzx – vitsoft Jan 25 '22 at 18:43