Your Spectrum
YS MegaBasic manual - Controlling Program Flow
Home YS MegaBasic Contents

CONTROLLING PROGRAM FLOW

PROCEDURES

YS MegaBasic now provides the Spectrum with procedures ... that is, subroutines which can be called just by executing their names - as if they were new commands. And, in the same way as commands, a subroutine's name can be followed by a series of expressions, if so required. These expressions can be assigned to variables at the start, ready for manipulation by the rest of the subroutine. The only drawback with YS MegaBasic procedures is that 'local' variables cannot be used.
  Using procedures, it's possible to split a program up into a number of separate tasks, one procedure being written for each task. Each procedure can then be tested on its own and, finally, they can all be put together to form the finished program. Choose sensible names for your procedures and you'll find it much easier to follow the program flow.
  The start of a procedure is defined by an '@' symbol followed by the procedure's name. The 'define procedure' statement must be the first statement on a line; if parameters are to be passed into a subroutine then the 'define statement' must have an underline character followed by the required parameters, after the procedure's name. The parameters should be separated by commas.
  The end of the procedure is defined by the 'ENDPROC_' command. Once this command is encountered, the computer will resume processing at the statement after the procedure call. To aid
14.b

legibility it is possible to tag the procedure name on to the end of the 'ENDPROC_' command. Let's look at an example:

  9000 @DISPLAY_A,A$
  9010 PAPER A: INK 9
  9020 MODE_4: STIPPLE_6
  9030 PRINT A$
  9040 ENDPROC_DISPLAY


This is a simple procedure. Line 9000 defines the 'DISPLAY' procedure, and indicates that a numerical expression (A) followed by a string expression (A$) are required. Line 9010 sets the required colours and line 9020 sets the correct character size. Line 9030 prints the string which has been supplied to the subroutine and, finally, line 9040 defines the end of the procedure. To activate the procedure you'd use a line like 'DISPLAY_2,"MEGABASIC"' elsewhere in your program.
  Since there are no local variables, you must make sure that those used in one procedure don't clash with others in another part of the program. Procedures can only be called from within a program, and so can't be used as direct commands.

REPEAT-UNTIL

As well as procedures, YS MegaBasic provides 'REPEAT-UNTIL' loops. The 'REPEAT' command is used to mark the beginning of a loop and 'UNTIL_' is followed by a single numeric expression. If this numerical expression returns a zero value then the program flow jumps back to the statement after the last REPEAT instruction; if the expression is a positive number, program flow continues with the next statement as it should. 'REPEAT-UNTIL' loops can be nested up to ten deep.

THE PROCEDURE / REPEAT-UNTIL STACK

A stack is used by procedures and 'REPEAT-UNTIL' loops to store line and statement numbers. When a procedure's called, the line and statement number of the statement after the call is placed onto the stack - this lets the MegaSpectrum know where to return to at the end of the procedure. When a 'REPEAT' command is executed, the command and line number of the statement after the 'REPEAT' command, is pushed onto the stack; this tells the 'UNTIL 'command where the last 'REPEAT' command was.
  When 'ENDPROC' is executed, a jump is made to the statement stored on the stack and its value is removed. Also, if the expression of an 'UNTIL' command is true, the top value is removed from the stack. If the stack is empty but an attempt is made to remove a value, then the "PROC stack underflow" error message is displayed on-screen. The
15

stack can store up to ten values in all ... any attempt to input an eleventh value will result in a "PROC stack overflow" error message.

POP AND PUSH

The 'POP_' command will remove a value from the stack, whereas 'PUSH_' places a value on the stack. The 'PUSH_' command is followed by two numeric expressions - the statement number followed by the line number. Anyone incorporating procedures or 'REPEAT-UNTIL' loops in a YS MegaBasic program should use the 'PCLEAR' command to reset the stack at the beginning of the program.

BRANCH

The 'BRANCH_' command makes the computer execute a subroutine at the end of every program line. The command is followed by a single numeric expression pointing to the start of the subroutine. If its value is zero, then the MegaSpectrum acts as normal at the end of each line. The end of the subroutine is defined by 'ENDPROC_'.

MTASK

A simple kind of multi-tasking is available with YS MegaBasic. The 'MTASK_' command allows the program to be run from two separate places at once. Think of the program as being split into two parts: the first continues after the 'MTASK_' command and the second starts at the line specified by the single numeric expression following 'MTASK_'. Once multi-tasking has been activated, the MegaSpectrum executes a line alternately from each part of the program. If the numeric expression you choose is zero, then multi-tasking is disabled; also if you enable multi-tasking, then branching is automatically disabled. When using 'MTASK_', any commands which make use of the ZX Interface 1 unit must be followed by '::'.
16.a