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
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.
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
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.