based on code from Rudy Wieser and hints from Dwayne Reid.
; warning: untested. ;---------------------------- ; 24-bit Subtraction-with-Borrow ; RH:RM:RL = Number to be subtracted ; DestH:DestM:DestL = Number to be subtracted FROM ;Out DestH:DestM:DestL = Result = Dest-R ; Carry = NOT( Borrow result) ; by David Cary 2001-06-26 ; based on code by Rudy Wieser (2000-02-17) movfw SourceL ; Lo byte subwf DestL,f movfw SourceM ;middle byte skpc incfsz SourceM,W subwf DestM,f movfw SourceH ; Hi byte skpc incfsz SourceH,W subwf DestH,f ;dest = dest - source, WITH VALID CARRY ;(although the Z flag is not valid). retlw 0 ;----------------------------
This ought to be easy to generalize to 32 bits or more.
http://piclist.com/techref/microchip/seepicsrc/psbpix/plus_minus.htm suggests negating one of the numbers and then adding. How would that compare to the code above ?
; warning: untested ; 24-bit Subtraction-with-Borrow ; RH:RM:RL = Number to be subtracted ; DestH:DestM:DestL = Number to be subtracted FROM ;Out DestH:DestM:DestL = Result = Dest-R ; Carry = NOT( Borrow result) ; by David Cary 2001-06-26 ; Some people believe that you have to add 1 after complementing ; to negate a number. Not in this case. ; first complement dest to get -dest-1 comf DestL,f comf DestM,f comf DestH,f ; then add R to get R-dest-1 ; From: Dwayne Reid ; http://piclist.com/techref/microchip/math/add/24b.htm movfw RL ; addwf Dest,F ;LS byte movfw RM ;middle byte skpnc incfsz RM,W addwf Dest,F ; movfw RH ;MS byte skpnc incfsz RH,W addwf Dest,F ; ; then complement dest again to get dest-R. comf DestL,f comf DestM,f comf DestH,f retlw 0
It looks pretty easy to compress the redundant bits of the above code ...
; by David Cary subtract_24: call negateDest call add24_R_to_Dest negateDest: comf DestL,f comf DestM,f comf DestH,f retlw 0If you're willing to give up a little speed to get more space, this looks like the most compact code (assumes you already have add24 subroutine).