- Name:
- push
- Syntax:
-
PUSH data { , data, data ... }
data - the byte data value, variable or constant, to be added to the stack.
- Description:
-
Copy a word data value to the stack. The data may either be a numeric constant or the value held within a variable. Data can be bit, byte or word sized.
All existing stack values are moved up one place and the variable value is added to the bottom of the stack. The stack is 16 words in size and can therefore hold up to 16 word variables. It is separate to the stack used by pushram and popram commands. After 16 consequative pushes (without any pop) the earliest data is simply lost.
Pushing and popping order
Note that variables need to be popped in the reverse order to which they are pushed on to the stack. If b1 is pushed before b2, then b2 needs to be popped before b1.
When b1 and b2 are pushed in a single command, the order of those variables need to be reversed in the corresponding pop command -
push b1, b2
gosub myroutine
pop b2, b1This is the same as -
push b1
push b2
gosub myroutine
pop b2
pop b1
Simple push and pop
This example shows how a variable (b0 here) can be pushed to the stack, have its value changed, then have its original value restored after a pop.
- Code Example:
-
main: b0 = 0 sertxd( "Before push, b0=", #b0, cr, lf ) push b0 sertxd( "After push, b0=", #b0, cr, lf ) b0 = 99 sertxd( "After change, b0=", #b0, cr, lf ) pop b0 sertxd( "After pop, b0=", #b0, cr, lf ) end
Copy Code Submit an Example
Pushing and popping multiple variables
Note that variables must be popped in the reverse order to which they are pushed on to the stack. Here b1 is pushed before b2, therefore b2 needs to be popped before b1.
- Code Example:
-
main: b1 = 11 b2 = 22 push b1 push b2 b1 = 99 b2 = 99 pop b2 pop b1 sertxd( "b1=", #b1, " b2=", #b2, cr, lf ) end
Copy Code Submit an Example
Pushing and popping multiple variables in same command
Note that variables must be popped in the reverse order to which they are pushed on to the stack. Here b1 is pushed before b2, therefore b2 needs to be popped before b1. When b1 and b2 are pushed in a single command, the order of those variables need to be reversed in the corresponding pop command.
- Code Example:
-
main: b1 = 11 b2 = 22 push b1, b2 b1 = 99 b2 = 99 pop b2, b1 sertxd( "b1=", #b1, " b2=", #b2, cr, lf ) end
Copy Code Submit an Example
Passing parameters using the stack
This examples demonstrates how parameters for a subroutine may be pushed on to the stack and then pulled from the stack within the subroutine whichcan perform actions depending on those values retrieved. Note that variables must be popped in the reverse order to which they are pushed.
- Code Example:
-
main: b0 = 11 push 1, b0 gosub mysub b1 = 22 push 2, b1 gosub mysub end mysub: pop b2 pop b1 sertxd( "b1=", #b1, " b2=", #b2, cr, lf ) return
Copy Code Submit an Example
Submit Your Own Code!
You must be logged in to submit code examples. Login now.