9

Quick question for you guys, in my loop I need to use CMP , BLT and BGT to compare some values. How would use said instructions in the following loop?

I'm trying to use BGT , BLT and CMP as I need them to make my application work. The trouble is I have no idea how to use them. If I wanted to use CMP to compare r6, with r4 and put the difference into r7, how would I do this? The same question if I wanted to use BLT if r7 is less than 0, how would I do this?

  BGT ??????? ; branch if greater than 5
  CMP ???????? ; compare r6 with r4 , put difference into r7
  BLT ???????? ;branch if r7 is less than 0
  BGT ???????? ;branch if r7 is greater than 0

Here's my entire loop:

LoopStart

  BL WaitBUT1
  BL readTemp
  BL checkTemp
  BGT ??????? ; branch if greater than 5
  BL errorVal
  CMP ???????? ; compare r6 with r4 , put difference into r7
  BLT ???????? ;branch if r7 is less than 0
  BL FanOn
  BL errorLedOn
  BL systemLedOn
  BL heaterOn
  BGT ???????? ;branch if r7 is greater than 0
  BL FanOff
  BL errorLedOff
  BL systemLedOff
  BL heaterOff
  BL WaitBUT2
  BL FanOff
  BL errorLedOff
  BL systemLedOff
  BL heaterOff

  B LoopStart
user1080390
  • 451
  • 3
  • 8
  • 20

3 Answers3

13

You cannot do a conditional branch without first setting the condition register somehow. This can be done with cmp or by adding s to most instructions. Check out the ARM assembly documentation for details. Quick example:

Branch if r0 greater than 5:

cmp r0, #5 ;Performs r0-5 and sets condition register
bgt label_foo ;Branches to label_foo if condition register is set to GT

Compare r6 with r4 , put difference into r7, branch if r7 < 0:

subs r7, r6, r4 ;Performs r7 = r6 - r4 and sets condition register
blt label_bar ;Branches to label_bar if r7 < 0 (in which case r6 < r4)
Leo
  • 2,328
  • 2
  • 21
  • 41
  • Note that `bgt` / `blt` are *signed* compare conditions. The unsigned equivalents are `bhi` (higher) and `blo` (lower). See [Assembly code comparison fails for 0xFFE700DE >0xA](https://stackoverflow.com/q/23058040) / https://azeria-labs.com/arm-conditional-execution-and-branching-part-6/ – Peter Cordes Jan 12 '23 at 14:48
1

If I wanted to use CMP to compare r6, with r4 and put the difference into r7, how would I do this?

subs r7, r6, r4    /* r7 ← r6 - r4 */

The same question if I wanted to use BLT if r7 is less than 0, how would I do this?

bmi _exit          /* branch if r7 < 0 */

BMI (minus/negative) When N is enabled (N is 1) where N is a flag that will be enabled if the result of the instruction yields a negative number. Disabled otherwise.

Why subS instead of sub? Because S is an optional suffix that when is specified, the condition flags (like N) are updated on the result of the operation.

Regards.

omotto
  • 1,721
  • 19
  • 20
  • This is correct, but note that `bmi` is different from `blt` because the subtraction could have signed overflow. e.g. an 8-bit example: `-128 < 1` is true, but `-128 - 1` wraps to +127. The same problem can happen for 32-bit integers. But yes, if you want to know the sign of the truncated subtraction result, `subs`/`bmi` instead of `subs` / `blt`. – Peter Cordes Jan 12 '23 at 14:40
0

You should checkout the ARM documentation at (example for CMP documentation): http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0068b/CIHIDDID.html

From what can be read there what you are trying to do might require two instructions rather than just one (unless your ARM assembler does some special processing)

Kind regards,
Bo

Bo.
  • 2,547
  • 3
  • 24
  • 36