Skip to content

04. Nesting structures and stacks

puregorill edited this page Jun 2, 2023 · 9 revisions

In order to be able to implement the control structures such as IF...ENDIF, SELECT...CASE and the loops such as REPEAT...UNTIL and so on, a stack must be used.
There are 3 stacks, one for IF...ENDIF, one for SELECT...CASE (yes, this structure has its own stack) and also the loops have their own stack.
By default, each of these 3 stacks has 12 levels.

IF stack

In the basic setting, this means that 12 IF...ENDIF may be nested within each other.
If you use ENDIFS (Note the "S" at the end of ENDIFS. Not to be confused with ENDIF, ENDIFS is a special command), it is also as if you had several IF...ELSE...IF...ELSE...IF...ENDIF...ENDIF...ENDIF nested inside each other. So pay attention to this when counting the stack levels.

"LOOP" stack

Also, 12 loops can be nested in each other completely independently of the other structures that use stacks.

SELECT stack

For SELECT...CASE, this means that 12 CASE statements may be present. But better count the DEFAULT statement as well.

To be on the safe side (although it can be a bit more due to a combination of SELECT and DEFAULT), count all CASE and DEFAULT and stick to the maximum stack size. No more than 12 CASE and DEFAULT added together in the default setting.

Therefore, no SELECT...CASE may be nested in another SELECT...CASE. The stack of SELECT...CASE takes care of storing the number of CASE commands.
Of course, you can nest the SELECT...CASE in an IF...ENDIF or a "LOOP".
Likewise, a "LOOP" or an IF...ENDIF can be embedded in a SELECT...CASE, but not (to be clear a second time) a SELECT...CASE within a SELECT...CASE.

Changing the stack size

If the stack size is too large (may increase the assembly time - but not sure if this is still an issue on modern hardware) or too small for your purposes, you can change it.

In the folder "MACRO HLA BASIC\MHLAB Internal\Create Stacks" there is the html-file "create stacks.html", which you can start with a browser of your choice.
It should be self-explanatory.

After pressing the button "create" copy the generated macro code into the file "Stacks.asm" and save it. You will find this file in "MACRO HLA BASIC\MHLAB Internal".

Clone this wiki locally