base:8-bit_ranged_comparison
Differences
This shows you the differences between two versions of the page.
base:8-bit_ranged_comparison [2015-04-17 04:30] – external edit 127.0.0.1 | base:8-bit_ranged_comparison [2015-04-22 20:15] (current) – ftc | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Range Checking a Byte ====== | ||
+ | ===== Approach by White Flame ===== | ||
+ | |||
+ | In checking for the range [x,y), instead of performing 2 comparisons the idea is to subtract x to align the range to [0, | ||
+ | |||
+ | Any numbers lower than the original range will have wrapped the byte into the high values <=255, and any number higher than the original range will still be too large. | ||
+ | |||
+ | <code asm> | ||
+ | ; Check .A for the range [10,100) | ||
+ | sec | ||
+ | sbc #10 ; start of the range | ||
+ | cmp #90 ; length of the range | ||
+ | bcs fail ; result needs to be 0-89 to pass the original 10-99 check | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ===== Approach by Lee Davison ===== | ||
+ | |||
+ | For all of these we assume that the byte to be tested is in A and that the start and end values, n and m, are already defined. Also that 0 < n < m < $FF. | ||
+ | |||
+ | If you don't need to preserve the byte in A then testing the byte can be done in five bytes and only six cycles. This sets the carry if A is in the range n to m. | ||
+ | |||
+ | <code asm> | ||
+ | CLC ; clear carry for add | ||
+ | ADC # | ||
+ | ADC # | ||
+ | </ | ||
+ | |||
+ | Obviously, if the state of the carry flag is always known before executing this code, you can skip the CLC as well and adjust the routine accordingly. |