Shop OBEX P1 Docs P2 Docs Learn Events
SX word limitations — Parallax Forums

SX word limitations

achilles03achilles03 Posts: 247
edited 2004-11-30 00:24 in General Discussion
How do you overcome the SX's limitation on numeric values over 255?· Is there some routine you can implement which can·rollover to another byte (like separating it into word.HIGHBYTE and word.LOWBYTE)?· How can you·process numbers in the ALU that are greater than 255?··Or can you?

Thanks!
Dave

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2004-11-29 19:28
    Well it depends on what exactly you are trying to do, the simplest arithmetic operation (not including boolean operations) is the counter. The fastest code to do this on the SX is:

    inc countlo
    snz
    · inc counthi

    In a nutshell, whenever countlo is 0 (ie a rollover) counthi is incremented, this can also be done using the carrybit as outlined in Gunther's book (which I highly recommend, and is available from Parallax) but this is not as fast as the inc/snz/inc method.

    For more complex arithmetic such as A+B->B:

    add Blo, Alo
    addb Bhi, c
    add Bhi, Ahi

    The first add, adds the low terms, the second instruction adds the contents of a single bit to the destination. In this instance we are adding the carry bit to the hi byte of the result register, since the carry bit is 1 if there was a carry the carry is properly propagated under all circumstances. Then the high bytes are added. There is a minor caveat with this algorithm where Bhi=255 and C=1 after the first add, the carry bit will most likely·not be properly set·after the last add, since the last instruction will write its carry result to the carry bit as well, if this condition must be detected (ie you use the resultant carry bit in a subsequent instruction) you'll need to enter a "sc"·or "snc" (skip if (not) carry) instruction after addb to explicitly handle the case (what you do here is application specific). One potential solution (not exhaustive nor optimized) is:

    clr·MyStatus.0
    add Blo, Alo
    addb Bhi, c
    snc
    ·· inc MyCarry
    add Bhi, Ahi
    addb MyCarry, c
    sz
    ·· call HandleOverflow

    Here an additional register is used to accumulate carries. So if MyCarry is 0 at the sz instruction, there was no carry of the hi byte (either with addb or second add) and we proceed as normal. If MyCarry is 1, there was a carry somewhere and the subroutine HandleOverflow is called·(though MyCarry is potentially incremented twice, there is no combination of numbers which will cause this to occur so you can get away with only using a single bit for MyCarry, just be sure to mask irrelevant bits before using the register in any instructions which take a byte as an argument). I just spewed this algorithm from my brain so I can pretty much guarantee it is neither fastest possible algorithm nor the most code compact.

    Other 16 bit operations are handled similarly (there is an equivalent subb instruction for subtraction).

    More complex mathematical operations like multiplication and division are too complex to discuss in a forum format, and Parallax has sample code that handles it for you.

    Also SX allows you to declare multi-byte data, which·(if I remember correctly) is accessed as A.1, A.2, etc.

    -Paul

    Post Edited (Paul Baker) : 11/29/2004 8:00:33 PM GMT
  • achilles03achilles03 Posts: 247
    edited 2004-11-29 19:48
    Thanks for the reply. I mainly wanted to know if you could declare multibyte data. However, can the ALU crunch numbers over 255? For instance, would it be easy to implement the following (I'll code in PBasic, since I have yet to get the SX starter kit).

    NUM1 VAR BYTE
    NUM2 VAR BYTE

    NUM1=5
    NUM2=400

    LOOP:
    IF (NUM2*NUM1)<5000 THEN LOOP
    END

    It's a never-ending loop, but I guess what I'm asking is can the ALU process numbers over 255?

    Thanks,
    Dave
  • Paul BakerPaul Baker Posts: 6,351
    edited 2004-11-29 20:17
    Hmmm, I do not have experience with using SX/B as of yet (I'm still cutting my teeth on the asm and peculiarities of the SX architecture). So, I do not know specifically whether SX/B will handle multibyte arguments. If not, you can always code in assembly (I recognize·this is not a desirable solution) or break your PBasic code into byte-wise pieces. I was going to bang out some simple code, but this question is best answered by someone who has more familiarity with SX/B.·Someone could try NUM2 VAR WORD (or whatever the equivalent command is) for you if no one specifically knows the answer outright.

    The ALU does not handle anything greater than 8 bit operands (neither does the BS2, which uses·an SX chip) but the SX/B cross compiler most likely·can spit out the proper assembly code to make it appear as if it is doing so (this is what the BS2 does).

    Post Edited (Paul Baker) : 11/29/2004 8:27:02 PM GMT
  • allanlane5allanlane5 Posts: 3,815
    edited 2004-11-29 22:00
    Just tried it. SX/B does not currently (11/29/2004) support the 'WORD' or 'LONG' keywords.

    However -- most 8-bit processors (like *all* PICs) do multi-byte math using multiple instructions.· There's no shame in that, especially at 50 MIPS.· Once upon a time, all microprocessors had mere 8-bit registers.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2004-11-29 22:54
    OK I found this in Exploring the SX, available at http://www.parallax.com/dl/docs/books/ExploringTheSx.pdf

    I found on page 56 (page 66 of the pdf):

    The following text·in Courier font is taken directly from the pdf

    If you want to deal with words, you’ll have to use the same techniques that you’d use in assembly language. Of course, there is no addb and subb instructions in SX/B. You can use the C variable directly, though as in:

    X=X+C

    Also, you can use the \ escape to incorporate these useful instructions:

    v1h=$10

    v1l=$FA

    v2h=$03

    v2l=$AA

    ' compute v1=v1+v2

    v1l=v1l+v2l

    \ addb v1h,c

    v1h=v1h+v2h

    Sorry about the double spacing, sometimes the forum interface insists on doing it and I haven't figured out why yet.

    Post Edited (Paul Baker) : 11/29/2004 10:56:18 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2004-11-30 00:24
    Paul,

    ·· Hitting SHIFT while hitting Enter will prevent the double spacing...



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage

    Knight Designs
    324 West Main Street
    P.O. Box 97
    Montour Falls, NY 14865
    (607) 535-6777

    Business Page:·· http://www.knightdesigns.com
    Personal Page:··· http://www.lightlink.com/dream/chris
    Designs Page:··· http://www.lightlink.com/dream/designs
    ·
Sign In or Register to comment.