for my homework I need to write a very little virtual 16 bit Assembler-Interpreter in C#.
It simulates the RAM with a byte-array (64k) and the registers with Variables (A,B,C,…).
Now I need a way to save local variables, google says that they are allocated on the Stack.
But the thing thats unclear to me is, when they are allocated on the Stack (with push…), how is the Interpreter accessing them when they are used later?
See the following 2 lines:
pi INT 3
mov A, pi
In the first line, pi is allocated on the stack, in the second line, pi is used, but how should the Interpreter know where pi is in the stack to access its data? (my Stack is a byte-array too with 2 helper-functions (push, pop), there is also a pointer to the top of the stack)
Typically there is no separate stack memory, instead the stack is in the regular RAM, so you only have the stack pointer that keeps track of it.
Also typically, local variables are allocated at the beginning of a subroutine by copying the stack pointer to another register, then moving the stack pointer to make room for the variables:
Accessing local variables is done using the copy of the stack pointer:
When you leave the subroutine, you restore the stack pointer to deallocate the local variables:
The syntax that you use in the question is usually used to declare global variables, not local variables: