ARM

ARM assembly code to find number of ones and zeros in a 32 bit number

Hello guys! Today we will see how to find number of ones and zeros in a 32 bit number. Let’s first understand the algorithm later we see a example code.
Algorithm:

  1. Start
  2. Store a 32 bit number in register(Rd).
  3. Initialize a counter register to 1Fh(32)
  4. Initialize one regiter(Ra) to zero to store number of ones, and another register(Rb) to zero to store number of zeros.
  5. Right rotate the number through carry
  6. If carry is set
    • Increment Ra.
  7. Else
    • Increment Rb.
  8. Decrement counter value by 1.
  9. Repeat steps 5-8 till counter value reaches 32.
  10. Stop

Here is a example code to find number of ones and zeros in a 32 bit number
Code: