A19..A16 are lit, indicating the actual stack usage is 15 longs.
Okay, how does it work? The stack_monitor.start method
fills the target stack with a known pattern (constant value called
FILLER) and then launches the monitor method into its own cog.
PRI monitor(addr, size, ledMsb, ledLsb) | idx, used
used := 0
repeat idx from addr to addr + size * 4 step 4
used -= long[idx] <> FILLER
outa[ledMsb..LedLsb] := used
Darned nice, too, and very helpful in the Parallax forums.
Okay, I think that’s about enough. After three months
of Propeller work, you should have enough to get going —
so get going! Remember to be patient with yourself; this is
a big change from PBASIC and a whole lot of power is at
your disposal; you just have to learn to use it. If you have a
specific idea that you just can’t make work, feel free to send
it to me — we just might make another article out of it!
Until next time, Happy Spinning! NV
,xI . cNoC.com
As you can see, this method is actually quite small. It starts by clearing the
LED pins and making them outputs
before entering an infinite loop. Within
the loop, the variable — used — is cleared
and then the elements of the stack being
monitored are scanned to see if they
match the FILLER pattern. Now here’s a
really clever bit of code — have a look at
the line that modifies used.
You see how there’s a subtraction?
This is odd, isn’t it, since we actually
want to add up the number of longs
used by the object’s stack. Well, here’s
why it works: The right side of the
expression (after -=) will evaluate as
true or false. The key is that true is
defined as -1 ($FFFFFFFF) in Spin, so
when we subtract -1, it has the same
effect as adding +1. Subtracting zero
(false) has no effect. That’s pretty cool,
isn’t it? I told you Phil was a sharp guy!
June 2006 17