Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

division of 16-bit numbers in pic16f877a assembly

Status
Not open for further replies.

salamah99

Newbie level 5
Newbie level 5
Joined
Jul 24, 2023
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
81
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 ?
 

Code:
; 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).
 

Code:
; 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).
This stuck in infinite loop (I tried it), which means the process is not dealing with borrow correctly,
 

How can it be stuck in an infinite loop as there are no loops? I suspect that the problem is with your testing.
 

How can it be stuck in an infinite loop as there are no loops? I suspect that the problem is with your testing.
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
 

How can it be stuck in an infinite loop as there are no loops? I suspect that the problem is with your testing.
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

The subtract routine I posted is the standard PIC 16 bit subtract routine that has been used millions of times before.

Don't forget that the borrow flag is inverted so that the carry flag is zero when there is a borrow and a one when there is no borrow.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top