Arm/popcount

From ASMBits

arm/bswapPrevious

Write a function that returns the number of 1 bits in a given 32-bit integer (also known as population count).

int popcount (int n);

Expected solution length: Around 15 lines.

Sample Input

5
255

Sample Output

2
8

Make sure you test all of the corner cases (What's the smallest possible answer? Largest?).

Write your solution here

x
 
1
// A test case to test your function with
2
.global _start
3
_start:
4
    mov r0, #5
5
    bl popcount
6
    1: b 1b    // Done
7
8
// Only your function (starting at popcount) is judged. The test code above is not executed.
9
popcount:
10
    
11
    
Upload a source file...