User Tools

Site Tools


base:16-bit_absolute_comparison

This is an old revision of the document!


16-Bit Absolute Value Comparison

Skate & Eins Method

N1 = 16-bit signed number at zeropage

N2 = 16-bit signed number at zeropage

Here we compare;

|N1| and |N2|

in other representation

abs(N1) and abs(N2)

So, if N1 = 2000, N2 = -3000, N2 should be bigger since we compare the distance from zero.

Please note that equality is neglected here. Equal absolute values may end up in any of the two conditions.

	lda N1+1
	eor N2+1
	bmi differentSigns
 
// sameSigns:
	lda N1
	cmp N2
	lda N1+1
	sbc N2+1
	eor N1+1
	bmi num1IsBigger
	jmp num2IsBigger
 
differentSigns:
	clc
	lda N1
	adc N2
	lda N1+1
	adc N2+1
	eor N1+1
	bmi num1IsBigger
 
num2IsBigger:
	// ...add your code here for |N1| < |N2|...
	jmp endOfAbsCompare
 
num1IsBigger:
	//...add your code here for |N1| > |N2|...
 
endOfAbsCompare:

TWW method including the zero flag:

/*! «»«»«»«»{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

base/16-bit_absolute_comparison.1603377471.txt.gz · Last modified: 2020-10-22 16:37 by tww