Shop OBEX P1 Docs P2 Docs Learn Events
signed byte and signed word addition (negative numbers subtract?) — Parallax Forums

signed byte and signed word addition (negative numbers subtract?)

ZootZoot Posts: 2,227
edited 2008-08-27 15:24 in General Discussion
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:

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

  • InSilicoInSilico Posts: 52
    edited 2008-08-27 07:44
    It depends on what encoding you used to store negative numbers.

    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
  • JonnyMacJonnyMac Posts: 9,215
    edited 2008-08-27 09:07
    SB actually means "Skip if Bit set," not Set Bit, and that line of code will cause the CLR __PARAM1 line to be skipped if bit 7 of ticks is 1 (i.e., ticks is negative). Also, a 16-bit word will give a signed range of -32768 to 32767 -- bit 15 is used for the sign.
  • InSilicoInSilico Posts: 52
    edited 2008-08-27 09:22
    Oops, I meant -32768 to 32767 for words. I need to get some more sleep.

    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. smile.gif

    Post Edited (InSilico) : 8/27/2008 9:28:02 AM GMT
  • ZootZoot Posts: 2,227
    edited 2008-08-27 13:58
    InSilico --- this code works fine as posted. I was just looking for a possibly more elegant and compact method.

    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 smile.gif The reason for loading up __PARAM1 ahead of time, and then clearing it or not is because the CLR is a single-word instruction which follows a skip.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-08-27 14:43
    Zoot,
    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
  • ZootZoot Posts: 2,227
    edited 2008-08-27 15:24
    That is compact! duh duh duh DEC. Thanks.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
Sign In or Register to comment.