Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an ARM assembly language program that counts the number of 1’s for any val

ID: 3739910 • 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 move0x7E0C0A2B 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.

The only commands you can use are: [ADD,BEQ,BNE,CLZ,CMP,LDR,LSL,MOV]

Please help, thanks!

Explanation / Answer

  LDR r0, = 0x7E0C0A2B

    MOV r1, #0

    MOV r2, #0           

count:

MOVS r0, r0, LSR #1

    ADC r1,r1, r2                     

    CMP r0, #0

    BNE count

           

This code first initializes r0 with the number. Then we initialize 2 regsiters, r1 will contain final count i.e. number of 1's and is initialized to 0.

r2 is just used as dummy register in ADC instruction.

Loop:

- Right shift number by 1, LSR instruction copies LSB into carry flag

- ADC : count = count + r2 + carry bit => count = count + 0 + C here count will be increased by 1 if LSB was 1 and hence C bit was one, otherwise adding 0 will not affect the count

- Check if r0 is equal to 0, if yes then exit otherwise iterate over number again

           

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote