Pop, Push, Exch... The Stack: Difference between revisions
m (Wikipedia python library) |
m (Reverted edits by 61.9.6.233 to last version by Anders) |
||
(12 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
{{PageAuthor|virtlink}} | |||
== [http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.9 4.9.9 The Stack] == | == [http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.9 4.9.9 The Stack] == | ||
Line 13: | Line 15: | ||
== [http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.9.1 4.9.9.1 Exch] == | == [http://nsis.sourceforge.net/Docs/Chapter4.html#4.9.9.1 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 | 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 == | == Examples == | ||
Line 19: | Line 21: | ||
{| cellpadding="0" cellspacing="0" border="1" | {| cellpadding="0" cellspacing="0" border="1" | ||
|- | |||
| '''Code''' | |||
| '''Stack''' | |||
|- | |- | ||
| <highlight-nsis> | | <highlight-nsis> | ||
Line 24: | Line 29: | ||
Push "Value 2" | Push "Value 2" | ||
</highlight-nsis> | </highlight-nsis> | ||
| <code>Value 2 | | | ||
Value 1</code> | <code>Value 2</code><br> | ||
<code>Value 1</code> | |||
|- | |- | ||
| <highlight-nsis> | | <highlight-nsis> | ||
Line 31: | Line 37: | ||
;$0 contains: "Value 2" | ;$0 contains: "Value 2" | ||
</highlight-nsis> | </highlight-nsis> | ||
| <code>Value 1</code> | | | ||
<code>Value 1</code> | |||
|- | |- | ||
| <highlight-nsis> | | <highlight-nsis> | ||
Line 38: | Line 45: | ||
Push "Value 4" | Push "Value 4" | ||
</highlight-nsis> | </highlight-nsis> | ||
| <code>Value 4 | | | ||
Value 3 | <code>Value 4</code><br> | ||
Value 2 | <code>Value 3</code><br> | ||
Value 1</code> | <code>Value 2</code><br> | ||
<code>Value 1</code> | |||
|- | |- | ||
| <highlight-nsis>Exch ; No parameters | | <highlight-nsis>Exch ; No parameters | ||
;Exchanges the top two values of the stack. | ;Exchanges the top two values of the stack. | ||
</highlight-nsis> | </highlight-nsis> | ||
| <code>Value 3 | | | ||
Value 4 | <code>Value 3</code><br> | ||
Value 2 | <code>Value 4</code><br> | ||
Value 1</code> | <code>Value 2</code><br> | ||
<code>Value 1</code> | |||
|- | |- | ||
| <highlight-nsis>StrCpy $0 "Value X" | | <highlight-nsis>StrCpy $0 "Value X" | ||
Line 56: | Line 65: | ||
;$0 contains "Value 3" | ;$0 contains "Value 3" | ||
</highlight-nsis> | </highlight-nsis> | ||
| <code>Value X | | | ||
Value 4 | <code>Value X</code><br> | ||
Value 2 | <code>Value 4</code><br> | ||
Value 1</code> | <code>Value 2</code><br> | ||
<code>Value 1</code> | |||
|- | |- | ||
| <highlight-nsis>Exch 3 | | <highlight-nsis>Exch 3 | ||
;Exchanges the top value with the fourth value (thus index 3). | ;Exchanges the top value with the fourth value (thus index 3). | ||
</highlight-nsis> | </highlight-nsis> | ||
| <code>Value 1 | | | ||
Value 4 | <code>Value 1</code><br> | ||
Value 2 | <code>Value 4</code><br> | ||
Value X</code> | <code>Value 2</code><br> | ||
<code>Value X</code> | |||
|} | |} | ||
[[Category:Tutorials]] |
Latest revision as of 16:47, 23 August 2011
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). |
|