Keil ARM7 Program That Searches An Array

2.9k views Asked by At

The program requires reading the elements of an array of 10 numbers and count the number of zeros in that array and store it in R7. Here's what I've developed so far...

    AREA addition, CODE, READWRITE
    ENTRY
    LDR R0,=ARR
    MOV R1, #0        ; Loop Iterator
    MOV R2, #0        ; Array Index
    MOV R7, #0        ; Number Of Zeros In The Array

LOP CMP R1, #10
    BEQ EXT
    LDR R3, [R0]
    CMP R3, #0
    BEQ MOVE1
    B CNT

MOVE1 ADD R7, R7, #1
      B CNT

CNT ADD R2, R2, #4
    ADD R1, R1, #1
    B LOP

ARR DCD 3,-5,-1,0,10,0,4,-8,7,6
EXT

    END

The problem is that it never enters MOVE1. I really cannot figure out why.

Thanks in advance.

2

There are 2 answers

0
Scott Hunter On BEST ANSWER

R0 never changes, so the value loaded into R3 never changes, so the test for your loop always comes out the same way. (And you don't need the B CNT after MOVE1, since that is the next instruction, anyway.)

1
Mike On

You're not using R2 to index the array so you are comparing the first value with zero ten times. I'm no ARM expert but, if you want to do that then you will have to use the equivalent syntax of LDR R3, [R0+R2].

In fact, why bother? Just add the 4 to the pointer (R0) instead of R2 and it always looks at the right place.

A couple of points:

BEQ MOVE1
B CNT

A conditional branch followed by an unconditional one means you've got the comparison wrong. (unless you have a massive block of code and need to go from relative to absolute addressing).

Don't count up, count down.

MOV R1, #10
...
SUB R1, #1
BNE LOP      ; Branches unless R1 is now zero

You might also find that LDR sets the zero flag (I've only ever come across one assembler that didn't but try it or look it up) and so there's no need to actually compare it to zero.

When you're working with assembler, you have to learn to think flexibly. A more compact version of your code might be:

ENTRY
    LDR  R0,=ARR
    MOV  R1, #10       ; Loop Iterator
    MOV  R7, #0        ; Number Of Zeros In The Array

LOP LDR  R3, [R0], #4  ; Get current number, adds 4 to R0 afterwards - points to next number
    CMP  R3, #0        ; Might not be necessary, see if it works without this
    BNE  CNT           ; Next loop if not zero

    ADD  R7, R7, #1    ; Add 1 to count then drop down

CNT SUBS R1, R1, #1   ; S suffix sets z flag (New one on me).
    BNE  LOP

ARR DCD 3,-5,-1,0,10,0,4,-8,7,6
EXT