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!
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. comparebp
withsp
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 (likesi
) 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 usestosw
to push ax onto a stack pointed to bydi
. 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 andadd/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.