0

If I do CMP X1,X2, can I only use BGT,BLT,etc? Similarly, if I wanted to use SUBS for the same purpose, am I limited to using CBZ,CBNZ? e.g. SUBS X1,X1,X2, CBZ X1, Exit? Am I allowed to use BGT,BLT with SUBS and CBZ,CBNZ with CMP?

Nate Eldredge
  • 48,811
  • 6
  • 54
  • 82
Mike Tero
  • 1
  • 1
  • 1
  • The processors generally do not impose rules that limit the combination of instructions you can use, so if you can make use of some combination, you're free to use it. If you make the processor do extra work that you don't end up using, that's ok too. So, that means you can use `sub` to compute a difference that you don't use. – Erik Eidt Mar 01 '22 at 18:38
  • Sorry of course but I meant if you wanted to use that result to do something else like branch to something else. – Mike Tero Mar 01 '22 at 18:54
  • 2
    Flags are registers so, once set, will hold that value until changed by the program, like any other register, and main memory. Sourcing/reading/consulting registers generally doesn't *consume* them. – Erik Eidt Mar 01 '22 at 18:59
  • 1
    Not all instructions affect all flags, and some affect them unexpectedly. The various branch instructions can test one flag or combinations of flags. The way forward is to be armed with a reference that shows exactly which flags are set by each instruction, and which are tested. Which test/branch insructions you use also depends on whether the operands are signed or unsigned. Many processor instructions are sign-agnostic (it's just a binary operation) and it's up to you to test the appropriate flags. – Weather Vane Mar 01 '22 at 19:34
  • `cbz` and `cbnz` don't interact with flags at all; if you're using them you might as well us `sub` instead of `subs`. I'd suggest looking at compiler output for something with an `if(x>y)` ; `else if (x – Peter Cordes Mar 02 '22 at 09:58

0 Answers0