Parallax Forums
  HomeLog InRegisterCommunity CalendarSearch the ForumHelp
   
Parallax Forums > Public Forums > Propeller Chip > dealing with numbers that go negative (two's compliment)  Forum Quick Jump
 
New Topic Post Reply Printable Version
[ << Previous Thread | Next Thread >> ] | Show Newest Post First ]

James Long
Lil Brother SMT Assembly Services



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Aug 2004
Total Posts : 976
 
   Posted 2/26/2008 8:08 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
OK....I basically stick with Spin....because I just don't have time to learn PASM.
 
I've hit a problem, that until this point, I have not encountered.
 
A number that can go negative, and when it does, it is in two's compliment.
 
How does a programmer let the chip know if the items goes to two's compliment?
 
I'm sort of confused.....as how to prevent the program from thinking the number just jumped to some huge unreasonable number.
 
I'm a little rusty with respect to programming....so be easy on me.
 
James L


James L
Partner/Designer
Back to Top
 

Mike Green
Registered Member



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Oct 2004
Total Posts : 13558
 
   Posted 2/26/2008 8:17 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
Spin does all arithmetic in 32 bit signed arithmetic. All of the operators understand about negative numbers, so you don't have to tell the chip about anything. For a lot of the operators, the sign doesn't matter anyway like logical shifts or Boolean operations. Multiplication and division and the compare operators are signed operations. Things like addition and subtraction work the same whether signed or unsigned.
Back to Top
 

James Long
Lil Brother SMT Assembly Services



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Aug 2004
Total Posts : 976
 
   Posted 2/26/2008 8:25 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
Mike Green said...
Spin does all arithmetic in 32 bit signed arithmetic. All of the operators understand about negative numbers, so you don't have to tell the chip about anything. For a lot of the operators, the sign doesn't matter anyway like logical shifts or Boolean operations. Multiplication and division and the compare operators are signed operations. Things like addition and subtraction work the same whether signed or unsigned.
Mike,
 
Ok.....I see....so my little testing (Hyperterminal) is just a formating problem for the output for me.
 
Thanks for the reply......I didn't actually think about it that way.....but that makes sense.
 
I will have to put some led output and test the program to make sure the limit's are working right. I could go back and try to format the output....but it is not that important.
 
James L


James L
Partner/Designer
Back to Top
 

Paul Baker
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 6316
 
   Posted 2/26/2008 8:35 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
For data sizes less than 32 bits you can use the sign extend ~X for 8bit arithmetic and ~~X for 16 bit arithmetic to maintain proper execution on a 32 bit syatem.


Paul Baker

Propeller Applications Engineer

Parallax, Inc.

Back to Top
 

James Long
Lil Brother SMT Assembly Services



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Aug 2004
Total Posts : 976
 
   Posted 2/27/2008 12:30 PM (GMT -8)    Quote This PostAlert An Admin About This Post.

Ok

So I have the numbers shown on Hyperterminal.....but I would like to format them to be signed.

What is the best way to do this?  Just use the "~~" (the numbers are 16 bit)  ?

It is important, because these number will at some point be stored in a file, And I would like to get the formating issue worked out.

I'm not performing any calculation with the numbers at this point.....just recieving them from an accelerometer, and displaying them.

James L


James L
Partner/Designer
Back to Top
 

Paul Baker
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2004
Total Posts : 6316
 
   Posted 2/27/2008 4:30 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
Yes if it's 16 bit math, sign extend then send it via the DEC method, it will detect a negative and output the correct value.


Paul Baker

Propeller Applications Engineer

Parallax, Inc.

Back to Top
 

James Long
Lil Brother SMT Assembly Services



Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Aug 2004
Total Posts : 976
 
   Posted 2/27/2008 5:02 PM (GMT -8)    Quote This PostAlert An Admin About This Post.

Paul,

Ok....I'll modify my code....and see if I can get it to work out. If not...i'll be posting code here...to see if you guys can help out.

I've been spending too much time on the SMT business....and not on my projects. Oh well.....as life goes.

 

James L


James L
Partner/Designer
Back to Top
 

Rayman
Registered Member

Email Address Not AvailablePersonal Homepage Not AvailablePrivate Messaging Not AvailableAIM Not AvailableICQ Not AvailableY! Not AvailableMSN Not Available
Date Joined Jul 2007
Total Posts : 2408
 
   Posted 2/27/2008 5:05 PM (GMT -8)    Quote This PostAlert An Admin About This Post.
This is another area that is easy to miss in the manual and could be handled better by a compiler with a more C-like syntax (like UINT and INT) declarations.

As I understand it though:
All variables not declared as bytes or words are longs. All longs are signed.
If you RDbyte or RDword the source is treated as unsigned and the result is always positive.
Back to Top
 
[ << Previous Thread | Next Thread >> ]
New Topic Post Reply Printable Version
 
Forum Information
Currently it is Saturday, November 21, 2009 11:31 AM (GMT -8)
There are a total of 393,855 posts in 55,536 threads.
In the last 3 days there were 84 new threads and 706 reply posts. View Active Threads
Who's Online
This forum has 17692 registered members. Please welcome our newest member, old guy.
65 Guest(s), 13 Registered Member(s) are currently online.  Details
heater, Siri, Kenny Gardner, keith_kw, Jay Kickliter, mctrivia, Alsowolfman, Shawn Lowe, dMajo, Harprit, Beau Schwabe (Parallax), Leon, MicroDirk