Homework 3
Due Thursday, November 8
In this problem, you will write an assembly language file,
SpecialStack.s, which manages a stack of your own, NOT
one pointed to by ESP. The routines will be callable from C. Here
are the details:
- SpecialStack.s will be self-contained; no stack
manipulation code may be in the C callers.
- The stack grows away from 0. For example, a push increases the
address of the top of the stack by 4.
- The address of the top of the stack is stored in a word with the
label tos in the .data segment. (Or,
if you prefer, a .comm segment.)
- You will allocate your first chunk of stack space by calling
malloc(). (Calling brk() may be
easier, but that requires more background.) If a push would result in
stack overflow, then call malloc() to add another
chunk of space. Set the chunk size in a .equ directive.
- The first two (i.e. two lowest-address) and last two (i.e. two
highest-address) words in a stack chunk will be reserved for chunk
linking:
- The next-to-lowest-address and next-to-highest-address words
will contain the value -1, signaling begin or end of chunk. (It will
be assumed that no data item will ever be -1.
- The lowest-address word will contain 0 if there is no preceding
chunk; if there is such a chunk, this word will contain the address
of the highest-address data word in that chunk.
- The highest-address word will contain 0 if there is no following
chunk; if there is such a chunk, this word will contain the address
of the lowest-address data word in that chunk.
- Your code will check for stack underflow, meaning an error
in which you attempt to perform a pop on an empty stack.
- Your SpecialStack.s file must contain the
following functions, shown with their C signatures:
- void initstack(void): This sets up the first
chunk of the stack, and initializes tos.
- void pushstack(int m): This pushes
m onto the stack. Note that this may trigger the
formation of a new chunk.
- int popstack(int *errcode): This pops the
stack returning the popped value. Upon an underflow error, it
will place an error code (1 for error, 0 if not) into
errcode.
- void swapstack(void): This will swap the top two
elements of the stack.
- void printstack(int n): This will print (in
hex) the first n elements of the stack, starting
from the top of the stack and working toward 0.
Error checking:
Other than as specified in popstack(), you need not
write error-checking code.
Testing:
Matt will be testing your code by calling it from C; make sure you check
beforehand that that will work. Of course, set a small chunk size for
easy testing.