“Write an ARM assembly language program that counts the number of 1’s for any va
ID: 3805289 • Letter: #
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. You CAN USE ONLY the following commands: LDR, MOV, CMP, BEQ, CLZ, ADD, LSL, BNE. You MUST ALSO USE A LOOP."
(PLEASE WRITE THE ARM ASSEMBLY CODE FOR IT! I've asked this question already, but the person who answered it didn't give the ARM assembly code!)
Explanation / Answer
Please find the ARM assembly code to count the number of one's for any value in R0. The comments are provided for understanding the code.
mov r2, #1 /* index is equal to 1 */
mov r1, #0 /* count is equal to 0 */
.loop: /* loop */
and r3, r0, #1 /* Take the LSB and compare */
cmp r3, #1
addeq r1, r1, #1 /* increment the counter */
mov r0, r0, lsr #1 /*the next iteration in loop */
add r2, r2, #1
cmp r2, #32 /* loop condition */
ble .loop
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.