Pop, Push, Exch... The Stack
Author: virtlink (talk, contrib) |
4.9.9 The Stack
The stack is a pile of values that is maintained by NSIS. This pile can be as big as you like, so you can put values on the stack, and get values from the stack. There is only one stack. The stack follows the LIFO (Last In First Out) principle. The stack can be used, for example, to pass parameters to functions or plugins. It can also be used to extend the $0-$9 and $R0-$R9 values, by putting their current values on the stack, assign new values to them, doing something with it, and return the old values to the variables.There are three instructions that can be used for interaction with the stack: Pop, Push and Exch.
4.9.9.3 Push
The push instruction pushes a value onto the stack. The value is put ON TOP of the stack. The stack's size will increase by one.
4.9.9.2 Pop
The pop instruction takes the TOP VALUE from the stack and assigns it to the specified variable. The stack's size will decrease by one. If there are no values on the stack (e.g. the stack's size is equal to zero), then the error flag will be set.
4.9.9.1 Exch
The Exch exchanges two values. If Exch is used without any parameters, the TOP TWO values of the stack are swapped.If a user variable is used as the parameter for the Exch instruction, then the value of the stack is assigned to the variable, and the variable's value is put ON TOP of the stack.If Exch is used in combination with a stack index (e.g. a number that points to the value on the stack, starting at 0 (the top value)), then the top value is exchanged for the value at the specified indexe.
Examples
The following examples show the use of the Pop, Push and Exch instructions. On the left, the executed code is shown. And on the right, you see the values that are on the stack after executing the code.
Code | Stack |
Push "Value 1" Push "Value 2" |
|
Pop $0 ;$0 contains: "Value 2" |
|
Push $0 Push "Value 3" Push "Value 4" |
|
Exch ; No parameters ;Exchanges the top two values of the stack. |
|
StrCpy $0 "Value X" Exch $0 ;Exchanges the top value with the variable. ;$0 contains "Value 3" |
|
Exch 3 ;Exchanges the top value with the fourth value (thus index 3). |
|