In this blog post, we’ll explore how to convert a BCD (Binary-Coded Decimal) number into its binary equivalent using an 8086 assembly language program. The following code snippet demonstrates this process:
DATA SEGMENT NO1 DB "9036" D2 DB ? D1 DB 16 DUP(?) DATA ENDS CODE SEGMENT ASSUME CS:CODE, DS:DATA START: MOV AX, DATA MOV DS, AX LEA SI, NO1 LEA DI, D1 MOV CX, 04H TOP: MOV AL, [SI] MOV DX, 0CH UP1: ROL AX,1 DEC DX JNZ UP1 AND AX, 1111000000000000B MOV DX, 04H UP2: ROL AX, 1 JNC DN MOV BX, 1 MOV [DI], BX JMP DN2 DN: MOV BX, 0 MOV [DI], BX DN2: INC DI DEC DX JNZ UP2 INC SI DEC CX JNZ TOP INT 3 CODE ENDS END START
Step-by-Step Explanation:
1. Data Segment:
NO1
: Defines a string representing the BCD number “9036”.D2
: Reserves a byte to store the final binary result.D1
: Reserves 16 bytes to store intermediate binary digits.
2.Code Segment:
Initialization:
MOV AX, DATA
: Loads the address of the data segment into the AX register.MOV DS, AX
: Sets the DS register to point to the data segment, allowing access to variables.LEA SI, NO1
: Loads the address of the BCD stringNO1
into the SI register.LEA DI, D1
: Loads the address of theD1
array into the DI register, which will store the binary digits.MOV CX, 04H
: Sets the loop counter CX to 4, corresponding to the number of BCD digits.
Loop to Process Each BCD Digit:
MOV AL, [SI]
: Loads the current BCD digit from the string into the AL register.MOV DX, 0CH
: Sets DX to 12, preparing for 12-bit rotation.
Rotate and Mask:
UP1:
Label marking the start of the rotation loop.ROL AX,1
: Rotates the bits of AX to the left by one position.DEC DX
: Decrements DX.JNZ UP1
: If DX is not zero, repeat the rotation.AND AX, 1111000000000000B
: Masks the upper 4 bits of AX to isolate the BCD digit.
Convert to Binary:
MOV DX, 04H
: Sets DX to 4, preparing to process 4 bits.UP2:
Label marking the start of the bit processing loop.ROL AX, 1
: Rotates the bits of AX to the left by one position.JNC DN
: If the carry flag is not set (indicating the bit was ‘0’), jump to theDN
label.MOV BX, 1
: If the carry flag is set (indicating the bit was ‘1’), set BX to 1.MOV [DI], BX
: Store BX (1) into the current position of theD1
array.JMP DN2
: Jump to theDN2
label to continue.DN:
Label reached when the bit is ‘0’.MOV BX, 0
: Set BX to 0.MOV [DI], BX
: Store BX (0) into the current position of theD1
array.DN2:
Label to continue the loop.INC DI
: Move to the next position in theD1
array.DEC DX
: Decrement the bit counter.JNZ UP2
: If DX is not zero, repeat the bit processing loop.
Prepare for Next BCD Digit:
INC SI
: Move to the next BCD digit in the string.DEC CX
: Decrement the digit counter.JNZ TOP
: If CX is not zero, repeat the digit processing loop.
Program Termination:
INT 3
: Generates an interrupt 3, which typically halts the program execution.
Overall Process:
- The program initializes registers and sets up pointers to the BCD string (
NO1
) and the destination array (D1
). - It processes each BCD digit from the string by isolating the relevant four bits using bitwise operations and rotations.
- For each bit in the BCD digit, it checks if the bit is ‘1’ and stores the corresponding binary value in the destination array (
D1
). - After processing four bits, it moves to the next BCD digit and repeats the process.
- This process continues for all BCD digits in the string.
- The program terminates after converting all BCD digits into their binary equivalents.
Output
C:\TASM>MASM BCDBIN.ASM Microsoft (R) Macro Assembler Version 5.00 Copyright (C) Microsoft Corp 1981-1985, 1987. All rights reserved. Object filename [BCDBIN.OBJ]: Source listing [NUL.LST]: Cross-reference [NUL.CRF]: 50324 + 450332 Bytes symbol space free 0 Warning Errors 0 Severe Errors C:\TASM>LINK BCDBIN.OBJ Microsoft (R) Overlay Linker Version 3.60 Copyright (C) Microsoft Corp 1983-1987. All rights reserved. Run File [BCDBIN.EXE]: List File [NUL.MAP]: Libraries [.LIB]: LINK : warning L4021: no stack segment C:\TASM>DEBUG BCDBIN.EXE -G AX=0006 BX=0000 CX=0000 DX=0000 SP=0000 BP=0000 SI=0004 DI=0015 DS=0B97 ES=0B87 SS=0B97 CS=0B99 IP=0039 NV UP EI PL ZR NA PE NC 0B99:0039 CC INT 3 -D 0B97:0000 0B97:0000 39 30 33 36 00 01 00 00-01 00 00 00 00 00 00 01 9036............ 0B97:0010 09 00 03 06 00 00 00 00-00 00 00 00 00 00 00 00 ................ 0B97:0020 B8 97 0B 8E D8 8D 36 00-00 8D 3E 05 00 B9 04 00 ......6...>..... 0B97:0030 8A 04 BA 0C 00 D1 C0 4A-75 FB 25 00 F0 BA 04 00 .......Ju.%..... 0B97:0040 D1 C0 73 08 BB 01 00 89-1D EB 06 90 BB 00 00 89 ..s............. 0B97:0050 1D 47 4A 75 EB 46 49 75-D7 CC 26 80 7F 0A 00 74 .GJu.FIu..&....t 0B97:0060 3E 8B 46 08 8B 56 0A 89-46 FC 89 56 FE C4 5E FC >.F..V..F..V..^. 0B97:0070 26 8A 47 0C 2A E4 40 50-8B C3 05 0C 00 52 50 E8 &.G.*[email protected]. -Q
Understanding the Memory Dump
The memory dump displayed in the DEBUG session shows the contents of memory after running the program. Let’s analyze the key parts.
Memory Address | Value (Hex) | Binary Representation | Decimal Equivalent (BCD Digit) | Explanation |
---|---|---|---|---|
0B97:0000 to 0B97:000F | 39 30 33 36 | 1001 0000 0011 0110 | N/A | The input BCD string "9036" stored in memory |
0B97:0010 | 09 | 0000 1001 | 9 | First BCD digit (converted from BCD 9 ) |
0B97:0011 | 00 | 0000 0000 | 0 | Second BCD digit (converted from BCD 0 ) |
0B97:0012 | 03 | 0000 0011 | 3 | Third BCD digit (converted from BCD 3 ) |
0B97:0013 | 06 | 0000 0110 | 6 | Fourth BCD digit (converted from BCD 6 ) |
hi Ankur,
thanks for your efforts to give so many assembly programs.
can i request you to please add output printing code to each too.
Hello Shridhar, Thank you for your kind words! I’m glad you find the assembly programs helpful. That’s a great suggestion—I’ll work on adding output printing code to each program to make them more complete. Stay tuned for updates!