Search code examples
assemblydosx86-16

Want Output in next line After inputing in the first line(Assembly Language)


I am making a program in assembly language to convert Decimal number (from user) into Hexa. I am almost completed it but i am facing a issue. Basically issue is that I am taking a input(Decimal) and than convert it into Hexa and printing it but my output replace the input value and showing me output in the same line. I need help to solve this issue. I also attached my code below.

.model small
.stack 100h
.data
Menu db 10, 13, 'Enter a choice (1 or 2):'
     db 10, 13, '1) Convert 1 to 5 Decimal values to Hex'
     db 10, 13, '2) Quit Program', 10, 13, '$'

MenuInputError db 10, 13, 'Choice must be a 1, 2, or 3!'
               db 10, 13, 'Try again!', 10, 13, '$'

Decimal db 10, 13, 'Enter a number with 1 to 5 digits: ', 10, 13, '$'

.code
    Main proc
    mov ax,@data
    mov ds,ax
    
    DisplayMenu:
    mov dx,OFFSET Menu
    mov ah,9
    int 21h
    mov ah,1
    int 21h
    
    CMP al,'1'
    JL DisplayError
    
    CMP al, '3'
    JG DisplayError
    
    CMP al,'1'
    JE Dec2Hex
    
    CMP al,'2'
    JE Quit
    
    DisplayError:
    mov dx, OFFSET MenuInputError
    mov ah,9
    int 21h
    JMP DisplayMenu
    
    Dec2Hex:
    CALL DEC2HEXA
    JMP DisplayMenu
    
    Quit:
    mov ah,4CH
    int 21h
    
    Main endp
    
DEC2HEXA proc
    mov dx,OFFSET Decimal
    mov ah,09h
    int 21h
    mov ax,0
    PUSH ax
    
    Again:
    mov ah,1
    int 21h
    
    CMP al,13 ; If Return is entered, start division.
    JE StartDivision
    
    CMP al,'0'
    JL Again
    
    CMP al,'9'
    JG Again
    
    mov ah,0
    SUB al,30h  ;30h = 48
    mov cx,ax
    POP ax
    
    mov bx,10
    MUL bx
    
    ADD ax,cx
    PUSH ax
    JMP Again
    
    StartDivision:
    mov cx,0
    mov bx,16
    POP ax
    
    Div1:
    div bx 
    PUSH dx 
    ADD cx,1 
    mov dx,0 
    CMP ax,0 
    JNE Div1
    
    HEXA:
    mov dx,0
    POP dx
    ADD dl,30h
    
    CMP dl,39h
    JG MoreHexa
    
    DisplayHexa:
    mov ah,2
    int 21h
    LOOP Hexa
    JMP Skip
    
    MoreHexa:
    ADD dl, 7h
    JMP DisplayHexa
    
    Skip:
    ret
    
    DEC2HEXA endp

End Main

input:
When I input:

Screen after Input:
After Input, How it shows result


Solution

  • mov ah,1
    int 21h
    CMP al,13 ; If Return is entered, start division.
    JE StartDivision
    

    The Enter that finishes the input loop, brings the cursor to the start of the current line, where the inputted decimal number starts. Next your code immediately converts and displays the hexadecimal characters. This will overwrite the inputted decimal number. The first three characters in "2414" are overwritten by the three characters in "96E", leaving the fourth character "4" as it was.

    Just print an additional linefeed as soon as the input loop stops:

    StartDivision:
      mov dl, 10   ; Linefeed
      mov ah, 02h  ; DOS.PrintCharacter
      int 21h
      ...