PDA

View Full Version : Substitutions for x = x + 1



Steel
01-07-2006, 02:37 AM
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?

Chris Savage
01-07-2006, 03:06 AM
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 (mailto:csavage@parallax.com)

Bean
01-07-2006, 03:48 AM
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 (http://www.sxvm.com)

"If I was king for just one day...I don't think I could screw things up any worse ;)"
ˇ

Jon Williams
01-07-2006, 04:14 AM
In the PBASIC (BASIC Stamp) there are no INC or DEC instructions.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax

allanlane5
01-07-2006, 04:27 AM
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 Allen
01-08-2006, 02:56 AM
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 (http://www.emesystems.com)

Bullwinkle
01-08-2006, 06:37 AM
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 Savage
01-08-2006, 07:34 AM
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 (mailto:csavage@parallax.com)