2

I had worked out some code for my assignment and something tells me that I'm not doing it correctly.. Hope someone can take a look at it. Thank you!

 AREA   Reset, CODE, READONLY

  ENTRY

  LDR  r1, = 0x13579BA0
  MOV  r3, #0
  MOV  r4, #0
  MOV  r2, #8

Loop  CMP  r2, #0
  BGE   DONE

  LDR  r5, [r1, r4]
  AND  r5, r5, #0x00000000
  ADD  r3, r3, r5
  ADD  r4, r4, #4

  SUB  r2, r2, #1
  B    Loop
  LDR  r0, [r3]
DONE  B    DONE

  END

Write an ARM assembly program that will add the hexadecimal digits in register 1 and save the sum in register 0. For example, if r1 is initialized as follows:

        LDR    r1, =0x120A760C

When you program has run to completion, register 0 will contain the sum of 1+2+0+A+7+6+0+C.

You will need to use the following in your solution: · An 8-iteration loop · Logical shift right instruction · The AND instruction (used to force selected bits to 0)

I know that I did not even use LSR. where should I put it? I'm just getting started on Assembly hope someone makes some improvements on this code..

2Xchampion
  • 666
  • 2
  • 10
  • 24
  • You seem to have misunderstood how some instructions work. For example, your loop exit condition is all wrong. `r2` will be >= 0 on the very first iteration. And why are you trying to do `LDR r5, [r1, r4]` in the loop? That would read 4 bytes from memory, which is not something you'd want to do there. And what is `AND r5, r5, #0x00000000` supposed to achieve? ANDing something with 0 gives you 0 as the result. Go through the instruction set manual more closely until you feel that you're confident in your understanding of what the various instructions do. – Michael Oct 03 '15 at 07:37
  • Pity they're forcing you to use an 8-iteration loop - there's a much better/faster way that doesn't use a loop (e.g. `temp1 = (x & 0x0F0F0F0F) + ( (x >> 4) & 0x0F0F0F0F); temp2 = (temp1 & 0x00FF00FF) + ( (temp1 >> 8) & 0x00FF00FF); .... `). – Brendan Oct 03 '15 at 11:36

0 Answers0