Most of the time you would only translate a binary number into a binary coded decimal number for outputting the text version of that number. Unlike the old world 6502 microprocessor there is no binary coded decimal switch in the flags register of the PC and the floating point maths co-processor which is now built into the PC processor uses IEEE format and is still binary. Making it impossible to do actual binary coded decimal mathematics using the PC. The translation of binary to binary coded decimal and then to ASCII text is done for you when using high level languages such as C or C++, BASIC and so on. You would only need to do this if you were programming the computer in Assembler (machine code), in which case you divide the binary number by 1010 binary (HA0 hexidecimal, 10 decimal) and store the remainder in to memory, this would have to be done several times to translate the whole of the binary number in to binary coded decimal. Before doing so and assuming the binary number is an integer (a positive/negative number) the binary number would have to be made positive first.
In 8086 assembler lanuage it would be written something like this:
Put a space character in to the first byte of the variable called textnumber, the space is ASCII character 32
move AL,32
mov [textnumber],AL
Move into the AX register the binary number
mov AX, Binary_number
Check for negative number, if the value of the 16 bit number is 32768 or greater then the most signicant bit is high which means the number is negative
cmp AX,32768
jl means jump if less meaning the value in the AX register is lest than (in this case) 32768.
jl number_is_positive
Negate the number, a negative number becomes positive, neg is the same as multipling a number by -1
neg AX
Push the AX register onto the stack and load the AL register with the minus sign and store the AL into the first byte if the textnumber variable.
push AX
mov AL,'-
mov [textnumber],AL
Retrive the binary number from the stack
pop AX
Here the translation is done, the maximum numer of decimal digits that a 16 bit number is translated into is 5, -32768 to 32767 for integer numbers and 0 to 65535 for positive only numbers
:number_is_positive
Move into the SI register the number 5
mov SI,5
Move into the BX register the value of 10 decimal
mov BX,10
:loophere
Divide the AX register by the value in the BX register (10 decimal)
div BX
The modulas or remainder is stored in the DX register, we are only interested in the lower byte of the DX register (DL), the remainder of a divide by 10 can only be a value from 0 to 9.
You don't have to do this, but to print the answer to the screen you will need to translate the value in the DL register to ASCII text, the ASCII code for the numbers 0 to 9 is H30 to H39. The lower nybble is 0 to 9 and the upper nybble is 3 in hexdecimal or 48 decimal. Thus OR in the hexdecimal number H30 to the value in the DL register.
or DL,H30
Store the result into the variable textnumber with increment form start of textnumber memory location
mov [textnumber+SI],DL
'Decement the SI register by 1 and jump not equal to zero back to the label loophere
dec SI
jne loophere
Return from subroutine
ret
Alocated 6 bytes of memory to store, the first byte will have a space or minus sign stored in it, the rest will have ASCII text for the decimal number.
textnumber db 0,0,0,0,0,0
You may want or need to write a subroutine to get rid or the leading zeros. If you don't want to translate into ASCII text then do not OR the hex value 30 into the DL register and write a subroutine to compact the answer so 2 decimal digits are store in one byte.
In short, translating binary in to binary coded decimal you divide the number by decimal 10 and store the remainder in a right to left fashion. Not unlike converting a decimal number in to binary where you would divide the decimal number by 2 until there is no number left to divide and write the remainders down in a right to left fashion IE 25 in binary is
25/2 = 12 remainder 1
12/2 = 6 remainder 0
6/2 = 3 remainder 0
3/2 = 1 remainder 1
1/2 = 0 remainder 1
25 decimal = 11001 binary.
Just in case you don't know this.
AX register is the 16 bit Accumulator Register.
AL register is the lower byte of the AX register.
DX register is the 16 bit Data Register.
DL register is the lower byte of the DX register.
BX register is the Base register.
SI register is the Source Index register.