Shop OBEX P1 Docs P2 Docs Learn Events
Substitutions for x = x + 1 — Parallax Forums

Substitutions for x = x + 1

SteelSteel Posts: 313
edited 2006-01-07 23:34 in BASIC Stamp
This is no big deal at all, but are there any substitutions for this command.

In assembly language, you can just do "inc x" which is much easier to type, and *may* take fewer clock pulses.

Anybody have any information on whether this is faster than x = x+1?

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-01-06 19:06
    This thread should've been posted in the SX Forum since it's not Stamp related.· It's up to the compiler how to handle that, but my guess is it would be treated differently since you may not always be adding one.· You may be adding 5 or 50, so it would use more commands.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • BeanBean Posts: 8,129
    edited 2006-01-06 19:48
    If your talking about SX/B then SX/B has the "INC" command. And yes "INC x" is faster than "x = x + 1" but the flags are not affected the same way.
    Bean.

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

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    "If I was king for just one day...I don't think I could screw things up any worse [noparse];)[/noparse]"
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-01-06 20:14
    In the PBASIC (BASIC Stamp) there are no INC or DEC instructions.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-01-06 20:27
    And there's no "++" or "--" instruction either, so "MyVar++" doesn't increment.

    So nope, it looks like X = X + 1 is the 'fastest' way to go about it.
  • Tracy AllenTracy Allen Posts: 6,658
    edited 2006-01-07 18:56
    One exception might be,
    FOR x=0 to final : NEXT
    where of course the Stamp takes care of the x=x+1.

    However, the FOR:NEXT construct on the Stamp is actually quite slow. I wouldn't swear to it, but I think the DO:LOOP construction is faster.
    x=0 : DO : x=x+1 :LOOP UNTIL x=final

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • BullwinkleBullwinkle Posts: 101
    edited 2006-01-07 22:37
    The inc and dec statements in assembler languages operate on the accumulator or on a register on the CPU. Memory locations are uneffected. And you are correct in assuming such instructions are VERY FAST compared to incrementing a memory location, since the operation takes place entirely on the CPU. Whenever memory is accessed there is a memory latency that has to be taken into account.

    Statements in BASIC like x = x + 1 or in C like x++, operate on a memory location associated (by the compiler) with the name "x". Translated into assembler it would actually become (at least) 3 instructions. Something like:

    LOAD (address of x)
    INCREMENT
    STORE (address of x)

    Post Edited (Bullwinkle) : 1/7/2006 10:40:55 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-01-07 23:34
    Bullwinkle said...(trimmed)
    LOAD (address of x)
    INCREMENT
    STORE (address of x)
    It could also be, if the number could possibly be more than 1, something like:

    MOV W, X
    ADD W, #$01  ' Or other value
    MOV X, W
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.