Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculator using 2 stacks

I have an intel assembly assignment. I need to write a calculator which uses 2 stacks. For example, i have an expression like 23+4/2^4$ .So that $ indicates the end of expression. What I will do is to have two stacks, one for numbers, one for operators and push and pop them according to the operator precedence.

What I need is how can I use 2 stacks for two different purpose at the same time. As long as I know esp register indicates the place for variables in the stack to pop the last or to push a new one. But if I only have one esp register, how can I have two stacks?

Thanks in advance...

like image 930
israkir Avatar asked Dec 12 '25 22:12

israkir


1 Answers

I think what you're looking for is Dijkstra's shunting algorithm.

I have solved it without using stacks during interpretation, only during execution as described in my blog.

As for making extra stacks, it is quite easy. All a stack is, is really just an area of memory with a pointer to the top and bottom. Every time you push, you increment the top pointer, every time you pop you decrement the top pointer.

like image 87
Guge Avatar answered Dec 16 '25 00:12

Guge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!