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

#1 Sisina_9898   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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]
	ADD R4, R4, #1    //i++
	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

Is This A Good Question/Topic? 0
  • +

Replies To: ARM Loop Help

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2414
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 172
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1