Signed vs. Unsigned Comparisons

The 6502 compare instructions are used to compare two 8-bit numbers. The result of the comparison is reflected in the processor’s status flags, specifically the carry (C), zero (Z), and negative (N) flags.

However, the compare instructions do not directly support signed or unsigned comparisons. Instead, you can interpret the results of the comparison based on the context of the values being compared.

Before diving into the examples, let’s briefly review the meaning of the status flags:

  1. Carry (C) flag: Set if A >= M (unsigned comparison), cleared otherwise.

  2. Zero (Z) flag: Set if A == M, cleared otherwise.

  3. Negative (N) flag: Set if the result of (A - M) has its most significant bit (bit 7) set, cleared otherwise. This flag is essentially a copy of the most significant bit of the subtraction result.

Now, let’s go through some examples of comparing different combinations of signed and unsigned values:

Example 1: Comparing unsigned values

A = 0x50 (80 in decimal)
M = 0x30 (48 in decimal)

CMP A, M

Result:

  • A >= M, so the carry flag is set (C = 1)

  • A != M, so the zero flag is clear (Z = 0)

  • The result of the subtraction (0x50 - 0x30 = 0x20) has the most significant bit clear, so the negative flag is clear (N = 0)

Example 2: Comparing signed values

A = 0xF0 (-16 in decimal, assuming two's complement representation)
M = 0x10 (16 in decimal)

CMP A, M

Result:

  • A < M (unsigned), so the carry flag is clear (C = 0)

  • A != M, so the zero flag is clear (Z = 0)

  • The result of the subtraction (0xF0 - 0x10 = 0xE0) has the most significant bit set, so the negative flag is set (N = 1)

Since we are comparing signed values, to correctly interpret the flags, we should check the N flag for less than (N = 1) or greater than or equal to (N = 0) and the Z flag for equal (Z = 1). In this case, A < M as N = 1, and they are not equal as Z = 0.

Example 3: Comparing a signed value with an unsigned value

A = 0xF0 (-16 in decimal, assuming two's complement representation)
M = 0x80 (128 in decimal)

CMP A, M

Result:

  • A >= M (unsigned), so the carry flag is set (C = 1)

  • A != M, so the zero flag is clear (Z = 0)

  • The result of the subtraction (0xF0 - 0x80 = 0x70) has the most significant bit clear, so the negative flag is clear (N = 0)

Comparing a signed value with an unsigned value can be tricky, as the flags might not directly indicate the correct result. In this case, you should treat both values as signed or unsigned, depending on the context. If both values were treated as signed, the result would be A < M, and if both values were treated as unsigned, the result would be A >= M. It is important to know the context and interpretation of the values being compared when using the CMP instruction with mixed signed and unsigned values.

Comments