LIFO Stack Algorithm Design

I implement a stack-based virtual machine, and I tried to read the literature that explains or describes the algorithms for working with stacks. Here is an example:

int i = 3
int j = 4
int k = 5

Suppose that i, jand kare local variables, so they are traditionally stored on the stack. The assembly / bytecode translation will look something like this:

pushi 3
pushi 4
pushi 5

And the stack will be: 5 4 3

I have an integer stack and a string stack, so pushihowever, my question does not contain them on the heap (with some ptr*or literal identifier), how does the compiler or interpreter know that if I want to do something like int x = i + jafter the definition, I need to stroke two and three times, respectively , and also do everything possible not to lose k(save it in the register or something else, and then push it away)?

I hope my question makes sense and probably a much more sensible approach: P Thanks for any insight!

+3
source share
2 answers

. ( , / ), . .

+2

, , , push pop . , , .

+2

Source: https://habr.com/ru/post/1745349/


All Articles