TOP
TOP is a function returning the end address of the current BASIC program.
Availability | Present in all original versions of BBC BASIC. | |
Syntax | BASIC I-V | <num-var> = TOP
|
Token (hex) | BASIC I-V | B8 50 (TO , P ) (rvalue)
|
Description | BASIC I-V | Returns the first free address after the BASIC program, that is, one plus the address of the terminating byte (&FF in 6502 BASIC). |
Associated keywords | PAGE , HIMEM , LOMEM
|
Description
TOP
returns the end address of the BASIC program. The start address of the program is always equal to PAGE
, so one can find out the size of the program, in bytes, with the following:
PRINT TOP-PAGE
Programs use TOP
to access data or machine code embedded in the program file. For instance, the loader files of Superior Software titles contain a short BASIC program followed by a Teletext banner. The BASIC program uses TOP
to fetch the banner and poke it into the MODE 7
screen.
Relocatable machine code binaries may start with a 12-byte stub, which BASIC interprets as:
10876:::CALLTOP
so that they will run correctly even if CHAIN
ed. (See CALL
; and Polyglot programs for the significance of the line number. Only ARM binaries need the colons.)
If PAGE
has been changed, for example to switch between two programs in memory, the user must enter the command:
OLD
to update TOP
and other pointers to the new program. (See OLD
.)
As a system variable, TOP
seldom tolerates being part of a base?offset
or base!offset
expression. TOP
is read-only.
TO vs. TOP
Although TOP
shares a token with TO
, this does not lead to any trivial pitfalls. The code to evaluate expressions, which anticipates TOP
, is separate from the code to interpret FOR
statements, which expects TO
after the second expression. So assuming the variable P
has been defined earlier, one can safely use:
FOR X%=1TOP ... NEXT
or even:
FOR X%=TOP TOP ... NEXT
(The space is needed to end the second expression.) It would also be possible to do:
FOR X%=TOP TOTOP+&100 ... NEXT
-- beardo 22:02, 2 May 2007 (BST)