1

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!

zx485
  • 28,498
  • 28
  • 50
  • 59
DoronZor
  • 11
  • 2

2 Answers2

2

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)

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • Thank you. Than this should work? stack 100h push ax push bx ... mov ax, ss mov bx, sp mov ss, 0f00h mov sp, 0f00h And than I have one stack in ax:bx and another stack in ss:sp? – DoronZor Jun 19 '16 at 12:21
  • There can be only 1 stack active at a time: the one defined with SS:SP. You can setup a second memory region for stacking purposes but you need to load the full pointer to this region in the SS:SP pair. – Sep Roland Jun 19 '16 at 12:26
  • Also an instruction like `mov ss, 0f00h` does not exist! You can't move an immediate value (number) in a segment register. – Sep Roland Jun 19 '16 at 12:27
  • Thanks. can I activate both stacks not simultanisly? meaning- can I define ss, sp, and push to this stack, and later define new ss, sp and push to that stack? Later I will pop from each stack in it's turn, and define ss, sp before each pop. Will it work? – DoronZor Jun 19 '16 at 12:31
  • That's exactly how you can do this. – Sep Roland Jun 19 '16 at 12:35
  • Considering I don't know the unput size, how can I decide where to start the "second" stack in? can I take, like: mov ax, 1000h mov ss, ax? it will give me 16^3-2 cells to use in the first stack, but won't it overrun other data? – DoronZor Jun 19 '16 at 12:38
  • Assuming that you're writing a .COM program and that it realistically will not be too long, I would recommend to only work with changing the SP stackpointer. Have the first stack at SP=0xD000 and the second one at SP=0xE000. Leave all the segment registers intact, so CS=DS=ES=SS. – Sep Roland Jun 19 '16 at 12:43
  • 1
    Thanks. In my case, I need to push data to both stacks one at a time. It means that I must have a pointer for each stack right? otherwise I will overrun data in the stack, since I returns sp to the same point every time... – DoronZor Jun 19 '16 at 13:12
  • 3
    Correct. As it is you're best of with 3 stacks! One for the *numbers*, one for the *operands* and one for the *normal stacked items* like `call`, `ret`, `int`, ... – Sep Roland Jun 19 '16 at 13:26
0

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.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847