Search code examples
assemblyhexcharacterradixdata-conversion

Converting character to hex in assembler


So right now I'm working on reading in a single character from the keyboard and displaying its hex value. At the moment I believe I'm getting the correct hex value, but it is not being displayed correctly. For example, 'k' is 6F in hex, but it's displaying as '6;'. I know that the correction must be made in my DIS16 procedure, but I'm not sure when and what to do. If anyone can help, I'd really appreciate it.

DIS16   PROC

    mov cx, 0   


    mov bx, 16


numdiv:     

    mov dx, 0   


    div bx


    push dx


    add cx, 1


    cmp ax, 0   


    jne numdiv      


dispnum:


    pop dx


    add dl, 30h



    mov ah, 02h
    int 21h


    loop    dispnum

RET
DIS16   ENDP

Solution

  • Since the characters 'A' thru 'F' do not immediately follow '9', you have to adjust for that.

    ; as you have it
    pop dx
    add dl, 30h
    cmp dl, '9'
    jbe skip
    add dl, 7 ; bump up to 'A' - 'F'
    skip:
    ; print it... as you were