Mix (C++ and Assembly) Program to Convert Binary Number into BCD Format

#include<iostream.h>
#include<conio.h>

void main()
{
	int a;
	cout<<"\n Enter a binary number:";
	cin>>a;

	int t;

	//Converting first digit
	t=a/1000;
	a=a%1000;
	asm{
		mov ax,t
		mov bx,00h
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		add bx,ax
	}

	//Converting second digit
	t=a/100;
	a=a%100;
	asm{
		mov ax,t
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		add bx,ax
	}

	//Converting third digit
	t=a/10;
	a=a%10;
	asm{
		mov ax,t
		rol ax,1
		rol ax,1
		rol ax,1
		rol ax,1
		add bx,ax
	}

	//Converting fourth digit
	t=a;
	asm{
		mov ax,t
		add bx,ax
		mov a,bx
	}

	cout<<"\n BCD is:"<<a;
	getch();
}

/* Output

 Enter a binary number:36

 BCD is:54

*/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.