please dont rip this site

PIC Microcontoller Math Method


Two's complement 8 bit

from Andy Warren

If [...] your original number is in a register, you can do it with this bit of code:

        COMF    REG     ;REG = two's-complement of REG.
        INCF    REG     ;

or this one:

        COMF    REG,W   ;W = two's-complement of REG
        ADDLW   1       ;(REG is unchanged).

If the original number is in W, the easiest way is:

        SUBLW   0       ;W = two's-complement of W.

Clyde Smith-Stubbs [clyde at htsoft.com] of HI-TECH Software says

[These snippits do not handle the problem: REG = 0x80] because there is no correct result in this case. The value 0x80 interpreted as 2's complement is legal, and equal to -128. The negative of this, 128, is NOT representable as an 8 bit 2's complement number.

From http://www.myke.com/basic.htm: Negating the Contents of "w"

If you have to Negate the contents of the "w" register, you could use the code above (after saving the value in "w" into a register) or you could use the code below for low-end devices (PIC16C5x, PIC12C5xx, PIC16C505). Any file register can be used for this code because its contents are never changed.
  addwf  Reg, w           ;  w = w + Reg
  subwf  Reg, w           ;  w = Reg - w
                          ;  w = Reg - ( w + Reg )
                          ;  w = -w
              


file: /techref/microchip/math/neg/8bit.htm, 2KB, , updated: 2004/12/6 10:48, local time: 2008/11/22 02:09,
TOP NEW HELP FIND: 
38.103.63.59:LOG IN
©2008 PLEASE DON'T RIP! DO: LINK / DIGG! / MAKE!

 ©2008 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it!
<A HREF="http://piclist.org/techref/microchip/math/neg/8bit.htm"> PIC Microcontoller Math Method Two's complement 8 bit</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be reviewed) Just type in the box and press the Post button. (HTML welcomed!): A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Did you find what you needed?

  PICList 2008 contributors:
o List host: MIT, Site host massmind.org, Top posters @20081122 Apptech, Jinx, Xiaofan Chen, olin piclist, Vitaliy, William \Chops\ Westfield, Tamas Rudnai, JonnyMac, Alan B. Pearce, Gerhard Fiedler,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Shultz Electronics, Timothy Weber, on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
  'What can I do?' - SiCKO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .