Converting a BCD number to its binary equivalent
In the following program example we will convert a BCD number to its binary equivalent.
Program source code
; Author: Evgeny Sergeevich Dovgopolov number: db 24h result: db 00h main: lxi sp, 0100h ; Initialize the stack pointer at address 0100h lxi h, number ; Load the address of the variable number into the HL register pair lxi d, result ; Load the address of the variable result into the DE register pair mov a, m ; Copy the memory value pointed to by the HL register pair into A call cvt8b_v ; Call the procedure cvt8b_v. The result is in A stax d ; Store the value in A at the memory address pointed to by DE hlt ; Stop program execution cvt8b_v: push h ; Push the value of the HL register pair onto the top of the stack push d ; Push the value of the DE register pair onto the top of the stack ani 0Fh ; Isolate the low nibble (LSB) mov c, a ; Copy the value of register A into register C mov a, m ; Copy the memory value pointed to by the HL register pair into A ani 0F0h ; Isolate the high nibble (MSB) rrc ; Rotate the value in register A one bit right without involving the CY flag rrc ; Rotate the value in register A one bit right without involving the CY flag rrc ; Rotate the value in register A one bit right without involving the CY flag rrc ; Rotate the value in register A one bit right without involving the CY flag mov d, a ; Save the second nibble (MSB) in register D xra a ; Clear register A mvi e, 0Ah ; Set the loop counter E to 10 SumLoop: add d ; Add the second nibble (MSB) 10 times. A + D dcr e ; Decrement the E counter register by 1 jnz SumLoop ; Jump to the label SumLoop until E becomes 0 add c ; Add the first nibble (LSB) to the second nibble (MSB) pop d ; Pop the value from the stack into the DE register pair pop h ; Pop the value from the stack into the HL register pair ret ; Return from the procedure back to the call site
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".