Write an LC3 assembly language program that counts the numbe
Write an LC-3 assembly language program that counts the number of 1s in the value stored in R0 and stores the result in R1. For example, if R0 contains 0001001101110000, then R1should store the value 6.
Solution
.ORIG x3000
08.;initialise the registers we are using
09.LD R0, X
10.AND R1, R1, #0
11.AND R2, R1, #0
12.
13.;check if R0 is 0
14.LOOP ADD R0, R0, #0
15.BRz DONE
16.;count the bits
17.ADD R1, R1, #1
18.ADD R2, R0, #-1
19.AND R0, R0, R2
20.BRnzp LOOP
21.DONE HALT
22.;X .FILL 1370h ; 0001001101110000 answer should be 6
23.;X .FILL x5555 ; 0101010101010101 answer should be 8
24.X .FILL xEEBB ; 1110111010111011 answer should be 12
25.;X .FILL x882 ; 0000100010000010 answer should be 3
26.;X .FILL xFFFF ; 1111111111111111 answer should be 16
28..END
