signed byte and signed word addition (negative numbers subtract?)
![Zoot](https://forums.parallax.com/uploads/userpics/559/nW4UO23UEIWZ4.png)
Will this work? I want to take a signed byte (-127 to 127) and convert to signed word and add to another signed word. If the signed byte is $F0, then it's signed word version would be $FFF0 and if that is added to a signed word $0010 I would expect $0000.
Here is what I've got, but I'm not sure this working properly or if there is a better way:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
Here is what I've got, but I'm not sure this working properly or if there is a better way:
ticks VAR Byte ' signed byte ticksTot VAR Word ' signed word ASM MOV __PARAM1, $FF ' convert to signed word SB ticks.7 ' bit7 = 1, it's negative CLR __PARAM1 ADD ticksTot_LSB, ticks ADDB ticksTot_MSB, C ADD ticksTot_MSB, __PARAM1
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
Comments
The most common method (aka just about every computer I know uses this) is Two's complement: http://en.wikipedia.org/wiki/Two's_complement
In your code you're setting the 7th bit of ticks·as "1" to indicate a negative number. This is not how it is done in Two's complement.
Positive numbers are represented "normally" as a binary number. For example:
45 (decimal) --> 00101101 (binary)
To get -45, you first invert the bits and add one:
00101101 -->
11010010 --> (1s become 0s, 0s become 1s)
11010011······ (added 1 to the number)
So now -45 (decimal) --> 11010011 (binary) in two's complement
The cool thing about Two's complement is that you don't have to do anything else for addition and subtraction. "Normal" add and subtract functions will work on two's complement binary numbers.
Note that with Two's complement on 8 bits, the possible range is -128·to 127. Two's complement on 16 bits (a word) is done exactly the same way, except with a range of
-32768 to 32767. [noparse][[/noparse]corrected my error]
Take a closer look at how Two's complement works, then this problem becomes much easier.
Hope this helps!
Post Edited (InSilico) : 8/27/2008 9:30:27 AM GMT
You seem to be working in SX/B? SX/B supports operations on words and negative numbers (My 1.51.03 version does). You can then look into the assembly source code that the SX/B compiler generates (Ctrl + L), so you can see "how it's done". It's how I was able to catch up with assembly.
Post Edited (InSilico) : 8/27/2008 9:28:02 AM GMT
Remember I'm taking a twos-complement signed BYTE, converting it to a signed word (high byte needs to be either 0 if positive byte, or $FF if negative byte) then adding that to an existing signed word. This portion of my app is in ASM because, well, it needs to be
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php
This should also work:
ticks·VAR·Byte·······'·signed·byte
ticksTot·VAR·Word·'·signed·word
ASM
SNB·ticks.7················'·bit7·=·1,·it's·negative
DEC·ticksTot_MSB······ ' adding $FF to msb byte is like decrementing msb byte
ADD·ticksTot_LSB,·ticks
ADDB·ticksTot_MSB,·C
regards peter
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
1uffakind.com/robots/povBitMapBuilder.php
1uffakind.com/robots/resistorLadder.php