# ARM Loop Help

Page 1 of 1

## 2 Replies - 758 Views - Last Post: 20 May 2019 - 11:36 AM

### #1 Sisina_9898

Reputation: 0
• Posts: 5
• Joined: 16-May 19

# ARM Loop Help

Posted 16 May 2019 - 09:13 AM

Hello there!
I have a little task that I think I completed but I'm not quite sure that this is the right solution.
More specific i don't know if this is the correct way to go through the elements of the arrays. I will be happy if someone could help me
The task is : Write a program that returns in R0 the difference between the positive and the negative numbers in an array of words
with starting address R0 and the length of the array in R1.
This is what i have wrote:
MOV R2, #0  //positive
MOV R3, #0  //negative
MOV R4, #0  //iterator

L1:	CMP R4, R1        //checks if the iterator has reached the length of the array
BEQ L4            /if so jumps to L4
LDRB R5, [R0, R4] //R5 = R0[R4]
CMP R5, #0        //checks if R5 is greater or less than 0
BHI L2            //if greater jumps to L2
BLS L3            //if lower jumps to L3

L2:	ADD R2, R2, #1    //increment positive counter
B L1		  //and goes back to the beginning of the loop

L3:	ADD R3, R3, #1    //increment negative counter
B L1              //and goes back to the beginning of the loop

L4:	SUB R0, R2, R3   //subtraction of negative and positive numbers is stored in R0
BX LR

Is This A Good Question/Topic? 0

## Replies To: ARM Loop Help

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 2414
• Posts: 4,546
• Joined: 30-May 10

## Re: ARM Loop Help

Posted 17 May 2019 - 12:02 AM

So what's the problem?

Does it assemble?
Does it run, but crash?
Does it run, but produce the wrong answer?

What tests have you done?
What tests could we do (answer none, because you posted only a fragment).

Start with very simple arrays, like say a single element array containing a value.
Do you get that value returned.

### #3 turboscrew

• D.I.C Lover

Reputation: 172
• Posts: 1,114
• Joined: 03-April 12

## Re: ARM Loop Help

Posted 20 May 2019 - 11:36 AM

Looks good to me. And nicely commented - easy to read.
A small stylistic remark: you only need one comparison: if the number is not less, it must be higher or equal.
BTW, how do you classify zero?