<!-- PDF page 260 -->

To make all the extra memory easily accessible to the user, *NextBASIC* provides a special command called **BANK** which can be combined with a number of normal commands to extend their functionality to the whole of the ZX Spectrum Next's memory and not just the memory map addresses. We've seen some already used in the course of this guide, especially in chapters *14* through *17 as well as Chapter 20*.

Memory banks are marked as *in-use* or *free* by the user or by commands that access them (`BANK … PEEK` / `PEEK$` / `POKE` / `COPY` / `ERASE` / `USR` / `LAYER`, `LAYER … BANK` and `LOAD … BANK`). Users can mark a bank as *in-use* or as *free*, by either using an explicit command from the list above or one of the two special commands `BANK NEW` *var* and `BANK` *n* `CLEAR`.

`BANK NEW` *var*

Reserves the next available free bank number and assigns it to the numeric variable *var*, ready for use with and by other `BANK` commands. This command is useful for allocating banks for use in *NextBASIC*, allowing for cases where a resident machine code program has previously allocated banks for its own use.

Note, that is not essential to use this command, as commands such as `LOAD … BANK` will automatically allocate the specified bank for use by *NextBASIC*, but only if the specified bank is not already in use by a resident machine code program.

Let's try a small example. Assuming you have a 2048K ZX Spectrum Next; type the following program:

```
10 FOR %f = 0 TO 111
20 BANK NEW a
30 PRINT AT 0,0; "Allocating
   bank:"; a
40 NEXT %f
```

Once you `RUN` it, the program will begin to allocate memory banks and print the ones it allocates; you'll notice two things: Allocation begins at bank **111** (**47** if using an unexpanded KS1/Issue 2 ZX Spectrum Next) and progresses backwards and that program execution will stop abruptly with a `4 Out of memory` error report once you reach a bank that's allocated by the system as described in the *NextZXOS and NextBASIC Memory Allocation section*. Indeed, if you use the dot command `.mem` then you'll see that you have **0 banks free (0K)**. In order to free up a bank to be used, you will need to use the `BANK` *n* `CLEAR` command whose syntax is as follows:

`BANK` *n* `CLEAR`

Marks bank *n* as *free* for use by other parts of the system (eg dot commands).

Let's try to free a bit of memory after the mess we've made with the previous program. Without making any more changes, let's try:

```
BANK 11 CLEAR
```

More likely than not, the system will report: **In Use, 0:1.** What has happened? Most likely that the bank itself is in use by the system. Let's try again:

```
BANK 12 CLEAR
```

This time the system will most likely report: **0 OK, 0:1.** We can verify this by running `.mem` again. This time it will show us **2 Banks free** (Remember `.mem` reports memory in MMU sized banks – that is 8K). Bank **11** you tried to free originally (unless the system hasn't been modified), is being used by *Layer 2 (*which takes **3** banks, *by default* **9**,**10** and **11**but can be changed by the `LAYER…BANK` command*)* so it's rightfully marked as *in-use*. Note

