'unsigned 16-bit subtract'
I'm working on a sort routine, sorting 16 bit values that can span the
full "unsigned" range. To compare two values in the sort, I'm subtracting
one from another: if the result is negative, then the second is larger
than the first.
I started with the 16-bit subtract routine in Myke's book:
movf r2hi, w
subwf r1hi, w
movf r2lo, w
subwf r1lo, w
btfss status, c
; check for negative value
btfsc temphi, 7
but this is a "signed" routine in the sense that it looks at the high bit
of the result to see if it's negative, and because he does the
subtraction "backward", the czrry flag is useless. But I'm using full
"unsigned" 16 bit values, so for instance if I'm subtracting 0x0001 from
0x8001, the result will get reported as negative, since the high bit is
set, even though the number didn't "roll over."
What I want is a 16 bit subtract that will let me check the carry flag at
the end, to see if the number rolled over.
Here's what I think, and I'm looking for confirmation that this will
work: I can subtract the low bytes first, and if carry is set (result is
"negative") then decrement the high byte of the number I'm subtracting
FROM (basically perform the carry). Then I subtract the high bytes, and
if the carry is set after that, then the result is negative (meaning that
the number I'm subtracting is larger than the number I'm subtracting
from). That's the brute force and awkwardness approach, but it seems
logically sound to me. Did I miss something?
Even if that will work, is there a more elegant (read: faster) way to do
this? Somehow I suspect there is :-)
Thanks for any help,
What about inverting the number you are subtracting, then addig it to the numbe
you are subtracting from and looking for a carry?
At 10:00 02/12/99 -0800, Dave Johnson wrote:
>I'm working on a sort routine, sorting 16 bit values that can span the
>full "unsigned" range. To compare two values in the sort, I'm subtracting
>one from another: if the result is negative, then the second is larger
>than the first.
>I started with the 16-bit subtract routine in Myke's book:
maybe you can sort after the high byte first (ie. only 8 bit math), and
then sort after the low bytes within the groups with equal high byte.
depending on the numbers you have that can be faster.
part 0 741 bytes content-type:application/octet-stream;Good Luck!
Attachment converted: wonderland:test.asm (????/----) (00029F32)
Dave Johnson wrote:
> What I want is a 16 bit subtract that will let me check the carry flag at
> the end, to see if the number rolled over.
The standard subtract works. Carry is preserved at the end.
If carry flag is set, there is no borrow occured. So r1 is more than or
equal to r2.
More... (looser matching)
- Last day of these posts
- In 1999
, 2000 only
- New search...