asm_thumb2_compare.rst 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. Comparison instructions
  2. =======================
  3. These perform an arithmetic or logical instruction on two arguments, discarding the result
  4. but setting the condition flags. Typically these are used to test data values without changing
  5. them prior to executing a conditional branch.
  6. Document conventions
  7. --------------------
  8. Notation: ``Rd, Rm, Rn`` denote ARM registers R0-R7. ``imm8`` denotes an immediate
  9. value having a width of 8 bits.
  10. The Application Program Status Register (APSR)
  11. ----------------------------------------------
  12. This contains four bits which are tested by the conditional branch instructions. Typically a
  13. conditional branch will test multiple bits, for example ``bge(LABEL)``. The meaning of
  14. condition codes can depend on whether the operands of an arithmetic instruction are viewed as
  15. signed or unsigned integers. Thus ``bhi(LABEL)`` assumes unsigned numbers were processed while
  16. ``bgt(LABEL)`` assumes signed operands.
  17. APSR Bits
  18. ---------
  19. * Z (zero)
  20. This is set if the result of an operation is zero or the operands of a comparison are equal.
  21. * N (negative)
  22. Set if the result is negative.
  23. * C (carry)
  24. An addition sets the carry flag when the result overflows out of the MSB, for example adding
  25. 0x80000000 and 0x80000000. By the nature of two's complement arithmetic this behaviour is reversed
  26. on subtraction, with a borrow indicated by the carry bit being clear. Thus 0x10 - 0x01 is executed
  27. as 0x10 + 0xffffffff which will set the carry bit.
  28. * V (overflow)
  29. The overflow flag is set if the result, viewed as a two's compliment number, has the "wrong" sign
  30. in relation to the operands. For example adding 1 to 0x7fffffff will set the overflow bit because
  31. the result (0x8000000), viewed as a two's complement integer, is negative. Note that in this instance
  32. the carry bit is not set.
  33. Comparison instructions
  34. -----------------------
  35. These set the APSR (Application Program Status Register) N (negative), Z (zero), C (carry) and V
  36. (overflow) flags.
  37. * cmp(Rn, imm8) ``Rn - imm8``
  38. * cmp(Rn, Rm) ``Rn - Rm``
  39. * cmn(Rn, Rm) ``Rn + Rm``
  40. * tst(Rn, Rm) ``Rn & Rm``
  41. Conditional execution
  42. ---------------------
  43. The ``it`` and ``ite`` instructions provide a means of conditionally executing from one to four subsequent
  44. instructions without the need for a label.
  45. * it(<condition>) If then
  46. Execute the next instruction if <condition> is true:
  47. ::
  48. cmp(r0, r1)
  49. it(eq)
  50. mov(r0, 100) # runs if r0 == r1
  51. # execution continues here
  52. * ite(<condition>) If then else
  53. If <condtion> is true, execute the next instruction, otherwise execute the
  54. subsequent one. Thus:
  55. ::
  56. cmp(r0, r1)
  57. ite(eq)
  58. mov(r0, 100) # runs if r0 == r1
  59. mov(r0, 200) # runs if r0 != r1
  60. # execution continues here
  61. This may be extended to control the execution of upto four subsequent instructions: it[x[y[z]]]
  62. where x,y,z=t/e; e.g. itt, itee, itete, ittte, itttt, iteee, etc.