Shop OBEX P1 Docs P2 Docs Learn Events
16bit scaling — Parallax Forums

16bit scaling

nick bernardnick bernard Posts: 329
edited 2005-08-11 07:22 in General Discussion
how do i multiply a byte by 10 and div by 64 in SXB?
the algorithm may be simplified because the denominator is 2^6...

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
If I lived back in the wild west days, instead of carrying a six-gun in my holster, I'd carry a soldering iron. That way, if some smart-aleck cowboy said something like "Hey, look. He's carrying a soldering iron!" and started laughing, and everybody else started laughing, I could just say, "That's right, it's a soldering iron. The soldering iron of justice." Then everybody would get real quiet and ashamed, because they had made fun of the soldering iron of justice, and I could probably hit them up for a free drink. - Jack Handy

Comments

  • BeanBean Posts: 8,129
    edited 2005-08-09 17:07
    It's not pretty but it works:
    '
    DEVICE SX28,TURBO,OPTIONX,STACKX,OSC4MHZ
    FREQ 4_000_000

    Value VAR BYTE
    Temp1 VAR BYTE
    Temp2 VAR BYTE
    Temp3 VAR BYTE
    Result VAR BYTE

    PROGRAM START NOSTARTUP

    START:
    · Value=103

    · '·Result=Value * 10 / 64
    · Temp1=Value / 8
    · Temp2=__REMAINDER * 4
    · Temp3=Value / 32
    · Temp2=Temp2 + __REMAINDER
    · Temp3=Temp3 + Temp2.5
    · Result=Temp1 + Temp3

    · WATCH Result,8,udec
    · BREAK
    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

    "One experiment is worth a thousand theories"


    Post Edited (Bean (Hitt Consulting)) : 8/9/2005 5:23:32 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-08-09 17:24
    Nick,

    Bookmark this page: http://www.sxlist.com/techref/scenix/constdivmul_help.htm

    It is a web form to generate code to multiply a variable by a constant value (in your case 0.15625). It was designed before the advent of SX/B so the code it spits out is in assembler, but it shouldn't require too much effort to convert (simply code up the ALGORITHM overview in the comments).

    For your example it generated the following code (notice it's error is 0%, so this is an exact formula):

    ; ACC = ACC * 0.15625; Temp = TEMP; ACC size = 8 bits; Error = 0.5 %; Bytes order = little endian; Round = no; ALGORITHM:; Clear accumulator; Add input / 8 to accumulator; Add input / 32 to accumulator; Move accumulator to result;; Approximated constant: 0.15625, Error: 0 %;     Input: ACC0, 8 bits;    Output: ACC0, 6 bits; Code size: 11 instructionsACC0	DS	1;copy accumulator to temporary	mov	w, ACC0;shift accumulator right 2 times	clc	rr	ACC0	clc	rr	ACC0;add temporary to accumulator	add	ACC0, w;shift accumulator right 3 times	rr	ACC0	clc	rr	ACC0	clc	rr	ACC0
    

    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 8/9/2005 5:27:41 PM GMT
  • BeanBean Posts: 8,129
    edited 2005-08-09 17:57
    Paul,
    Cool... Beats the hell out of my code.
    Thanks for the link.
    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

    "One experiment is worth a thousand theories"
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-08-09 18:08
    No prob, the real thanks goes to Nick Golovchenko for writing the program, and James Newton for inspiring and hosting the program on his SX site.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • nick bernardnick bernard Posts: 329
    edited 2005-08-09 21:01
    that is stunning! its gunna take me a while to comprend it though. can i insert the assembly into the sxb file, or do i have to make a macro or something?

    "if this is out there imagine what else is out there" - navin johnson

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If I lived back in the wild west days, instead of carrying a six-gun in my holster, I'd carry a soldering iron. That way, if some smart-aleck cowboy said something like "Hey, look. He's carrying a soldering iron!" and started laughing, and everybody else started laughing, I could just say, "That's right, it's a soldering iron. The soldering iron of justice." Then everybody would get real quiet and ashamed, because they had made fun of the soldering iron of justice, and I could probably hit them up for a free drink. - Jack Handy
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-08-10 01:22
    Nick,

    ·· You can have the code as in-line assembly with your SX/B projects.· See the ASM/ENDASM commands.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-08-10 02:29
    You can also insert single lines of ASM like this:

    \ NOP

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Paul BakerPaul Baker Posts: 6,351
    edited 2005-08-10 04:53
    Nick, I agree, the principle is to add and subtract various powers of two of the value (via right and/or left shifting the value) to obtain the correct result. In your case, it just so happens that x*(10/64) is the same as x*(1/8 + 1/32). You can stick with peforming the math in SX/B, but embedding the assembly like Jon and Chris mentioned will result in faster execution.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • nick bernardnick bernard Posts: 329
    edited 2005-08-10 13:13
    thanks guys. much appreciated as always.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If I lived back in the wild west days, instead of carrying a six-gun in my holster, I'd carry a soldering iron. That way, if some smart-aleck cowboy said something like "Hey, look. He's carrying a soldering iron!" and started laughing, and everybody else started laughing, I could just say, "That's right, it's a soldering iron. The soldering iron of justice." Then everybody would get real quiet and ashamed, because they had made fun of the soldering iron of justice, and I could probably hit them up for a free drink. - Jack Handy
  • nick bernardnick bernard Posts: 329
    edited 2005-08-10 14:53
    WOW it does 9 bits too!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    If I lived back in the wild west days, instead of carrying a six-gun in my holster, I'd carry a soldering iron. That way, if some smart-aleck cowboy said something like "Hey, look. He's carrying a soldering iron!" and started laughing, and everybody else started laughing, I could just say, "That's right, it's a soldering iron. The soldering iron of justice." Then everybody would get real quiet and ashamed, because they had made fun of the soldering iron of justice, and I could probably hit them up for a free drink. - Jack Handy
  • KenMKenM Posts: 657
    edited 2005-08-11 07:22
    And any a tiny tiny bit to KenM for being a financial contributor to the sight for less than one lunch a month.....step up guys.
    Paul Baker said...
    No prob, the real thanks goes to Nick Golovchenko for writing the program, and James Newton for inspiring and hosting the program on his SX site.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Ken
Sign In or Register to comment.