Skip to content

Confused by the interaction of the stack and set_local #32

Closed Answered by maximecb
pierrec asked this question in Q&A
Discussion options

You must be logged in to vote

Hello @pierrec

I tried your example above and was also surprised for a moment that it does not work 😆

The reason is that set_local will pop the value you just pushed, and then you end up with stack size 0.

push 0; # Stack size is 1
set_local 0; # Pops the value 0, stack size is then zero, error!

push_0;
exit;

To make it work, you would need to allocate another stack slot:

push 0; # Stack size is 1
push 77; # Stack size is now 2
set_local 0; # Pops the value 77

# After this point, you're left with 77 on the stack.

get_local 0; # This would read stack slot 0
exit; # Exits with value 77

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@pierrec
Comment options

@maximecb
Comment options

@pierrec
Comment options

@maximecb
Comment options

Answer selected by pierrec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants