Mix (C++ and Assembly) Program to Count Number of 0’s and 1’s

#include<iostream.h>
#include<conio.h>
void main()
{
int ip,n0,n1;
cout<<"\n Enter Input:";
cin>>ip;
asm{
mov ax,ip
mov bx,00h
mov cx,10h
mov dx,00h
}
up:
asm{
rol ax,1
jc one
inc bx
jmp nxt
}
one:
asm{
inc dx
}
nxt:
asm{
dec cx
jnz up
}
asm{
mov n0,bx
mov n1,dx
}
cout<<"\n No of zeros:"<<n0;
cout<<"\n No of ones:"<<n1;
getch();
}
/* Output
 Enter Input:12
 No of zeros:14
 No of ones:2
*/

Leave a Reply

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