Stack up or stack down?
CannibalRobotics
Posts: 535
So if I'm trying to test stack depth and I use:
Then I use:
So, unless I'm really lucky with the value of the test byte, it should tell me the depth?
I'm getting 7 every time even after changing the value of the test byte.
So, is this only using 7 longs in the stack?
Thanks in advance,
Jim-
bytefill(@MCPStack, %1010_0101, 500 * 4) ' Fill stack space.to fill the stack. Where MCPStack is set to 500 longs.
Then I use:
pub TestStack | i,j repeat i from @MCPStack to (@MCPStack + (500 * 4)-1) bytemove(@j,i,1) if j <> %1010_0101 return (i - @MCPStack)/4to read it.
So, unless I'm really lucky with the value of the test byte, it should tell me the depth?
I'm getting 7 every time even after changing the value of the test byte.
So, is this only using 7 longs in the stack?
Thanks in advance,
Jim-
Comments
TestStack is called about once a minute.
I'm not sure why you get a value of 7. The test should fail immediately on the first byte, and you should get a value of 0. The beginning of the stack contains a stack frame, so the first byte shouldn't match your test value. You should check the stack starting from the end instead of the beginning. Also, it is better to use a 32-bit test value rather than 8 bits because there is less chance of a false match. Your code should look something like the code shown below.
Dave
EDIT: When do you fill the stack? You should be doing it before you do the cognew. I suspect that you might be doing it at the beginning of the new cog's method. This might explain why the first 7 longs contain the test value.
I'll try it prior to COGNEW, it's sitting in the top of the main program loaded into the cog.
J-
got it worked out and relearned a few things along the way. This one counts down from the top, not up from the bottom, seems to make more sense and the values are more reasonable for the code; in the 30 to 50 longs range.
Note the placement ahead of COGNEW - Thanks Dave.
In the start pub we have... To sample the stack use:
The other thing I was reminded of is that variables used locally, like the i and j of TestStack must be initialized on every call.
This begs a discussion of the question: Is stack depth the sum of all stack calls in an object or is it the maximum depth of the most simultaneously active pub/pri stack calls?
Here's a simple program to measure the amount of stack used by each call
It produces the following output. main uses 12 bytes, and recurse uses 20 bytes for each call. The difference between main and recurse is the 8 bytes used for the two parameters in recurse. The other 12 bytes are for the Result variable, and the 8-byte stack frame used for each call.