User Tools

Site Tools


base:16-bit_comparison

16-Bit Comparison

TWW Method

16 bit equivalent of the CMP OPC:

/*! «»«»«»«»{CMP16}«»«»«»«»
    Does exactly the same as CMP of two values (effectively its a val1-val2) and sets the flags as follows:

                       (BCC/BCS)      (BEQ/BNE)      (BMI/BPL)
    If val1 = val2 : Carry =  SET   Zero =  SET   Negative = CLEAR
    If val1 > val2 : Carry =  SET   Zero = CLEAR  Negative = CLEAR
    If val1 < val2 : Carry = CLEAR  Zero = CLEAR  Negative =  SET
*/

    lda val1
    sec
    sbc val2
    php
    lda val1+1
    sbc val2+1
    php
    pla
    sta temp
    pla
    and #%00000010
    ora #%11111101
    and temp
    pha
    plp


Somewhere on ZP:
temp:
    .byte $00

Different approach: instead of setting flags, goes to different destinations:

; Val1 ≥ Val2 ?
  LDA Val1 +1    ; high bytes
  CMP Val2+1
  BCC LsThan     ; hiVal1 < hiVal2 --> Val1 < Val2
  BNE GrtEqu     ; hiVal1 ≠ hiVal2 --> Val1 > Val2
  LDA Val1       ; low bytes
  CMP Val2
  ;BEQ Equal     ; Val1 = Val2
  BCS GrtEqu     ; loVal1 ≥ loVal2 --> Val1 ≥ Val2
LsThan
...
GrtEqu
...
base/16-bit_comparison.txt · Last modified: 2021-09-13 17:23 by tww