Let R1 be the register with the bits from which we want to count the number of binary ones.
Let R2 be the register which contains the result after counting the number of ones.
Let R3 be the dummy variable.
Program:
// Initialize R2 to zero
MOV R2, #0
// Initialize R3 to zero
MOV R3, #0
// Right shift by 1 and the right-most bit will be un carry flag
L1: AND R3, R3, R1, LSR #1
// If the carry flag is set, R2 is added by one.
ADDCS R2, #1
// If R1==0 or not, update the status flags.
// Until R1==0, branch back to the L1 loop.
BEQ L1
Get Answers For Free
Most questions answered within 1 hours.