SX word limitations
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
Thanks!
Dave
Comments
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
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
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
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.
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
·· 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
·