Shop OBEX P1 Docs P2 Docs Learn Events
SX/B Multiplication — Parallax Forums

SX/B Multiplication

jgjonolajgjonola Posts: 27
edited 2005-10-11 19:27 in General Discussion
Hey Guys,· Is there a way to do multibyte multiplication using sx/b?· If I do this...

z·= 255 * 31... is there any way to get the real result?· If not, do you have an assembly work around?

Also, one other quick one (hopefully)· If a user enters (via a keypad) a 3 digit number, how do i take the 3 digits and make it 1 number.· like I enter 1,2,3 now i want to turn that into 123.· Any suggestions?· I was thinking first digit * 100 + second digit * 10 + 3rd digit.· Is there an easier way?
Thank you,

John Gjonola

Post Edited (jgjonola) : 9/27/2005 6:11:27 AM GMT

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-27 12:16
    Not at the moment -- but we are trying to figure out a way to add it cleanly (SX/B is supposed to be a learning tool).· I ran into this myself with a small project that we wanted to do in SX/B, but I got lucky.· I only ever had to multiply by 32 so it was a simple matter of shifting left five times.· Here's my code:

    ' Use: MAKE_ADDR theChar
    ' -- multiples 'theChar' by 32


    MAKE_ADDR:
    · eeLo = 0················· ' clear address
    · eeHi = 0
    · IF __PARAM1 > 0 THEN
    ··· ASM
    ····· MOV· eeLo, __PARAM1···' copy character value
    ····· MOV· __PARAM1, #5···· ' prep to shift 5 times
    ····· CLC
    ····· RL·· eeLo············ ' shift the low byte
    ····· RL·· eeHi··········· ·' shift high (use C)
    ····· DJNZ __PARAM1, $-2
    ··· ENDASM
    · ENDIF
    · RETURN


    The point of showing this is that if you can find an assembly routine for what you want to do then you can add it as I have done.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-27 12:22
    On your second question, you might do something like this

    GET_VALUE:
    · keyCount = 0
    · theVal = 0
    · DO
    ····key = SCAN_KEY
    ··· IF key = Enter THEN EXIT
    ··· theVal = theVal * 10
    ··· theVal = theVal + key
    ··· INC keyCount
    · LOOP UNTIL keyCount = 3
    · RETURN


    This routine will allow up to three keys and can terminate with an enter key -- that way you can get one- or two-digit values.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • dkemppaidkemppai Posts: 315
    edited 2005-09-27 12:25
    Jon Williams (Parallax) said...

    Not at the moment -- but we are trying to figure out a way to add it cleanly (SX/B is supposed to be a learning tool).· I ran into this myself with a small project that we wanted to do in SX/B, but I got lucky.· I only ever had to multiply by 32 so it was a simple matter of shifting left five times.· Here's my code:

    ' Use: MAKE_ADDR theChar
    ' -- multiples 'theChar' by 32


    MAKE_ADDR:
    · eeLo = 0················· ' clear address
    · eeHi = 0
    · IF __PARAM1 > 0 THEN
    ··· ASM
    ····· MOV· eeLo, __PARAM1···' copy character value
    ····· MOV· __PARAM1, #5···· ' prep to shift 5 times
    ····· CLC
    ····· RL·· eeLo············ ' shift the low byte
    ····· RL·· eeHi··········· ·' shift high (use C)
    ····· DJNZ __PARAM1, $-2
    ··· ENDASM
    · ENDIF
    · RETURN


    The point of showing this is that if you can find an assembly routine for what you want to do then you can add it as I have done.

    JW,

    See, multi-byte math...·· ...it's the way of the future!·(grin) smile.gif I·thinking of·some projectes·last night that could use it...



    JG,

    http://www.sxlist.com/techref/ubicom/lib/math/mul.htm·These are ASM routines, but you should be able to implement them in SX/B...

    -Dan
  • BeanBean Posts: 8,129
    edited 2005-09-27 12:27
    John,
    Here is a routine to get a 16 bit result from multiplying two bytes:

    DEVICE SX28,OSCXT1,OPTIONX,STACKX,TURBO
    FREQ 4_000_000

    A VAR BYTE ' Multiplicand
    B VAR BYTE ' Multiplier
    LSB VAR BYTE ' LSB of Product
    MSB VAR BYTE ' MSB of Product

    PROGRAM Start NOSTARTUP

    Start:
    · A = 255
    · B = 31
    · ASM ' LSB,MSB = A * B
    ··· MOV __PARAM1,A '·Copy multiplicand because the copy will get trashed
    ··· CLR MSB ' Start with all bits 0 execpt LSB.7 which is used as a counter flag to indicate we are done
    ··· MOV LSB,#128
    · Mult_Loop:
    ··· RR __PARAM1 ' Rotate multiplicand
    ··· MOV W,B· '·Prepare·to add multiplier to Product MSB
    ··· SNC '·Only add if the carry is set
    ··· ADD MSB,W ' If carry was set add multiplier to MSB
    ··· RR MSB '·Rotate the whole product
    ··· RR LSB
    ··· JNC @Mult_Loop ' Repeat until that bit we set initially shows up
    · ENDASM
    · WATCH LSB,16,UDEC
    END

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012
    Product web site: www.sxvm.com
    Available now... SX-Video OSD module $59.95 www.sxvm.com

    "I'm a man, but I can change, if I have to, I guess"
    Red Green


    Post Edited (Bean (Hitt Consulting)) : 9/27/2005 6:59:52 PM GMT
  • PJMontyPJMonty Posts: 983
    edited 2005-09-27 17:19
    Bean,

    Perhaps you could add some comments to your code to explain why it is doing what it is doing.
      Thanks, PeterM
  • jgjonolajgjonola Posts: 27
    edited 2005-09-27 17:25
    Thanks to all of you! Jon, perfect for my second question. Bean, perfect for my first.

    Thanks again,
    John Gjonola
  • BeanBean Posts: 8,129
    edited 2005-09-27 17:29
    Peter,
    What's a comment ? Ha Ha just kidding...

    Okay, I'll edit the post and add comments...
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012
    Product web site: www.sxvm.com
    Available now... SX-Video OSD module $59.95 www.sxvm.com

    "I'm a man, but I can change, if I have to, I guess"
    Red Green
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-27 18:54
    Bean:

    I noticed you used the SX/B keyword "LOOP" in your code and I fear that this would cause an error in actual use (I checked, it does). You may want to edit that to "Mult_Loop."

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • BeanBean Posts: 8,129
    edited 2005-09-27 19:04
    Fixed... Thanks Jon.
    See thats's why I don't comment stuff.... [noparse];)[/noparse]
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012
    Product web site: www.sxvm.com
    Available now... SX-Video OSD module $59.95 www.sxvm.com

    "I'm a man, but I can change, if I have to, I guess"
    Red Green
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-27 19:17
    Since I will likely need 8x8 multiply soon I packaged it into an SX/B subroutine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • PaulPaul Posts: 263
    edited 2005-10-08 17:41
    Bean,
    You wouldn't have an ASM 16bit by 8bit <i>divide</i> laying around also would you? I tried the one from SXlist but unless all quotients are $FF, I think its not working. Getting the 32bit by 16 bit divide sounds like overkill at this point but maybe I have to go there.
    I need to divide 10K by a number between 25 and 200 for a pulse to frequency conversion.
    Thanks
    Paul

    Update: I found another 16bit/8bit division on SXList made by Nikolai Golovchenko. This dropped into a ASM/ENDASM perfectly. Lesson learned: If you dont see it on SXList..keep digging..lol

    Post Edited (Paul) : 10/9/2005 1:45:08 AM GMT
  • James NewtonJames Newton Posts: 329
    edited 2005-10-10 22:22
    Paul, which routine did you find that you actually used? Was it the 16/16 which is in the description of the 32/16? Or a different one?

    I'm trying to see why the 16/8 would not work and the only thing I can think of is that it may be dependant on the carryx option setting... I'll play with it a bit.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • James NewtonJames Newton Posts: 329
    edited 2005-10-10 23:05
    As I suspected, that 16/8 routine on sxlist.com (at http://www.sxlist.com/techref/ubicom/lib/math/div/16by8lz_sx.htm ) does not work with CARRYX turned on. I guess it is on by default in SX/B? A version that works with CARRYX could be made if there is interest.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



  • PaulPaul Posts: 263
    edited 2005-10-11 14:29
    James,
    I tried the one marked "16 bits by 8 from Lou Zher" first. Then found the /math/div16by8to16_16.htm (with 16 bit decimal not remainder) from Nickolai. Strangely I dont see it listed anymore. hmmm. Found it! It's at the bottom of /math/basic.html... not in /math/div.htlm. (you might want to move it to div.html [noparse];)[/noparse] )
    I'm afraid I don't know anything about carryx or y or z..I can barely carry a tune. This is my first attempt to prog the SX other than running Parallax demo download and blinking lights.
    It sure gives one an appreciation for the BS2 and something as simple as "DEBUG DEC3 10000/166"
  • James NewtonJames Newton Posts: 329
    edited 2005-10-11 19:27
    I've updated the page at
    http://www.sxlist.com/techref/ubicom/lib/math/div

    Thanks for letting me know.... Not sure how it got that way...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ---
    James Newton, Host of SXList.com
    james at sxlist,com 1-619-652-0593 fax:1-208-279-8767
    SX FAQ / Code / Tutorials / Documentation:
    http://www.sxlist.com Pick faster!



Sign In or Register to comment.