Two Stacks in Assembler?

1.8k Views Asked by At

I am coding in 8086 assembler and I have ran into an interesting question. The topic is to evaluate parentheses. If this was a question in Java or C, I would simply define two stacks - one for numbers, and another for operands. Can I do something similar in Assembly? As far as I know, the Stack is defined in the last memory cells of the data segment. If I define another Data Segment, would I have another usable stack?

Another info: I don't know the input size in the beginning and I am supposed to make the program as efficient as possible.

Thanks!

2

There are 2 best solutions below

0
On

So you want to use two stack data structures to evaluate expressions like ((a+b) + (c))?

You can use the call stack (sp) for one of them, if you're careful to check that syntax errors in the input don't crash your program. (e.g. compare bp with sp to detect when you've emptied the stack data structure that you're storing on the call stack).

Don't change sp to point to the other stack data structure; use a different register (like si) to access it.

You could use lodsw to pop into ax (with the direction flag set appropriately for the direction you have your stack growing). Or use stosw to push ax onto a stack pointed to by di. But since they use different index registers, it's not worth it (especially not changing the direction flag all the time).

So for the second stack data structure, just use normal mov loads/stores and add/subb si, 2 as appropriate.

If it proves inconvenient to keep one of the stacks on the call stack (sp), then don't do that either.

9
On

As far as I know, the Stack is difined in the last memory cells of the data segment

This is true if you're developing a .COM style program where all the segment registers have the same value and where DOS placed the stackpointer at the high end of this 64KB memory.

If I will difine another Data Segment, will I have another useable stack?

There's no need to change the Data Segment to have another stack. Change the SS:SP register pair and start using the newly defined stack.
The stack extends downwards starting at SP. So if you had SP=4096 then the stack would be 4096 bytes. (not counting wraparound which would probably be wrong anyway)