Name:
pushram
Syntax:

PUSHRAM

PUSHRAM CLEAR

 

Description:

Push the contents of RAM 0-15 (varaiables b0-b15) onto a 4 level 'RAM stack'. 

To reuse variables within a macro or sub procedure it can be useful to 'free up' variables so that they can be reused as temporary variables. The pushram command pushs the contents of b0-b15 onto the ram stack so that these 16 variables can then be reused for other purposes. The popram command then restores the original values at the end of the macro.

The optional 'clear' resets b0-b15 to 0 after the pushram has taken place. If clear is not used the values are not altered.

The 4-level stack used for pushram and popram is separate to the stack used for push and pop commands.

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

    Protecting variable values

    Before calling a subroutine a 'pushram' is executed so variables can be restored, if the subroutine uses those variables and alters their values. After the subroutine completes a 'popram' must be executed to restore the original variable values as they were previously.

    Code Example:
    main:	for b1 = 1 to 5
    	  sertxd( "Before gosub, b1=", #b1, cr, lf )
    	  pushram
    	  gosub mysub
    	  popram
    	  sertxd( "After gosub, b1=", #b1, cr, lf, cr, lf )
    	next
    	end
    	
    mysub:	b1 = 99	
    	sertxd( "   Inside mysub, b1=", #b1, cr, lf )
    	return
    Copy Code Submit an Example

    Use within subroutine

    On entry into the subroutine a 'pushram' is used to allow re-use of variables within the routine. At the end of the routine, before any return, a 'popram' must be executed to restore the original variable values as they were outside the subroutine.

    Code Example:
    main:	for b1 = 1 to 5
    	  sertxd( "Before gosub, b1=", #b1, cr, lf )
    	  gosub mysub
    	  sertxd( "After gosub, b1=", #b1, cr, lf, cr, lf )
    	next
    	end
    	
    mysub:	pushram
    	b1 = 99	
    	sertxd( "   Inside mysub, b1=", #b1, cr, lf )
    	popram
    	return
    Copy Code Submit an Example

    Using the 'pushram clear' option.

    Using 'pushram clear' clears the variable values after their existing values have been pushed. Within 'mysub1' the values remain as they were before the 'pushram', so b1=b1+1' always produces a result one greater than the value of b1 outside the routine. . Within the 'mysub2' routine the variables are cleared, so b1 is zeroed, and 'b1=b1+1' always produces the value 1.

    Code Example:
    main:	for b1 = 1 to 5
    	  sertxd( "Before gosub, b1=", #b1, cr, lf )
    	  gosub mysub1
    	  gosub mysub2
    	  sertxd( "After gosub, b1=", #b1, cr, lf, cr, lf )
    	next
    	end
    	
    mysub1:	pushram
    	b1 = b1 + 1	
    	sertxd( "   Inside mysub1, b1=", #b1, cr, lf )
    	popram
    	return
    
    mysub2:	pushram clear
    	b1 = b1 + 1	
    	sertxd( "   Inside mysub2, b1=", #b1, cr, lf )
    	popram
    	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.