Name:
push
Syntax:

PUSH data { , data, data ... }

data - the byte data value, variable or constant, to be added to the stack.

Description:

Copy a byte data value to the stack. The data may either be a numeric constant or the value held within a variable. Data can be bit or byte 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 32 bytes deep and is separate to the stack used by pushram and popram commands.

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, b1

This is the same as -

    push b1
    push b2
    gosub myroutine
    pop b2
    pop b1

Word variables

Note the stack is byte sized so care must be taken when using word variables. When a word variable is pushed only its least significant byte will be pushed, and when a word variable is popped it will only receive a byte value result.

To push a word variable the two component byte parts of that variable must be pushed. For example, the component byte variable parts of 'w2' are 'b5' (MSB) and 'b4' (LSB). Popping a word variable must also be done using the component byte variable parts, and note that the order of those parts will be reversed in the pop than in the push. For example to push and then pop 'w2' the following should be used -

    push b4   ; push 'w2', LSB first
    push b5
 
    pop b5    ; pop 'w2', MSB first
    pop b4

Numbers with value greater than 255

Only the least significant 8-bits of a number value are pushed to the stack. If it is desired to push a number with a value greater than 255 it must be split into 8-bit (byte) parts and pushed seperately. For example the value '12345' is made up of two byte parts, 48 and 57 (48*256+57=12345), which can be pushed separately -

    push 57   ; push 12345, LSB first
    push 48
 
    pop b5    ; pop 'w2', MSB first
    pop b4

The 'w2' variable will now contain the value 12345.

Applies To:
20X2, 28X2, 40X2
See Also:
Related Create:
    Share:
    Print:

    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.

    Comments

    • Login to leave a comment.