assemblyz80

How do I do a "greater than" jump in Z80 Assembly, rather than "greater than or equal to"?


I'm trying to learn Z80 assembly - and forgive me if this is extremely obvious - but I'm rather new to assembly as a whole.

I have familiarised myself with how jumps work after making a comparison with cp and how they equate to things I know, that NZ is the equivilant of "!=", C correlates to "<" and so on. Though from as far as I've been able to see, ">" isn't as easy.

NC is the opposite of C, NC - as I understand - correlates to ">=" in my scenario. My assumption is that I can combine NC and NZ in the same jump condition to remove the "=" so to speak, but it doesn't seem to work.

What can I do to make my jump's condition be that a is more than the compared amount, without allowing them to equal zero?


Solution

  • CP performs a subtraction and sets the flags appropriately. It doesn't store the results of the subtraction.

    So to compare for the A greater than the operand, you need to look for a result of that subtraction that was a strictly positive number, i.e. it was 1 or greater.

    There's no direct route to that, you'll have to do it as a compound — NC to eliminate all results less than 0, getting you to greater than or equal, followed by NZ to eliminate the possibility of equality. But you might want to flip those for more straightforward code. E.g.

          CP <whatever>
          JR C, testFailed   ; A was less than the operand.
          JR Z, testFailed   ; A was exactly equal to the operand.
    
    testSucceeded:
          ; A was not less than the operand, and was not
          ; equal to the operand. Therefore it must have
          ; been greater than the operand.
          ...
    
    testFailed:
          ...