CMPSX

Instruction: Compare two signed values plus C.

CMPSX SValue1, < # > SValue2


Result: Optionally, equality and greater/lesser status is written to the Z and C flags.

  • SValue1 (d-field) is the register containing the value to compare with that of SValue2.
  • SValue2 (s-field) is a register or a 9-bit literal whose value is compared with SValue1.

Opcode Table:

–INSTR–  ZCRI –CON–    –DEST–         –SRC–

Z Result

C Result

Result

Clocks

 110001    000i    1111    ddddddddd    sssssssss

Z & (D = S+C)

Signed (D < S+C)

Not Written

4

Concise Truth Table:

In

Out

Destination

Source

Z

C

Effects

Destination1

Z

C

$0000_0003; 3

$0000_0002; 2

x

0

wr wz wc

$0000_0001; 1

0

0

$0000_0003; 3

$0000_0002; 2

0

1

wr wz wc

$0000_0000; 0

0

0

$0000_0003; 3

$0000_0002; 2

1

1

wr wz wc

$0000_0000; 0

1

0

$0000_0003; 3

$0000_0003; 3

0

0

wr wz wc

$0000_0000; 0

0

0

$0000_0003; 3

$0000_0003; 3

1

0

wr wz wc

$0000_0000; 0

1

0

$0000_0003; 3

$0000_0003; 3

x

1

wr wz wc

$FFFF_FFFF; -1

0

1

$0000_0003; 3

$0000_0004; 4

x

0

wr wz wc

$FFFF_FFFF; -1

0

1

$0000_0003; 3

$0000_0004; 4

x

1

wr wz wc

$FFFF_FFFE; -2

0

1

$8000_0000; -2,147,483,648

$7FFF_FFFF; 2,147,483,647

0

0

wr wz wc

$0000_0001; 1

0

12

$7FFF_FFFF; 2,147,483,647

$8000_0000; -2,147,483,648

0

0

wr wz wc

$FFFF_FFFF; -1

0

02

$8000_0000; -2,147,483,648

$0000_0001; 1

0

0

wr wz wc

$7FFF_FFFF; 2,147,483,6473

0

1

$7FFF_FFFF; 2,147,483,647

$FFFF_FFFF; -1

0

0

wr wz wc

$8000_0000; -2,147,483,6483

0

0

$FFFF_FFFE; -2

$FFFF_FFFF; -1

x

0

wr wz wc

$FFFF_FFFF; -1

0

1

$FFFF_FFFE; -2

$FFFF_FFFF; -1

x

1

wr wz wc

$FFFF_FFFE; -2

0

1

$FFFF_FFFE; -2

$FFFF_FFFE; -2

0

0

wr wz wc

$0000_0000; 0

0

0

$FFFF_FFFE; -2

$FFFF_FFFE; -2

1

0

wr wz wc

$0000_0000; 0

1

0

$FFFF_FFFE; -2

$FFFF_FFFE; -2

x

1

wr wz wc

$FFFF_FFFF; -1

0

1

$FFFF_FFFE; -2

$FFFF_FFFD; -3

x

0

wr wz wc

$0000_0001; 1

0

0

$FFFF_FFFE; -2

$FFFF_FFFD; -3

0

1

wr wz wc

$0000_0000; 0

0

0

$FFFF_FFFE; -2

$FFFF_FFFD; -3

1

1

wr wz wc

$0000_0000; 0

1

0

1 Destination is not written unless the WR effect is given.
2 The C flag result of CMPSX (Compare Signed, Extended) may differ from CMPX (Compare Unsigned, Extended) where the "interpreted sign" of Source and Destination are opposite. The first example in the second group, above, shows that CMPSX sets C because signed $8000_0000 (2,147,483,648) is less than signed $7FFF_FFFF (2,147,483,647). CMPX, however, would have cleared C because unsigned $8000_0000 (2,147,483,648) is not less than unsigned $7FFF_FFFF (2,147,483,647). The second example is the complementary case where the Source and Destination values are switched. Note that examples with differing Z and C are not shown but have expected effects similar to the other examples.
3 The examples of the third group, above, demonstrate cases where the comparison is properly reflected in the flags but the Destination Out has crossed the signed border (signed overflow error) in either the negative or positive direction. This signed overflow condition can not be reflected in the flags. If this condition is important to an application, it must detect it through other means.

Explanation

CMPSX (Compare Signed, Extended) compares the signed values of SValue1 and SValue2 plus C. The Z and C flags, if written, indicate the relative equal, and greater or lesser relationship between the two. The CMPSX instruction is used to perform signed multi-long comparison; 64bit comparisons, for example.

In a signed multi-long operation, the first instruction is unsigned (ex: CMP), any middle instructions are unsigned, extended (ex: CMPX), and the last instruction is signed, extended (ex: CMPSX). Make sure to use the WC, and optionally WZ, effect on all the instructions in the comparison operation.

For example, a signed double-long (64-bit) comparison may look like this:

cmp      XLow,     YLow     wc    wz    'Compare low longs; save C and Z
cmpsx    XHigh,    YHigh    wc    wz    'Compare high longs; save C and Z 

After executing the above, the C and Z flags will indicate the relationship between the two double-long (64-bit) values. If XHigh:XLow started out as $FFFF_FFFF:FFFF_FFFF (-1) and YHigh:YLow was $0000_0000:0000_0001 (1) the resulting flags would be: Z = 0 and C = 1; (Value1 < Value2). This is demonstrated below. Note that the comparison is really just a subtraction with the result not written; the Z and C flag result is important, however.

                         Hexadecimal            Decimal        Flags 
                       (high)    (low) 
  (XHigh:XLow)      $FFFF_FFFF:FFFF_FFFF                 -1     n/a 
- (YHigh:YLow)    - $0000_0000:0000_0001    -             1     n/a 
                  ----------------------    ---------------   -------- 
                  = $FFFF_FFFF:FFFF_FFFE    =            -2   Z=0, C=1

A signed triple-long (96-bit) comparison would look similar but with a CMPX instruction inserted between the CMP and CMPSX instructions:

cmp      XLow,     YLow     wc    wz    'Compare low longs; save C and Z
cmpx     XMid,     YMid     wc    wz    'Compare middle longs; save C and Z
cmpsx    XHigh,    YHigh    wc    wz    'Compare high longs; save C and Z 

For CMPSX, if the WZ effect is specified, the Z flag is set (1) if Z was previously set and SValue1 equals SValue2 + C (use WC and WZ on preceding CMP and CMPX instructions). If the WC effect is specified, the C flag is set (1) if SValue1 is less than SValue2 (as multi-long values).

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.