Author Topic: dspic assembly question regarding status word carry bit and subtraction  (Read 1734 times)

0 Members and 1 Guest are viewing this topic.

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 414
  • Country: us
trying to code the basic if statement in assembly in ASM30  for dspic30f2011

INC pulse_timing
MOV off_cnt ,W0
CP pulse_timing
BRA LTU loopend

    //  if (TRUE) code in here.


loopend:




datasheet says CP

""""Compute (f) – (WREG) and update the STATUS register. This instruction
is equivalent to the SUBWF instruction, but the result of the subtraction is
not stored.""""

datasheet says of BRA LTU

Condition = !C
If (Condition)
(PC + 2) + 2 * Slit16 ? PC
NOP ? Instruction Register




Now the SUBWF reference is wrong.  dspic30F has no such instruction.  I guess they mean SUB.

My question is what is the result of a SUB instruction on the C  ( carry bit ).  The data sheet can't be bothered to explain.

My guess is that if the SUB instruction results in a negative value then the C bit is set. 








 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: dspic assembly question regarding status word carry bit and subtraction
« Reply #1 on: December 17, 2016, 08:54:02 pm »
How subtract operations affect the Carry flag is described on page 17 of the sPIC30F/33F Programmer’s
Reference Manual:-

2.2.14.1 MCU ALU Status Bits
...When a subtract operation is performed, the C flag is used as a /Borrow flag.


So C will be set if the subtraction would not cause a borrow, ie. WREG is less than or equal to the value it is compared to.

 

Offline jpanhalt

  • Super Contributor
  • ***
  • Posts: 3466
  • Country: us
Re: dspic assembly question regarding status word carry bit and subtraction
« Reply #2 on: December 17, 2016, 09:49:39 pm »
I have not used that chip.  But, I have used enhanced mid-range and briefly looked at the Instruction Set for your chip.

I believe the comment you reference is made in reference to the 16F and 18F chips.  Yes, it would have been helpful if that point had been mentioned.

As for the Carry/Borrow bit, there appears to be the equivalent of subwfb instruction (i.e., subbr; see any 16F1xxx instruction set), which can save steps needed to deal with the carry/borrow bit in the Status register of the 8-bit chips. 

John
 

Offline snarkysparkyTopic starter

  • Frequent Contributor
  • **
  • Posts: 414
  • Country: us
Re: dspic assembly question regarding status word carry bit and subtraction
« Reply #3 on: December 17, 2016, 11:57:48 pm »
Found this answer on microchip forum.

""""There is actaully a reason for it in hardware.  A subtract is done by adding the complement.  This produces a carry on a subtract exactly when there is no borrow.
 
For example, for byte arithmetic 2 - 1 becomes 2 + 255, which produces a carry.  2 - 3 is 2 + 253 with does not produce a carry.  And that is the way it is set in a PIC. """""



It sounds correct to me.  Any thoughts ?

 

Offline Bruce Abbott

  • Frequent Contributor
  • **
  • Posts: 627
  • Country: nz
    • Bruce Abbott's R/C Models and Electronics
Re: dspic assembly question regarding status word carry bit and subtraction
« Reply #4 on: December 20, 2016, 05:11:07 am »
It sounds correct to me.  Any thoughts ?
Yes, that's it.


 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf