Write an ARM assembly language program that counts the number of 1’s for any val
ID: 3805078 • Letter: W
Question
Write an ARM assembly language program that counts the number of 1’s for any value in R0. The program must assemble/compile in KEIL and must be able to run in the KEIL simulator. Generally, R0 may contain any value, but for purpose of this exercise, you may move 0x2345ABCD into R0. The number in R0 does not need be preserved. You may use any other registers as you need. The result, total count of 1’s in R0, should be in R1 when the program ends.
What we need to do is count the number of 1's in any number given, in binary of course. So the above number is 0010 0011 0100 0101 1010 1011 1100 1101, and we should have 0x00000010 in R1 at the end, because there are 16 1's in the number above. I'm thinking there is some way using CLZ and LSL.
Explanation / Answer
CSEG AT 0H ; code segment
MOV R0,#0FH ; R0 have some value
MOV R1,#8 ;Counter for counting 1's
BACK:RLC R0 ;
JNC NXT ; jump if not carry
INC R5 ; increament the value of R5
NXT: DJNZ R1,BACK ; decreament by 1 , jump to BACK lable if not zero
END
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.