Hey everyone, been using this site for a lot of my programming needs, but is time I would appreciate some help. Just started assembly language and I am having trouble with a problem.

The question: Write a program that will prompt the user to enter a number and then the program will determine the number of 1s that the binary representation of the number contains and display the result.

Example:

Enter in a number: 2
Number of 1s in this: 1

Enter in a number: 3
Number of 1s in this: 2

-------------------------------------------------

So some of the questions I have been considering...

Do I need to convert from decimal input to binary?
I know how to display the message and you need to use int 16h to enter in the decimal number right?

.model small
.stack
.data
message   db "Enter in a number:", "$"

.code

main   proc
   mov   ax,seg message
   mov   ds,ax

   mov   ah,09
   lea   dx,message
   int   21h

   mov   ax,4c00h
   int   21h
main   endp
end main

I am lost lol

Well the number is already represented in binary in the machine. You could convert it to an acii representation and check the string for ones but that is over complicating it. First off you need to check the number to see if it is a power of 2. If so there is only one "1" in the binary representation. For numbers that are not powers of 2 you need to find and algorithm to check how many powers of 2 make up the number and if there is a one left over. For instance, 12 contains two powers of 2. 4 and 8. There for 12 has 2 1s. 1010. 13 would have three 1011, 4 and 8 with a 1 left over. I think that would be the best way to go.

Write an assembly language program that will calculate the number of 1’s in the binary representation of last four digits of your roll number. For example if the binary representation of last four digits of your roll no. is 1101001100001 then the number of 1’s will be 6. The total number of 1’s should be stored in AX register. In this case, AX will contain 6 at the end of program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.