Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARM Assembler - How do I use CMP, BLT and BGT?

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
like image 795
user1080390 Avatar asked May 15 '12 13:05

user1080390


People also ask

How does CMP work in ARM assembly?

The CMP instruction subtracts the value of Operand2 from the value in Rn . This is the same as a SUBS instruction, except that the result is discarded. The CMN instruction adds the value of Operand2 to the value in Rn . This is the same as an ADDS instruction, except that the result is discarded.

How does BLT work in assembly?

The blt instruction compares 2 registers, treating them as signed integers, and takes a branch if one register is less than another. The move pseudo instruction moves the contents of one register into another register.

What is BLT instruction in ARM?

The BLT Instruction. BLT – Branch on Lower Than. The destination operand will be added to the PC, and the 68k will continue reading at the new offset held in PC, if the following conditions are met: The N flag is clear, but the V flag is set. The N flag is set, but the V flag is clear.


2 Answers

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)
like image 179
Leo Avatar answered Oct 11 '22 14:10

Leo


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.

like image 31
omotto Avatar answered Oct 11 '22 13:10

omotto