3

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?

Dylanerd
  • 43
  • 4
  • greater than is not less than or equal yes? the flag combination varies with unsigned vs signed as well so if you walk through the math, does z80 invert carry in to borrow on subtract, etc. but often it boils down to what you are looking at C or not C, N != V or N == V, etc. I would have to brush up on z80 but clearly there is an option there, just look. – old_timer Nov 05 '20 at 22:20
  • 1
    @Jester, not sure if you're speaking to something in particular about Z80, but doesn't swapping operands generally change `>` to `<` (and `>=` to `<=`) -- i.e. the equality condition doesn't invert with mere operand swap? To get `>` into `<=` test the opposite condition (logical negation, i.e. condition false, rather than operand swap) . – Erik Eidt Nov 06 '20 at 00:48
  • Oops. Yeah, my bad. Better delete that. – Jester Nov 06 '20 at 01:02
  • If you're using `C` to check for less than, then can we take it that you want to test for A greater than the operand, not the operand greater than A? – Tommy Nov 06 '20 at 15:05

1 Answers1

5

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:
      ...
Tommy
  • 99,986
  • 12
  • 185
  • 204