I took a little detour, tonight, from my list of tasks to tidy up a little.
I wrote some test code to verify that the push and pop are working pretty much as I expect. Here's the output:
Test 01: .....PASSED
Test 02: .....PASSED
Test 03: .....PASSED
Test 04: .....PASSED
Test 05: .....PASSED
Which doesn't mean anything unless you know what the tests are.
Test 01 verifies basic operation. It pushed 14 and pops once and verifies that the result is 14:
printf("Test 01: ");
TESTPUSH
temp = 14;
push(temp);
TESTPOP
if (pop() != temp) goto EXCEPTION;
printf(".....PASSED\n");
TESTPUSH, TESTPOP, and the if statement can each break it with the same result:
Test 01: Error: Exiting.
Test 02 pushes and pops 100 values:
printf("Test 02: ");
for (i = 0; i (less than) stackSize; i++)
{
TESTPUSH
push(i);
}
for (; i (greater than) 0; i--)
{
TESTPOP
temp = pop();
}
printf(".....PASSED\n");
Test 03 verifies that the same value is pushed as popped. This is a little more specific than Test 02, which just cares about staying inside the stack range.
Test 04 and 05 just check whether the logic of the testing code works. These might be better at the start of the test section, but I'm not moving things around right now.
// TEST 4: Verifies the test code for pops by negating it.
printf("Test 04: ");
if (current != base) goto EXCEPTION;
printf(".....PASSED\n");
// TEST 5: Verifies the test code for pushes by negating it.
printf("Test 05: ");
for (i = 0; i (less than) stackSize; i++)
{
TESTPUSH;
push(i);
}
if (current != (base+stackSize)) goto EXCEPTION;
printf(".....PASSED\n");
Next, resizing the stack with realloc.
Tuesday, March 30, 2010
Monday, March 29, 2010
The New Number Two
So, tonight I did the second thing on my list of stack tasks:
Basically, I dealt with two situations:
EXCEPTION: printf("Error: Exiting.\n");
return 0;
This means that the state of the stack is tested BEFORE any changes are made. Later versions of this, instead of exiting, will modify the stack to allow for additional pushes.
Here's POP:
#define POP if (current == base) goto EXCEPTION; pop(); // deal with extra pop.
I used a compiler directive to tie the test with the pop.
Here's PUSH:
#define PUSH(x) if (current == (base+stackSize)) goto EXCEPTION; push(x);
Next: Expanding the stack.
Write code to deal with cases outside the expected cases.
Basically, I dealt with two situations:
- More pops than pushes.
- More than 100 pushes.
EXCEPTION: printf("Error: Exiting.\n");
return 0;
This means that the state of the stack is tested BEFORE any changes are made. Later versions of this, instead of exiting, will modify the stack to allow for additional pushes.
Here's POP:
#define POP if (current == base) goto EXCEPTION; pop(); // deal with extra pop.
I used a compiler directive to tie the test with the pop.
Here's PUSH:
#define PUSH(x) if (current == (base+stackSize)) goto EXCEPTION; push(x);
Next: Expanding the stack.
Saturday, March 27, 2010
First task
Although I describe it as a queue in my list, the first few versions of this are a simple stack implemented in memory. Later versions will implement FIFO functionality.
Here's the stack:
base = malloc(arraySize * sizeof(int)); // Allocate memory for stack. NOTE: Memory is not freed.
"base" seems like a funny name for a stack pointer, but I expect to create a separate value for pointer and keep "base" for other stuff - like checking whether there have been more pops than pushes.
There's a push and a pop:
void push(int value)
{
*base = value; // Assign value to location.
base = base + 1; // Increase location one element.
}
int pop()
{
base = base - 1; // Decrease location to previous location
return *base; // Get value at current location
}
And I wrote various testing code, which lives in main(), such as the following:
for (i = 0; i (less than) 100; i++)
{
push(i);
}
This code (and some other I wrote) tests the program as it is intended to be used (no more pops than pushes and no pushes beyond the size of the stack.)
And here's some code that breaks it:
for (i = 0; i (less than) 1000000; i++)
{
push(i);
}
This got me a fun (and expected) "The memory could not be written." error.
The next version of the program will handle the edge cases - probably by throwing an exception if the stack tries to pop below 0 or push above 100.
Here's the stack:
base = malloc(arraySize * sizeof(int)); // Allocate memory for stack. NOTE: Memory is not freed.
"base" seems like a funny name for a stack pointer, but I expect to create a separate value for pointer and keep "base" for other stuff - like checking whether there have been more pops than pushes.
There's a push and a pop:
void push(int value)
{
*base = value; // Assign value to location.
base = base + 1; // Increase location one element.
}
int pop()
{
base = base - 1; // Decrease location to previous location
return *base; // Get value at current location
}
And I wrote various testing code, which lives in main(), such as the following:
for (i = 0; i (less than) 100; i++)
{
push(i);
}
This code (and some other I wrote) tests the program as it is intended to be used (no more pops than pushes and no pushes beyond the size of the stack.)
And here's some code that breaks it:
for (i = 0; i (less than) 1000000; i++)
{
push(i);
}
This got me a fun (and expected) "The memory could not be written." error.
The next version of the program will handle the edge cases - probably by throwing an exception if the stack tries to pop below 0 or push above 100.
I had so much fun with assembly . . .

Back in January, I started a blog called Learning ARM Assembly in 30 Days where I played with programming for ARM chips.
It was a lot of fun and I created a fun memory manager that I can only run in a simulator program.
But - as I explained in the blog - there was kind of a limit to how much I could accomplish for that platform. But there's some stuff I should have down a little better in real computer languages, like C, so I'm going to do that for a while.
Above is my first list of tasks. You'll notice that 1 is already crossed off. I'll describe it in the next post.
Subscribe to:
Posts (Atom)