Hi All, I got the following code to subtract 2-byte numbers in pic 16f877a assembly
"
SUB16 MACRO DST, SRC
MOVF (SRC),W ; Get low byte of subtrahend
SUBWF (DST),F ; Subtract DST(low) - SRC(low)
MOVF (SRC)+1,W ; Now get high byte of subtrahend
BTFSS STATUS,C ; If there was a borrow, rather than
INCF (SRC)+1,W ; decrement high byte of dst we inc src
SUBWF (DST)+1,F ; Subtract the high byte and we're done
ENDM
"
the code works correctly until it reaches a borrow, since i tried to subtract 00005 from 65535 in a loop (doing a division), i was tracking the subtraction by printing each value to LCD
when program reaches 65280 (0xFF00) , the result of subtraction goes to 65531, I am trying to find the trick and solve it in the above code. can anybody help ?
; 16-bit Subtraction-with-Borrow
; SourceH:SourceL - Number to be subtracted
; Carry - NOT( Borrow to be subtracted )
; DestH:DestL - Number to be subtracted FROM
;Out DestH:DestL - Result
; Carry - NOT( Borrow result)
movf SourceL,W
subwf DestL
movf SourceH,W
btfss STATUS,C
incfsz SourceH,W
subwf DestH ;dest = dest - source, WITH VALID CARRY
;(although the Z flag is not valid).
; 16-bit Subtraction-with-Borrow
; SourceH:SourceL - Number to be subtracted
; Carry - NOT( Borrow to be subtracted )
; DestH:DestL - Number to be subtracted FROM
;Out DestH:DestL - Result
; Carry - NOT( Borrow result)
movf SourceL,W
subwf DestL
movf SourceH,W
btfss STATUS,C
incfsz SourceH,W
subwf DestH ;dest = dest - source, WITH VALID CARRY
;(although the Z flag is not valid).
It's stuck because I am doing a division loop, which calls subtract each time, when dividend reaches 0xFF00 and divisor is 0x0005 , the dividend value becomes 0xFFFB, that means the borrow is not handled correctly in the subtraction, here an infinite loop occurs
Sorry - is this 16-bit subtraction (as per the content of the OPs post and the replies) or 16-bit division (as per the title)?
There is a difference!
Susan
Sorry - is this 16-bit subtraction (as per the content of the OPs post and the replies) or 16-bit division (as per the title)?
There is a difference!
Susan
The code I provided is a subtraction code, but the whole process i am doing is division through subtraction, so i loop and subtract with incrementing a counter until i get a smaller number in dividend than the divisor
My problem is in the subtraction code, when i subtract 0x0005 from 0xFF00 , the result is set to 0xFFFB , which is not true at all
If this is an exercise to see how you can do division then fair enough.
If you just want the result, you can look at the code generated by the XC8 compiler for a 16-bit division.
Also the following might help: http://www.piclist.com/techref/microchip/math/div/index.htm. (Check out the tutorial) I found this using a Google search 2nd or 3rd response.
Susan
It's stuck because I am doing a division loop, which calls subtract each time, when dividend reaches 0xFF00 and divisor is 0x0005 , the dividend value becomes 0xFFFB, that means the borrow is not handled correctly in the subtraction, here an infinite loop occurs