16-bit binary to decimal

How can i convert 16-bit binary to decimal. So basically i want to put a 16-bit binary number, in lets say registry dx. Then i want to print that number, but the printed value should be in decimals. So how can i do that??? any ideas ?


  • This post has been deleted.
  • The way I learned how to do it is to divide your number by a decimal 10, push the remainder on the stack, increment a counter for digits pushed, check to see if the number is still nonzero, and repeat. Once your number has been divided down to zero, use the count to pop the digits off the stack one at a time, convert them to ASCII and print, and continue until the count is zero.

    The important thing to remember is that the remainders represent the decimal number *in reverse*. That's why pushing them on the stack is so handy in dealing with them. For example, if your number is decimal 10, you divide by 10 to get 1 with a remainder of 0. Push the remainder and divide by 10 again. This time you get 0 with a remainder of 1.

    Here's my TASM/MASM code:
    [code]WriteDecimal proc
    push ax
    push cx
    push dx
    push si
    mov ax,dx ;Assuming number to print starts in DX
    mov si,10 ;decimal 10
    xor cx,cx ;Initialize count at 0

    xor dx,dx ;Clear last remainder
    div si
    push dx ;Save digits in reverse order
    inc cx
    or ax,ax ;Is original number down to 0 yet?
    jnz NonZero ;No, continue looping

    mov ah,02h

    pop dx
    add dx,"0" ;Convert to ASCII
    int 21h ; and print
    loop WriteDigitLoop

    pop si
    pop dx
    pop cx
    pop ax

    WriteDecimal endp[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion