1

I am very new to ARM and doing an assignment for a class. What I am confused about is a double condition like, if (x > 0 && x < 100) do something.

What I am trying to get done is check to see if in an address memory, if the information is a upper or lower case character.

I found a link on here but it is not for ARM and doesn't seem like it can help me.

I tried something like:

BGT r2, #0x60

BLT r2, #0x7B

But is throwing errors about expecting an address expression so I am assuming you can not compare a value stright to an int.

Edited, fixed my condition, was typing too fast and put wrong GT LT arrows

Anthony Taylor
  • 3,073
  • 3
  • 21
  • 30
  • OK, so if you know how to do a single condition, you can reword your condition like `if (x > 0) { if (x < 100) { /* code here */ } }` – Blorgbeard Nov 05 '13 at 03:48
  • no difference between one, two, three or a dozen conditions in a row, they all implement the same as one condition. Just do one at a time, dont worry about tricks to do more than one (except for of course greater than OR equal type stuff which is really a single condition). – old_timer Nov 05 '13 at 19:26
  • more important question has to do with managing unsigned vs signed, that is the hard(er) part, more likely to be done wrong. – old_timer Nov 05 '13 at 19:27
  • `bgt` is a branch and only takes one **PC** relative offset or a register. Did you mean `mov` (conditionally set a register) or did you want a branch? My answer assumed you meant `mov` or some other non-branch instruction. – artless noise Nov 20 '13 at 16:37

3 Answers3

3

What I am confused about is a double condition...

You can use a compiler with optimizations and a disassembler to see how it implements this. With the GNU tools, this is gcc and objdump -S.

if (x > 0 && x < 100)

You need assembler that checks both conditions and sets a flag. Say the value x is in r0.

  sub r1, r0, #1    ; change zero test to minus.
  cmp r1, #98       ; allow equal.
  ; condition 'ls' (lower and same) for true
  movls r2, #42     ; set r2 to 42 if(x > 0 && x < 100)
  ; condition 'hi' (high unsigned) opposite flags
  movhi r2, #24

This is typical of any CPU. The compiler translates the tests to something that maps better to the underlying instruction set. Your example tests have different operators.

It is easier to understand some code that needs less concepts,

  if(x > 0 && y > 0)

This translates to something more straight forward in assembler,

  cmp r0, #0
  cmpgt r1,#0
  movgt r0,#42 ; condition passed (here with signed conditions).

It is more straight forward if the tests are for the same conditions (usually the same 'C' operators) as the condition codes allow a conditional compare in these cases.

artless noise
  • 21,212
  • 6
  • 68
  • 105
  • A typical rule of thumb ARM is that about three conditional instruction is about equal to a branch. Active profiling is always better. – artless noise Nov 05 '13 at 17:12
2

You can't do the comparison and branch together in one instruction like that. You need to use separate compare and branch instructions. (ARM does have a "compare and branch on zero," but that does not apply here.)

You want something more like:

    CMP r2, #100
    BGT label_out
    CMP r2, #0
    BLT label_out  

... do stuff

label_out:  ; the branches come here if R2 < 0 || R2 > 100
Joe Z
  • 17,413
  • 3
  • 28
  • 39
  • What do i use if I want it to go to label_out only if its && and not || – Anthony Taylor Nov 05 '13 at 04:09
  • I wrote an extensive tutorial on conditional branches in assembly language for another processor here: http://wiki.intellivision.us/index.php?title=Introducing_the_Instruction_Set_Part_3 It turns out that the ARM assembly language and this processor's assembly language are very similar. Just replace `CMPR` and `CMPI` with `CMP` in most places, and swap the argument order (ie. change `#123, R2` to `R2, #123`). Otherwise, it's all the same basic principles. – Joe Z Nov 05 '13 at 06:52
  • You probably want to jump to the 'If-Then' section here: http://wiki.intellivision.us/index.php?title=Introducing_the_Instruction_Set_Part_3#If-Then For your compound && condition, you want to skip to the `else` if either condition tests false. For a compound || condition, you want to jump to the `then` if either condition tests true. – Joe Z Nov 05 '13 at 06:54
0

You need to perform a comparison before using BLT or BGT. This thread: ARM Assembler - How do I use CMP, BLT and BGT? seems to have the answer you are seeking.

Community
  • 1
  • 1
It'sPete
  • 5,083
  • 8
  • 39
  • 72