Double Conditions in ARM assembly

后端 未结 3 1296
予麋鹿
予麋鹿 2020-12-21 22:41

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

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-21 23:36

    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.

提交回复
热议问题