BGE Instruction ARM

12,172

Solution 1

I think you're misinterpreting the way that CMP works with the 'comparison' variants of the condition codes. When you read a CMP followed by a conditional instruction, in your head, move the condition code to in between the two arguments to CMP. Hence

CMP r0, r1
BGE label

means "branch if r0 is greater than or equal to r1". In your case r0 is 3 and r1 is 0x8F (143) and so clearly the branch will not take place.

Solution 2

Bear in mind that BGE is a signed comparison, so in your example it will not branch, given that the first operand(r0) is not bigger or equal than the second one(r1).

LDR r0,=0X3       ; load 0X00000003
LDR r1,=0X8F      ; load 0x0000008F
CMP r0,r1         ; (r0 - r1) and sets the condition register
BGE a_label       ; Conditional branch which checks if it was greater or equal (False)
SUBS r1,r1, #0XC9

For the sake of completeness and as Michael has suggested, it is more common (or recommended) the use of MOV r0,#0X00000003 instead of LDR r0,=0X00000003 to load constants (most probably the generated file will be exactly the same) See this.

Share:
12,172

Related videos on Youtube

MangoKitty
Author by

MangoKitty

Updated on June 04, 2022

Comments

  • MangoKitty
    MangoKitty almost 2 years

    This test asks to branch under the condition 'BGE' branch to a label. The values stored in my registers being compared are:

    LDR r0,=0X3
    LDR r1,=0X8F
    CMP r0,r1
    BGE a_label
    SUBS r1,r1, #0XC9
    

    I expected it to branch but somehow 0X8F isn't greater than 0X3. Emulating my code on Keil proves this.

    Im wondering if anybody knows why it doesnt branch and how 0X8F is read as being greater than 0X3!

    Thank you so much <3

    • Michael
      Michael over 5 years
      Why would 3 be >= 0x8F..? Were you expecting sign-extension? Also, why are you not just using mov instead, since you're dealing with 8-bit immediates?