Search code examples
assemblyarmkeil

When comparing numbers in ARM Assembly is there a correct way to store the value


so I'm making some code that is able to read through a list of numbers, divide it into blocks of 3 and decide which of those 3 numbers is biggest. Then I'm going to take the biggest values from each block of 3 and add them all together.

In order to do this I'm storing my values in several registers:

 MOV r1,#0 ;loop counter
 MOV r2,#0 ;compare store 1
 MOV r3,#0 ;compare store 2
 MOV r4,#0 ;compare store 3
 MOV r5,#0 ;sum of values
 MOV r6,#0 ;which was greater in value
 LDR r8,=data_values ;the list of values

I'm using the CMP command to compare the values, however I'm not sure entirely if my method is correct for storing and adding the values. At the moment I've got this:

MOV r6,CMP r2,r3 ;Moving the comparison value into r6, my store for the greater value
MOV r6,CMP r6,r4 ;Comparing the larger value to the 3rd value
ADD r5,r5,r6 ;Adding the larger value to the sum

This looks consistent with other functions that have worked for me before but I keep getting these errors:

task3.s(26): warning: A1865W: '#' not seen before constant expression

And

task3.s(26): error: A1137E: Unexpected characters at end of line

Now I'm pretty sure that this isn't a constant, unless constant is defined differently here and there's also no extra characters present at the end of the line, unless it's counting the entire compare function as the extra characters

Image showing the whole line highlighted with no extra characters sitting at the end

Is there anything I should change or should it run fine and these warnings be ignored?

Thank you


Solution

  • If you want to compute the maximum of two numbers, the standard approach in ARM assembly is something like this:

    cmp   r0, r1  @ if r0 > r1
    movgt r2, r0  @ then r2 = r0
    movle r2, r1  @ else r2 = r1
    

    To add the maximum of r0, r1, and r2 to r3 you might want something like this:

    cmp   r0, r1      @ if r0 < r1
    movlt r0, r1      @ then r0 = r1 (i.e. r0 = max(r0, r1))
    cmp   r0, r2      @ if r0 < r2
    movlt r0, r2      @ then r0 = r2 (i.e. r0 = max(max(r0, r1), r2))
    add   r3, r0, r3  @ r3 += r0
    

    implementing this such that you don't clobber any registers is left as an exercise to the reader.

    Always keep in mind that almost every instruction can be executed conditionally on ARM. This is where the whole power of the instruction set lies.