It is never necessary to use line numbers. Instead of GOSUB you should use named user-defined procedures or functions. In the case of RESTORE you can use the relative option of the statement. You can avoid GOTO by making use of structures like REPEAT ... UNTIL and WHILE ... ENDWHILE and using the techniques described under Program Flow Control.
The Renumber utility can be used to add or remove line numbers automatically. Permissible line numbers are in the range 1 to 65535.
For example, the following will work:
but it's better to include the colon:FOR i=1 TO 5 PRINT i : NEXT
You may prefer to write each statement on a separate line. This will not impair the performance of a compiled program so long as you select the 'concatenate lines' Crunch option.FOR i=1 TO 5 : PRINT i : NEXT
Here a single procedure call has been split between several lines for reasons of clarity and to allow the various parameters to be commented. Everything after the continuation character is ignored by BASIC when the program is executed so comments can conveniently be put there. Note that the continuation lines themselves must begin with another \ character.PROC_render(pdev%, \ pointer to Direct3D device \ nobj%, \ number of objects \ yaw(), \ array of yaw angles \ pitch(),\ array of pitch angles \ roll() \ array of roll angles \ )
You can use the line continuation character almost anywhere that a space would be allowed, but not:
variables functions () ! ? & % unary+− NOT ^ * / MOD DIV + − = <> <= >= > < << <<< >> >>> AND EOR OR
IF A=2 AND B=3 THEN IF ((A=2)AND(B=3))THEN IF A=1 OR C=2 AND B=3 THEN IF((A=1)OR((C=2)AND(B=3)))THEN IF NOT(A=1 AND B=2) THEN IF(NOT((A=1)AND(B=2)))THEN N=A+B/C-D N=A+(B/C)-D N=A/B+C/D N=(A/B)+(C/D)
Array expressions are evaluated strictly left-to-right, so higher priority operators must come first and brackets cannot be used to override the order of execution:C%() = A%() * B%() + C%() B() = A() - PI A() = B() . C() A$() = B$() + C$() + D$() A() = B() * 2 / C() + 3 - A()
All arrays must be DIMensioned before use, and (with the exception of the dot product) the number of elements in all the arrays within an expression must be the same; if not, the Type mismatch error will result. In the case of the dot product the rules are as follows:C%() = C%() + B%() * A%() : REM not allowed C%() = A%() * (B%() + C%()) : REM not allowed
Don't confuse this with the MOD operator, which returns the remainder after an integer division.A() = A() / MOD(A())
You can use the compound assignment operators with arrays (e.g. +=, −=,*= or /=) but there is an important restriction. If the array is an integer (or byte) array the expression on the right-hand-side of the operator is converted to an integer before the operation is carried out. So the statement:
will multiply all the elements of array A%() by 2, not by 2.5.A%() *= 2.5
If there are fewer values supplied than the number of elements in the array, the remaining elements are unaffected. However in the special case of a single value being supplied, all the elements of the array are initialised to that value. So in the last example all the elements of C() will be initialised to PI. There must be enough free space on the stack to contain a copy of the array.A%() = 1, 2, 3, 4, 5 B$() = "Alpha", "Beta", "Gamma", "Delta" C() = PI
Using long, meaningful, variable names will make your program easier to understand and debug, and will not affect performance so long as the program is compiled with the abbreviate names crunch option.
BBC BASIC for Windows and BBC BASIC for SDL 2.0 use the following types of variable:
In addition the variables A%, B%, C%, D%, F%, X% and Y% have special uses in the CALL and USR routines, and L%, O% and P% have special meaning in the assembler.
Static variables are stored in 32 bits and can contain any whole number from −2147483648 to +2147483647.
Variant numeric variables can contain either a 64-bit signed integer value or an 80-bit (10-byte) floating-point value consisting of a 64-bit mantissa and a 16-bit exponent. They have a range of approximately ±3.4E−4932 to ±1.1E4932 and a precision of approximately 19 significant figures.
BBC BASIC for Windows version 5.95a and earlier only
Variant numeric variables come in two forms, depending on the
*FLOAT mode in effect. In *FLOAT 40 mode
they can contain either a 32-bit signed integer value or a
40-bit (5 byte) floating-point value consisting of a 32-bit mantissa and an 8-bit
exponent. They have a range of approximately ±5.9E−39 to
±3.4E38 and a precision of approximately 9 significant figures.
In *FLOAT 64 mode they can contain either a 32-bit signed integer
value or a 64 bit (8 byte) floating-point value consisting of a 53-bit mantissa and an
11-bit exponent. They have a range of approximately ±2.3E−308 to
±1.7E308 and a precision of approximately 15 significant figures.
An explanation of how variant numeric variables are stored is given in the Format of data in memory section.
The value given when the array is declared is the maximum value that the subscript can take. Since the minimum value for a subscript is zero, the total number of elements in that dimension is equal to the given value plus one. For example, the two-dimensional array declared with:
has a total of 66 elements (10+1 rows by 5+1 columns).DIM marks%(10,5)
The other system variables are as follows. They are mostly of use when accessing Windows or SDL 2.0 API functions from BASIC.
Name | Value |
---|---|
@chrmap% | A pointer to the 'character map' used in MODE 7 and for GET(x,y) |
@flags% | An integer incorporating BASIC's control flags. The MSB, if set, indicates that there is a pending Escape condition |
@fn%(n) | An array of pointers to internal functions |
@hfile%(n) | An array of file handles indexed by channel number |
@hpal% | The handle for the colour palette |
@hwnd% | The 'window handle' for BASIC's program (output) window |
@hwo% | The handle of the WAVEOUTPUT device |
@ispal% | A Boolean which is non-zero if the display is paletted |
@memhdc% | The 'device context' or 'renderer' for BASIC's output canvas |
@msg% | The MSG value (for use with ON MOUSE, ON MOVE and ON SYS) |
@wparam% | The WPARAM value (for use with ON MOUSE, ON MOVE and ON SYS) |
@lparam% | The LPARAM value (for use with ON MOUSE, ON MOVE and ON SYS) |
@ox% | The horizontal offset (in pixels) between the output bitmap and the canvas. Must be zero or positive |
@oy% | The vertical offset (in pixels) between the output bitmap and the canvas. Must be zero or positive |
@vdu% | A pointer to BASIC's text and graphics parameters (see below) |
@cmd$ | The command line of a 'compiled' program |
@dir$ | The directory (folder) from which your program was loaded |
@lib$ | The directory (folder) containing the BBC BASIC library files |
@tmp$ | The temporary directory (folder) |
@usr$ | A directory (folder) specific to the current user |
@vdu{} | A structure containing the main VDU variables |
BBC BASIC for Windows only: | |
@haccel% | The handle of the keyboard accelerator, if used |
@hcsr% | The handle for the mouse pointer (cursor) |
@hevent% | The handle of the event used to prevent blocking in serial and parallel I/O |
@hmdi% | The Multiple Document Interface window handle (if any) |
@hwacc% | The window handle to which keyboard accelerator commands should be sent |
@midi% | The MIDI device ID (non-zero if a MIDI file is playing) |
@prthdc% | The 'device context' for the current printer (if any) |
BBC BASIC for SDL 2.0 only: | |
@panx% | The horizontal position of the output canvas with respect to the window. |
@pany% | The vertical position of the output canvas with respect to the window. |
@zoom% | The scale factor of the output canvas with respect to the window (&8000 = 1:1). |
@platform% | The SDL version number in the most-significant 24 bits (major, minor, patch) and a platform identifier in the LS 4 bits (0 = Windows, 1 = Linux, 2 = MacOS, 3 = Android, 4 = iOS, 5 = in-browser). If bit 6 is set this indicates a 64-bit platform. |
On 64-bit platforms @chrmap%, @fn%(), @hfile%(), @hwnd%, @hpal%, @memhdc% and @vdu% may all return 64-bit values, despite them having only a single % suffix!
Files specified this way are automatically incorporated into an executable created by the Compile command. @dir$ and @lib$ are read-only, you must not write to them.INSTALL @lib$+"MYLIB" OSCLI "DISPLAY "+@dir$+"MYIMAGE" SYS "PlaySound", @dir$+"MYSOUND", 0, &20001
The main VDU variables may alternatively be accessed via the @vdu{} structure, as shown below:
Value | Alternative | Parameter |
---|---|---|
@vdu%!0 | @vdu.o.x% | The horizontal graphics origin (in BBC BASIC graphics units), as set by ORIGIN or VDU 29. |
@vdu%!4 | @vdu.o.y% | The vertical graphics origin (in BBC BASIC graphics units), as set by ORIGIN or VDU 29. |
@vdu%!8 | @vdu.l.x% | The horizontal coordinate (in pixels) of the last point 'visited', as set by MOVE, PLOT etc. |
@vdu%!12 | @vdu.l.y% | The vertical coordinate (in pixels) of the last point 'visited', as set by MOVE, PLOT etc. |
@vdu%!16 | @vdu.p.x% | The horizontal coordinate (in pixels) of the previous point 'visited', as set by MOVE, PLOT etc. |
@vdu%!20 | @vdu.p.y% | The vertical coordinate (in pixels) of the previous point 'visited', as set by MOVE, PLOT etc. |
@vdu%!24 | @vdu.tl% | The left edge of the text viewport (in pixels), as set by VDU 28. |
@vdu%!28 | @vdu.tr% | The right edge of the text viewport (in pixels), as set by VDU 28. |
@vdu%!32 | @vdu.tt% | The top of the text viewport (in pixels), as set by VDU 28. |
@vdu%!36 | @vdu.tb% | The bottom of the text viewport (in pixels), as set by VDU 28. |
@vdu%!40 | @vdu.d.x% | The width of a graphics 'dot', in pixels. |
@vdu%!44 | @vdu.d.y% | The height of a graphics 'dot', in pixels. |
@vdu%!48 | @vdu.c.x% | The horizontal text cursor position, in pixels. |
@vdu%!52 | @vdu.c.y% | The vertical text cursor position, in pixels. |
@vdu%!56 | @vdu.hf% | The handle of the current font, as set by *FONT. |
@vdu%!60 | @vdu.hr% | The handle of the current graphics clipping rectangle, as set by VDU 24 . |
@vdu%?64 | @vdu.g.a& | The current graphics foreground action (ROP2 code), as set by GCOL. |
@vdu%?65 | @vdu.g.b& | The current graphics foreground colour, as set by GCOL. |
@vdu%?66 | @vdu.g.c& | The current graphics background action (ROP2 code), as set by GCOL. |
@vdu%?67 | @vdu.g.d& | The current graphics background colour, as set by GCOL. |
@vdu%?68 | @vdu.t.a& | The top line of the text cursor (caret), as set by VDU 23,0,10. |
@vdu%?69 | @vdu.t.b& | The bottom line of the text cursor (caret), as set by VDU 23,0,11. |
@vdu%?70 | @vdu.t.c& | The text foreground colour, as set by COLOUR. |
@vdu%?71 | @vdu.t.d& | The text background colour, as set by COLOUR. |
@vdu%?72 | @vdu.m.a& | The current MODE number (255 for a user-defined mode). |
@vdu%?73 | @vdu.m.b& | The colour mask for the current MODE (number of colours−1). |
@vdu%?74 | @vdu.m.c& | The VDU emulator flags byte (8 bits). |
@vdu%?75 | @vdu.m.d& | The scroll counter in VDU 14 paged mode. |
@vdu%?76 | @vdu.w.a& | The width, in pixels, of the text cursor (caret) as set by VDU 23,0,18. * |
@vdu%?77 | @vdu.w.b& | The thickness, in pixels, of solid straight lines and outline shapes, as set by VDU 23,23.... * |
@vdu%?78 | @vdu.w.c& | The text cursor (caret) movement control, as set by VDU 23,16.... |
@vdu%?79 | @vdu.w.d& | BBC BASIC for Windows:
Set to &80 to suppress character substitution in MODE 7. BBC BASIC for SDL 2.0: The inter-character spacing adjustment, in pixels. |
@vdu%!208 | @size.x% | The width, in pixels, of BASIC's output 'screen', as set by MODE or VDU 23,22. |
@vdu%!212 | @size.y% | The height, in pixels, of BASIC's output 'screen', as set by MODE or VDU 23,22. |
@vdu%!216 | @char.x% | The width, in pixels, of one character cell, as set by MODE or VDU 23,22. |
@vdu%!220 | @char.y% | The height, in pixels, of one character cell, as set by MODE or VDU 23,22. |
These apply to BBC BASIC for Windows only, BBC BASIC for SDL 2.0 does not currently support hardcopy output: | ||
@vdu%!224 | The width of a character cell on the printer, as set by *PRINTERFONT. | |
@vdu%!228 | The height of a character cell on the printer, as set by *PRINTERFONT. | |
@vdu%!232 | The left edge of the printed page (left margin), as set by *MARGINS. | |
@vdu%!236 | The right edge of the printed page, as set by *MARGINS. | |
@vdu%!240 | The top edge of the printed page (top margin), as set by *MARGINS. | |
@vdu%!244 | The bottom edge of the printed page, as set by *MARGINS. | |
@vdu%!-12 | The horizontal coordinate determining where text will appear on the page when sent to the printer. | |
@vdu%!-8 | The vertical coordinate determining where text will appear on the page when sent to the printer. | |
@vdu%!-4 | The current printer job ID (if any). |
In BBC BASIC for SDL 2.0 System Variables may be 'stale', that is they may not represent the current state unless a 'thread synchronisation' is first performed. This is most easily (and quickly) achieved by reading the POS or VPOS value:
IF POS REM. SDL thread sync REM. System Variables may now be read reliably
Count% = 0 name$ = "" Value += 1
DEF PROC1 : LOCAL Count%, name$ DEF PROC2 : PRIVATE Count%, name$
DEF PROC1(Count%, name$)
CALL code, Count%, name$
INPUT Count% MOUSE x,y,b READ name$ SYS "GetTickCount" TO Count%
PROC1(Count%) DEF PROC1(RETURN C%)
SYS "GetFileSize", @hfile%(F%), ^sizehigh%
For example suppose you want to represent the position of an object in 3D space, which you can define by means of three numeric coordinates X, Y and Z. You could simply use three scalar variables, but it might be clearer or more convenient to group them together as a single entity describing the object position. One way of doing that would be to define an array with three elements:
An alternative way would be to define a structure with three members:DIM objpos(2) objpos(0) = X objpos(1) = Y objpos(2) = Z
In this case there is not much to choose between the alternatives, although the structure method does have the advantage of indicating the function of each member. However if the object description includes a name as well as a position then the array method cannot be used, because you can't store both a string and a number in the same array. In that case a structure is the answer:DIM objpos{x,y,z} objpos.x = X objpos.y = Y objpos.z = Z
DIM object{name$,pos{x,y,z}} object.name$ = Name$ object.pos.x = X object.pos.y = Y object.pos.z = Z
Normally a structure member can be treated in exactly the same way as a simple variable of the same type. However this is not so in the case of operations on entire arrays (e.g. SUM) or entire structures (e.g. SWAP) which cannot be used with structure members. Similarly you cannot pass an array member or a sub-structure member as the parameter of a procedure or function.structure_name.member_name structure.substructure.subsubstructure.member
A byte-array structure member is treated as a special case. The construct structure.array&() is exactly equivalent to $$^structure.array&(0), that is it refers to a NUL-terminated string stored in the array. This is convenient for certain structures used by the Windows™ and SDL 2.0 APIs.
This statement declares a structure called mystruct which contains the following members:DIM mystruct{one,two%,three$,four(3),five%(1,2),six$(9),seven{a,b%}}
Note the use of brace characters ({ }) rather than parentheses (( )) which are used when declaring an array.
mystruct.one A variant numeric value mystruct.two% An integer value mystruct.three$ A string mystruct.four() An array of four variant numeric values mystruct.five%() An array of six integer values (2 rows and 3 columns) mystruct.six$() An array of ten strings mystruct.seven{} A sub-structure containing the members mystruct.seven.a and mystruct.seven.b%
orDIM mystruct{(10)one,two%,three$}
These declare an array of 11 structures (subscript 0 to subscript 10) each of which has three members. Individual members are referenced as follows:DIM item{one,two%,three$} DIM mystruct{(10)} = item{}
Note that it is necessary to use both braces and parentheses to indicate to BASIC that you are referring to an element of an array of structures, rather than a simple array element. Arrays of structures can be multi-dimensional.mystruct{(1)}.one mystruct{(5)}.two% mystruct{(10)}.three$
The syntax is as follows:
The first example declares a structure new_struct{} which has the same layout and member names as the structure proto_struct{}. The second example declares a structure nest_struct{} whose second member pos is a sub-structure with the same layout and member names as the structure proto_struct{}. The third example declares an array (with 2 rows and 3 columns) of structures, each element of which has the same layout and member names as proto_struct{}.DIM new_struct{} = proto_struct{} DIM nest_struct{name$,pos{} = proto_struct{}} DIM struct_array{(1,2)} = proto_struct{}
Note: You cannot use a LOCAL structure as a prototype for another structure.
Here the function FN_distance has been defined with a formal structure parameter s{} and has been called with the actual structure parameter objpos{}; note the use of opening and closing braces with nothing in between. In just the same way as passing an entire array to a procedure or function the parameter is passed by reference so any changes that take place to structure members within the procedure or function affect the values of those members on return. See the section on Procedures and functions for more details.DIM objpos{x,y,z} PRINT FN_distance(objpos{}) END DEF FN_distance(s{}) = SQR(s.x^2 + s.y^2 + s.z^2)
You can also pass entire arrays of structures to procedures and functions:
DIM buffer{(1000) signal%, timestamp%} PROCclear(buffer{()}, 0, 1000) DEF PROCclear(B{()}, start%, finish%) LOCAL I% FOR I%=start% TO finish% B{(I%)}.signal% = 0 B{(I%)}.timestamp% = 0 NEXT ENDPROC
Be careful when passing structures to procedures or functions in libraries. If you crunch your main program with the Abbreviate names option, the names of the structure members will no longer agree with the names they have in the library, unless you use the crunch embedded files option.
All members of a LOCAL structure are initialised to zero or an empty string. Members of a PRIVATE structure are initially zero or empty strings, but subsequently keep their values from one call of the procedure/function to the next.DEF PROCtest1 LOCAL locstruct{} DIM locstruct{a,b,c} DEF FNtest2 PRIVATE pristruct{} DIM pristruct{p%,q%,r%}
If you pass a structure which already exists, it must have an identical format to the declaration within the function or procedure.PROCnewstruct(alpha{}) PROCnewstruct(beta{}) PROCnewstruct(gamma{}) DEF PROCnewstruct(RETURN s{}) DIM s{one,two%,three$} ... ENDPROC
Note: You cannot use this technique to declare LOCAL structures.
In this case pt{} evaluates to the address of the structure in memory, so pt.x% is the same as pt{}!0 and pt.y% is the same as pt{}!4.DIM pt{x%,y%} pt.x% = clientX% pt.y% = clientY% SYS "ClientToScreen", @hwnd%, pt{} screenX% = pt.x% screenY% = pt.y%
In the case of a sub-structure the value returned is the offset from the start of its parent structure, so to discover the memory address of a sub-structure you must add the two together:
If you need to create a structure member of an arbitrary size you can use a byte array. For example the Windows™ OSVERSIONINFO structure consists of five DWORDs (32-bit integers) followed by a 128-byte character array:DIM window{hw%,rc{l%,t%,r%,b%}} window.hw% = @hwnd% SYS "GetWindowRect", window.hw%, window{}+window.rc{}
The address of the ServicePack member can be obtained as osvi{}+20 or as ^osvi.ServicePack&(0). Its value as a NUL-terminated string can be obtained as osvi.ServicePack&().DIM osvi{Size%,Major%,Minor%,Build%,Platform%,ServicePack&(127)}
Here the structure word{} consists of the least-significant byte word.l& and the most-significant byte word.h&. You could write a set of procedures for manipulating 16-bit values stored in such a structure.DIM word{l&,h&}
This can be particularly useful for some structures used by the Windows™ and SDL 2.0 APIs which require the first member to contain the size of the structure:size% = DIM(struct_name{})
DIM osvi{} = _OSVERSIONINFO{} osvi.Size% = DIM(osvi{})
Note that BASIC only checks that the sizes of the structures are the same, not that their contents are compatible. Do not copy structures containing string members; since only the string descriptor is copied, not the string itself, you are likely to confuse BASIC and may even crash it.dest{} = source{}
Another occasion when you may wish to assign a pointer is when you are using structures to implement a linked list. In that case you are likely to want to reserve a block of memory to contain the new node:DIM canvas{f%, p%, w%, h%, s%, p%, u%, l%, d%, X%, Y%, W%, H%, b%, r%} SYS "SDL_CreateRGBSurface", 0, W%, H%, 32, R%, G%, B%, A% TO canvas%% PTR(canvas{}) = canvas%%
Note that the resulting structure is not guaranteed to be aligned.DIM node{link, data...} : REM Define a link node DIM new%% DIM(node{}) : REM Allocate memory for a new mode PTR(node{}) = new%% : REM Point the structure to the new node
Every time BBC BASIC encounters a FOR, REPEAT, WHILE, GOSUB, FN or PROC statement it 'pushes' the return address onto a 'stack' and every time it encounters a NEXT, UNTIL, ENDWHILE or RETURN statement, or the end of a function or procedure, it 'pops' the latest return address off the stack and goes back there.
Apart from memory size, there is no limit to the level of nesting of FOR ... NEXT, REPEAT ... UNTIL, WHILE ... ENDWHILE and GOSUB ... RETURN operations. The untrappable error message 'No room' will be issued if all the stack space is used up.
400 REPEAT 410 INPUT ' "What number should I stop at", num 420 FOR i=1 TO 100 430 PRINT i; 440 IF i=num THEN 460 450 NEXT i 460 UNTIL num=-1
REPEAT INPUT ' "What number should I stop at", num i=0 REPEAT i=i+1 PRINT i; UNTIL i=100 OR i=num UNTIL num=-1
REPEAT INPUT ' "What number should I stop at", num FOR i=1 TO 100 PRINT i; IF i=num THEN i=500 ELSE REM More program here if necessary ENDIF NEXT UNTIL num=-1
REPEAT INPUT ' "What number should I stop at", num FOR i=1 TO 100 PRINT i; IF i=num THEN EXIT FOR REM More program here if necessary NEXT i UNTIL num=-1
If you needed to know whether the loop had been terminated prematurely you could return a different value:DEF PROCloop(num) LOCAL i FOR i=1 TO 100 PRINT i; IF i=num THEN ENDPROC REM More program here if necessary NEXT i ENDPROC
DEF FNloop(num) LOCAL i FOR i=1 TO 100 PRINT i; IF i=num THEN =TRUE REM More program here if necessary NEXT i =FALSE
Fortunately BBC BASIC allows you to make an entire array 'local' as follows:DEF PROC_error_demo FOR i=0 TO 10 LOCAL data(i) NEXT ENDPROC
Note the use of an opening and closing bracket with nothing in between.DEF PROC_error_demo LOCAL data() DIM data(10) ENDPROC
The indirection operators should be used with great care, since attempting to read from or write to an inappropriate address may well crash BBC BASIC. Generally, the address used should either be in an area of memory allocated with DIM or have been returned from a Windows or SDL 2.0 API function.
There are five indirection operators:
Name | Symbol | Data type | No. of bytes |
---|---|---|---|
Query | ? | Byte | 1 |
Pling | ! | Integer (32-bit) | 4 |
Bracket | ] | Integer (64-bit) | 8 |
Pipe | | | Float | 5, 8 or 10 |
Dollar | $, $$ | String | 1 to 65536 |
Thus, '?' provides a direct replacement for PEEK and POKE.DIM M 0 ?M=&23 number=?M PRINT ?M
The range of values which may be stored is 0 to 255 (i.e. byte indirection is unsigned).?A=B is equivalent to POKE A,B B=?A is equivalent to B=PEEK(A)
DIM count% 0 FOR ?count%=0 TO 20 - - - - - - NEXT
would loadDIM M 3 !M=&12345678
&78 into address Mand
&56 into address M+1
&34 into address M+2
&12 into address M+3
would givePRINT ~!M
12345678The range of values which may be stored is −2147483648 to +2147483647 (i.e. integer indirection is signed).
would load*HEX 64 DIM M 7 ]M=&123456789ABCDEF0
&F0 into address Mand
&DE into address M+1
&BC into address M+2
&9A into address M+3
&78 into address M+4
&56 into address M+5
&34 into address M+6
&12 into address M+7
would give*HEX 64 PRINT ~]M
123456789ABCDEF0The range of values which may be stored is −9223372036854775808 to +9223372036854775807.
|F% = PIwould load the floating-point representation of PI into addresses F% to F%+4, addresses F% to F%+7 or addresses F% to F%+9 depending on the *FLOAT mode in effect.
$M="ABCDEF"would load the ASCII characters A to F into addresses M to M+5 and &0D into address M+6, and
PRINT $Mwould print
ABCDEF
SYS "GetCommandLine" TO cmdline% cmdline$ = $$cmdline%
Thus,memory?5=&50 PRINT memory?5
The example below displays the contents of the first 13 bytes of the current BASIC program:A?I=B is equivalent to POKE A+I,B B=A?I is equivalent to B=PEEK(A+I)
The memory address and the contents are printed in hexadecimal format.memory=PAGE FOR offset=0 TO 12 PRINT ~memory+offset, ~memory?offset NEXT
If M is the address of the start of the structure then:
$M is the stringLinked lists and tree structures can easily be created and manipulated in memory using this facility.
M?11 is the 8 bit number
M!12 is the address of the related structure
This will work for all types of variable (integer, floating-point, string, array etc.) but in the case of a normal string variable the address returned is not that of the first character of the string but of the 6-byte or 8-byte string descriptor (see the CALL statement for details of string descriptors). Therefore the address of the string itself is !^string$ or alternatively PTR(string$).A% = 1234 PRINT !^A%
In the case of an array the address returned by ^array() is that of a pointer to the array parameter block, therefore the address of the parameter block is !^array(). To obtain the address of the array data you should specify the name of the first element, e.g. ^array(0).
Knowing the memory address of a variable may seem to be of little value but can be useful in special circumstances, particularly when calling Windows™ or SDL 2.0 API functions or in assembly language code. For example the ReadFile function requires you to pass the address of a variable in which the number of bytes read will be returned:
In assembly language code you might want to copy the value of a BBC BASIC (integer) variable into one of the processor's registers:SYS "ReadFile", @hfile%(file%), store%, size%, ^bytesread%, 0
Note that the static variables A% to Z% occupy consecutive locations in memory, which can be handy when an API function requires the address of a structure rather than a simple variable. For example GetCursorPos requires the address of a POINT structure in which it returns two values:mov eax,[^variable%]
This will return the horizontal coordinate of the cursor in X% and the vertical coordinate in Y%.SYS "GetCursorPos", ^X%
Another use is to alter the byte-order of a variable, for example from little-endian to big-endian. The following code segment reverses the byte-order of the value stored in A%:
SWAP ?(^A%+0), ?(^A%+3) SWAP ?(^A%+1), ?(^A%+2)
? | An operator giving 8 bit indirection. |
! | An operator giving 32 bit indirection. |
] | An operator giving 64 bit indirection (when used alone). |
" | A delimiting character for constant strings. Strings always have an even number of " in them. " may be introduced into a string by the escape convention "". |
# | As a prefix indicates a file channel number (and is not optional). As a suffix indicates a 64-bit double variable. |
$ | As a prefix indicates a 'fixed string' (the syntax $<expression> is used to position a string anywhere in memory, overriding the interpreter's space allocation). As a suffix on a variable name, indicates a string variable. $$ indicates a NUL-terminated string. |
% | As a prefix indicates a binary constant e.g. %11101111. As a suffix on a variable name, indicates an integer (signed 32-bit) variable; %% indicates a signed 64-bit variable. |
& | As a prefix indicates a hexadecimal constant e.g. &EF. As a suffix on a variable name, indicates a byte (unsigned 8-bit) variable. |
' | A character which causes new lines in PRINT or INPUT. |
( ) | As a suffix on a variable name indicates an array. Objects in parentheses have highest priority. Delimiter for line labels. |
{ } | Braces indicate a structure. |
= | 'Becomes' for LET statement and FOR; 'result is' for FN; relation of equal to. |
− | Negation and subtraction operator. |
* | Multiplication operator; prefix indicating operating system command (e.g. *DIR). |
: | Multi-statement line statement delimiter. |
; | Suppresses forthcoming action in PRINT, BPUT#, THEN and *RUN. Comment delimiter in the assembler. Delimiter in VDU and INPUT. |
+ | Unary plus and addition operator; concatenation between strings. |
, | Delimiter in lists. |
. | Decimal point in real constants; abbreviation symbol on keyword entry; introduce label in assembler; dot product of arrays; delimiter of structure members. |
< | Relation of less than. |
<< | Left-shift operator (signed or unsigned). |
<<< | Left-shift operator (always 64-bits, irrespective of the *HEX mode). |
> | Relation of greater than. |
>> | Right-shift operator (signed) (always 64-bits, irrespective of the *HEX mode). |
>>> | Right-shift operator (unsigned). |
/ | Division operator. |
\ | The line continuation character. |
@ | Prefix character for system variables. |
<= | Relation of less than or equal to. |
>= | Relation of greater than or equal to. |
<> | Relation of not equal to. |
== | Relation of equal to (alternative to =). |
[ ] | Delimiters for assembler statements. Statements between these delimiters may need to be assembled twice in order to resolve any forward references. The pseudo operation OPT (initially 3) controls errors and listing. |
^ | Exponentiation (raise-to-the-power-of) operator; the address of operator. |
~ | A character in the start of a print field indicating that the item is to be printed in hexadecimal. Also used with STR$ to cause conversion to a hexadecimal string. |
| | A delimiter in the VDU statement. A unary operator giving floating-point indirection. |
+= | Assignment with addition (A += B is equivalent to A = A + B) |
−= | Assignment with subtraction (A −= B is equivalent to A = A − B) |
*= | Assignment with multiplication (A *= B is equivalent to A = A * B) |
/= | Assignment with division (A /= B is equivalent to A = A / B) |
AND= | Assignment with AND (A AND= B is equivalent to A = A AND B) |
DIV= | Assignment with DIV (A DIV= B is equivalent to A = A DIV B) |
EOR= | Assignment with EOR (A EOR= B is equivalent to A = A EOR B) |
MOD= | Assignment with MOD (A MOD= B is equivalent to A = A MOD B) |
OR= | Assignment with OR (A OR= B is equivalent to A = A OR B) |
In general variable names cannot start with a keyword, but there are some exceptions. For example the constant PI and the pseudo-variables LOMEM, HIMEM, PAGE, and TIME can form the first part of the name of a variable. Therefore PILE and TIMER are valid variable names although PI$ and TIME% are not.
37 out of the total of 138 keywords are allowed at the start of a variable name. They are shown in bold type in the table below:
It is inevitable that programs will sometimes contain bugs. You may be able to write small programs which are error-free and work first time, but the larger your programs get the greater the likelihood that they will have errors. These fall into three categories:
when what you meant was:PRONT "Hello world!"
When the interpreter encounters the word PRONT it won't know what to do, and will report a Mistake error.PRINT "Hello world!"
is syntactically correct, and generally won't result in an error, but if variable B is zero it will result in the Division by zero error. It could also result in the Number too big error if A is very large and B is very small.answer = A / B
Errors of logic and computational errors are best avoided at the design stage, before you even start writing code, but this ideal situation is rarely achieved in practice.
BBC BASIC for Windows and BBC BASIC for SDL 2.0 incorporate a number of features to make it easier to find and fix bugs.
If you hover the mouse over the highlighted statement, the error message will be displayed alongside in a tooltip. The statement remains highlighted so long as your program's output window remains open, or until you enter a command in immediate mode.
In Trace mode, the program statement which is currently being executed is highlighted, and this highlight moves as your program is running (unless the statement currently being executed is in a CALLed or INSTALLed module). If your program gets stuck in an 'infinite loop', you will be able to see what part of the program is being executed. If your program stops running because of an error (or an END or STOP statement) the last statement executed will remain highlighted for as long as the program's output window remains open, or until you enter a command in immediate mode.
If List Variables is enabled a window opens in which the values of the currently-defined variables are displayed (along with a list of arrays and function and procedure names). This list is updated as your program runs so you can monitor how the values change and possibly spot anomalous values. Note that variables are only listed once they have been accessed by your program (except for the static integer variables which are always listed).
ON ERROR ....and
ON ERROR LOCAL ....
If an error was detected in a program after this line had been encountered, the message 'Oh No!' would be printed and the program would terminate. If, as in this example, the ON ERROR line contains the END statement or transfers control elsewhere (e.g. using GOTO) then the position of the line within the program is unimportant so long as it is encountered before the error occurs. If there is no transfer of control, execution following the error continues as usual on the succeeding line, so in this case the position of the ON ERROR line can matter.ON ERROR PRINT '"Oh No!":END
As explained in the Program Flow Control sub-section, every time BBC BASIC encounters a FOR, REPEAT, GOSUB, FN, PROC or WHILE statement it 'pushes' the return address on to a 'stack' and every time it encounters a NEXT, UNTIL, RETURN, ENDWHILE statement or the end of a function or procedure it 'pops' the latest return address of the stack and goes back there. The program stack is where BBC BASIC records where it is within the structure of your program.
When an error is detected by BBC BASIC, the stack is cleared. Thus, you cannot just take any necessary action depending on the error and return to where you were because BBC BASIC no longer knows where you were.
If an error occurs within a procedure or function, the value of any parameters and LOCAL variables will be the last value they were set to within the procedure or function which gave rise to the error (the values, if any, they had before entry to the procedure or function are lost).
If an error occurs within a procedure or function containing PRIVATE variables, all subsequent calls to that procedure or function will be treated by BASIC as if they were re-entrant and the PRIVATE statement will be ignored. It is therefore most important that, if your program attempts to recover from errors using ON ERROR, no errors are permitted to occur within a procedure or function containing PRIVATE variables. If necessary use *ESC OFF to ensure that not even the Escape error can occur, or use ON ERROR LOCAL and RESTORE LOCAL to restore the PRIVATE variables.
The following example program will continue after the inevitable 'Division by zero' error:
This is, of course, a poor use of error trapping. You should test for n=0 rather than allow the error to occur. However, it does provide a simple demonstration of the action of ON ERROR LOCAL. Also, you should test ERR to ensure that the error was the one you expected rather than, for example, Escape (ERR is a function which returns the number of the last error; it is explained later in this sub-section).FOR n=-5 TO 5 ok% = TRUE ON ERROR LOCAL PRINT "Infinity" : ok% = FALSE IF ok% PRINT "The reciprocal of ";n;" is ";1/n NEXT n
After the loop terminates (when 'n' reaches 6) the previous error trapping state is restored. Alternatively, you can explicitly restore the previous state using RESTORE ERROR.
You can call a subroutine, function or procedure to 'process' the error, but it must return to the loop, subroutine, function or procedure where the error was trapped:
The position of the ON ERROR LOCAL within a procedure or function can be important. Consider the following two examples:x=OPENOUT "TEST" FOR i=-5 TO 5 ok% = TRUE ON ERROR LOCAL PROCerr : ok% = FALSE IF ok% PRINT#x,1/i NEXT CLOSE#x : x=OPENIN "TEST" REPEAT INPUT#x,i PRINT i UNTIL EOF#x CLOSE#x END : DEF PROCerr IF ERR <> 18 REPORT:END PRINT#x,3.4E38 ENDPROC
DEF PROC1(A) ON ERROR LOCAL REPORT : ENDPROC LOCAL B ....
In the case of the first example, if an error occurs within the procedure, the formal parameter A will be automatically restored to its original value, but the LOCAL variable B will not; it will retain whatever value it had when the error occurred. In the case of the second example, both the formal parameter A and the LOCAL variable B will be restored to the values they had before the procedure was called.DEF PROC1(A) LOCAL B ON ERROR LOCAL REPORT : ENDPROC ....
If a procedure or function uses PRIVATE variables it is essential that any ON ERROR LOCAL statement be placed after the PRIVATE statement.
At first sight ON ERROR LOCAL seems a more attractive proposition since BBC BASIC remembers where it is within the program structure. The one disadvantage of ON ERROR LOCAL is that you will need to include an error handling section at every level of your program where you need to trap errors. Many of these sections of program could be identical.
You can mix the use of ON ERROR and ON ERROR LOCAL within your program. A single ON ERROR statement can act as a 'catch all' for unexpected errors, and one or more ON ERROR LOCAL statements can be used when your program is able to recover from predictable errors. ON ERROR LOCAL 'remembers' the current ON ERROR setting, and restores it when the loop, procedure or function containing the ON ERROR LOCAL command is finished, or when a RESTORE ERROR or RESTORE LOCAL is executed.
The example below does not try to deal with errors, it just uses ERR and REPORT to tell the user about the error. Its only advantage over BBC BASIC's normal error handling is that it gives the error number; it would probably not be used in practice. As you can see from the second run, pressing <ESC> is treated as an error (number 17).
Example run:ON ERROR PROCerror REPEAT INPUT "Type a number " num PRINT num," ",SQR(num) PRINT UNTIL FALSE : DEF PROCerror PRINT PRINT "Error No ";ERR REPORT END
The example below has been further expanded to include error trapping. The only 'predictable' error is that the user will try a negative number. Any other error is unacceptable, so it is reported and the program aborted. Consequently, when <ESC> is used to abort the program, it is reported as an error. However, a further test for ERR=17 could be included so that the program would halt on ESCAPE without an error being reported.RUN Type a number 1 1 1 Type a number -2 -2 Error No 21 Negative root
RUN Type a number <Esc> Error No 17 Escape
This is a case where the placement of the ON ERROR statement is important. When PROCerror exits, execution continues after the ON ERROR statement, which in this case causes the program to restart from the beginning.ON ERROR PROCerror REPEAT INPUT "Type a number " num PRINT num," ",SQR(num) PRINT UNTIL FALSE : DEF PROCerror PRINT IF ERR=21 THEN PRINT "No negatives":ENDPROC REPORT END
Example run:
The above example is very simple and was chosen for clarity. In practice, it would be better to test for a negative number before using SQR rather than trap the 'Negative root' error. A more realistic example is the evaluation of a user-supplied HEX number, where trapping 'Bad hex or binary' would be much easier than testing the input string beforehand.RUN Type a number 5 5 2.23606798
Type a number 2 2 1.41421356
Type a number -1 -1 No negatives
Type a number 4 4 2
Type a number <Esc> Escape
The next example is similar to the previous one, but it uses the ON ERROR LOCAL command to trap the error.ON ERROR PROCerror REPEAT INPUT "Type a HEX number " Input$ num=EVAL("&"+Input$) PRINT Input$,num PRINT UNTIL FALSE : DEF PROCerror PRINT IF ERR=28 THEN PRINT "Not hex":ENDPROC REPORT END
Note that had ON ERROR (rather than ON ERROR LOCAL) been used in this case, an error would give rise to a Not in a REPEAT loop error at the UNTIL statement. This is because ON ERROR clears the program's stack.REPEAT ON ERROR LOCAL PROCerror INPUT "Type a HEX number " Input$ num=EVAL("&"+Input$) PRINT Input$,num PRINT UNTIL FALSE : DEF PROCerror PRINT IF ERR=28 THEN PRINT "Not hex":ENDPROC REPORT END
Arguably, the major advantage of procedures and functions is that they can be referred to by name. Consider the two similar program lines below.
The first statement gives no indication of what the subroutines at lines 500 and 800 actually do. The second, however, tells you what to expect from the two procedures. This enhanced readability stems from the choice of meaningful names for the two procedures.IF name$="ZZ" THEN GOSUB 500 ELSE GOSUB 800
IF name$="ZZ" THEN PROC_end ELSE PROC_print
A function often carries out a number of actions, but it always produces a single result. For instance, the 'built in' function INT returns the integer part of its argument.
age=INT(months/12)A procedure on the other hand, is specifically intended to carry out a number of actions, some of which may affect program variables, but it does not directly return a result.
Whilst BBC BASIC has a large number of pre-defined functions (INT and LEN for example) it is very useful to be able to define your own to do something special. Suppose you had written a function called FN_discount to calculate the discount price from the normal retail price. You could write something similar to the following example anywhere in your program where you wished this calculation to be carried out.
It may seem hardly worth while defining a function to do something this simple. However, functions and procedures are not confined to single line definitions and they are very useful for improving the structure and readability of your program.discount_price=FN_discount(retail_price)
Function and procedure names may end with a '$'. However, this is not compulsory for functions which return strings.PROCPRINTDETAILS FNDISCOUNT
PROC_print_details FN_discount
If two or more functions or procedures have the same name (either deliberately or by mistake) the one which is executed is determined by the following rules:
DEF FN_average(A,B) = (A+B)/2 DEF PROC_clear:PRINT SPC(40-POS);:ENDPROC
DEF PROC_clear_to_end LOCAL i%,x%,y% x%=POS:y%=VPOS REM If not already on the last line, print lines of REM spaces which will wrap around until the last line IF y% < 24 FOR i%=y% TO 23:PRINT SPC(80);:NEXT REM Print spaces to the end of the last line. PRINT SPC(80-x%); REM Return the cursor to its original position PRINT TAB(x%,y%); ENDPROC
In this case, to use the function we would need to pass two parameters.DEF FN_discnt(price,pcent)=price*(1-pcent/100)
orretail_price=26.55 discount_price=FN_discount(retail_price,25)
orprice=26.55 discount=25 price=FN_discount(price,discount)
price=FN_discount(26.55,25)
The formal parameters must be variables, but the actual parameters may be variables, constants or expressions. When the actual parameter is a variable, it need not be (and usually won't be) the same as the variable used as the formal parameter. Formal parameters are automatically made local to the procedure or function.FOR I% = 1 TO 10 PROC_printit(I%) : REM I% is the Actual parameter NEXT END DEF PROC_printit(num1) : REM num1 is the Formal parameter PRINT num1 ENDPROC
You can pass a mix of string and numeric parameters to the procedure or function, and a function can return either a string or numeric value, irrespective of the type of parameters passed to it. However, you must make sure that the parameter types match up. The first example below is correct; the second would give rise to an 'Incorrect arguments' error message and the third would cause a 'Type mismatch' error to be reported.
PROC_printit(1,"FRED",2) END : DEF PROC_printit(num1,name$,num2) PRINT num1,name$,num2 ENDPROC
PROC_printit(1,"FRED",2,4) END : DEF PROC_printit(num1,name$,num2) PRINT num1,name$,num2 ENDPROC
PROC_printit(1,"FRED","JIM") END : DEF PROC_printit(num1,name$,num2) PRINT num1,name$,num2 ENDPROC
Declaring variables as LOCAL initialises them to zero (in the case of numeric variables) or null/empty (in the case of string variables). Declaring variables as PRIVATE causes them to retain their values from one call of the function/procedure to the next. If an array or structure is made LOCAL or PRIVATE it must be re-DIMensioned before use.
Variables which are not formal parameters nor declared as LOCAL or PRIVATE are known to the whole program, including all the procedures and functions. Such variables are called global.
Since 'n' is the input variable to the function FN_fact_num, it is local to each and every use of the function. The function keeps calling itself until it returns the answer 1. It then works its way back through all the calls until it has completed the final multiplication, when it returns the answer. The limit of 35 on the input number prevents the answer being too big for the computer to handle.REPEAT INPUT "Enter an INTEGER less than 35 "num UNTIL INT(num)=num AND num<35 fact=FN_fact_num(num) PRINT num,fact END : DEF FN_fact_num(n) IF n=1 OR n=0 THEN =1 REM Return with 1 if n= 0 or 1 =n*FN_fact_num(n-1) REM Else go round again
Note the use of DIM as a function to return the number of elements in the array.DIM arr(20) FOR n%=0 TO 20 : arr(n%) = n% : NEXT PROC_multiply(arr(), 2) FOR n%=0 TO 20: PRINTarr(n%) : NEXT END : DEF PROC_multiply(B(), M) LOCAL n% FOR n% = 0 TO DIM(B(),1) B(n%) = B(n%) * M NEXT ENDPROC
The advantage of passing an array as a parameter, rather than accessing the 'global' array directly, is that the function or procedure doesn't need to know the name of the array. Ideally a FN/PROC shouldn't need to know the names of variables used in the main program from which it is called, and the main program shouldn't need to know the names of variables contained in a function or procedure it calls. This is a principle known as information hiding and is especially important in the case of functions and procedures contained in an INSTALLed library.
If a parameter passed by reference is modified within the function or procedure, it remains modified on exit. This is the case even if the actual parameter and the formal parameter have different names.DEF PROCcomplexsquare(RETURN r, RETURN i)
Suppose you want to write a function which returns a complex number. Since a complex number is represented by two numeric values (the real part and the imaginary part) a conventional user-defined function, which can return only a single value, is not suitable. Conventionally you would have to resort to using global variables or an array or structure to return the result, but by passing parameters by reference this can be avoided.
The following example shows the use of a procedure to return the square of a complex number:
INPUT "Enter complex number (real, imaginary): " real, imag PROCcomplexsquare(real, imag) PRINT "The square of the number is ";real " + ";imag " i" END DEF PROCcomplexsquare(RETURN r, RETURN i) LOCAL x,y x = r : y = i r = x^2 - y^2 i = 2 * x * y ENDPROC
You can use a similar technique to return strings rather than numbers. The following example, rather pointlessly, takes two strings and mixes them up by exchanging alternate characters between them:
You can also use this facility to create a variable whose name is determined at run-time, something which is otherwise impossible to achieve:INPUT "Enter two strings, separated by a comma: " A$, B$ PROCmixupstrings(A$, B$) PRINT "The mixed-up strings are " A$ " and " B$ END DEF PROCmixupstrings(RETURN a$, RETURN b$) LOCAL c$, I% FOR I% = 1 TO LEN(a$) STEP 2 c$ = MID$(a$,I%,1) MID$(a$,I%,1) = MID$(b$,I%,1) MID$(b$,I%,1) = c$ NEXT I% ENDPROC
INPUT "Enter a variable name: " name$ INPUT "Enter a numeric value for the variable: " value$ dummy% = EVAL("FNassign("+name$+","+value$+")") PRINT "The variable "name$" has the value ";EVAL(name$) END DEF FNassign(RETURN n, v) : n = v : = 0
If you pass an array which already exists, it must have an identical format to the declaration within the function or procedure.PROCnewarray(alpha()) PROCnewarray(beta()) PROCnewarray(gamma()) DEF PROCnewarray(RETURN a()) DIM a(3, 4, 5) ... ENDPROC
Note: You cannot use this technique to declare LOCAL arrays.
The above code calls the procedure PROC1 (without any parameters) but the name of the procedure is specified separately from the call itself. This allows you to do things such as determine which procedure or function should be called at run time in a more flexible way than can be achieved using ON PROC or EVAL. For example you could have an array of function pointers and select the wanted one according to an index value:pptr%% = ^PROC1 REM .... PROC(pptr%%)
If the procedure or function takes parameters then you must include a pair of parentheses when you use ^ to find the pointer value:DIM fptr%%(4) fptr%%() = ^FNzero, ^FNone, ^FNtwo, ^FNthree, ^FNfour REM .... answer = FN(fptr%%(index%))
There are two very important caveats when using indirect procedure and function calls. Firstly, at least one conventional PROC or FN call must have been made before you attempt to read a procedure or function pointer (if not a No such FN/PROC error will result). Secondly, you must be very careful to make an indirect call only with a valid procedure or function pointer; using an incorrect value may well crash BASIC.DIM fptr%%(4) fptr%%() = ^FN0(), ^FN1(), ^FN2(), ^FN3(), ^FN4() REM .... answer = FN(fptr%%(index%))(parameters)
Within FN_button an integer variable should be used to receive the procedure or function's address. An indirect procedure or function call can then be made using this variable:hb% = FN_button("Name",10,100,32,12,PROCbut)
Note that the use of RETURN is mandatory. In the case of a function being passed rather than a procedure, this ensures that it's the function's address not its value that is passed into FN_button. As before you must be very careful to use only a valid procedure or function pointer; using an incorrect value (e.g. as a result of omitting the ^) is likely to crash BASIC.DEF FN_button(text$,x%,y%,cx%,cy%,RETURN pr%) REM .... IF button_pressed PROC(^pr%)
Key | Operation |
---|---|
Insert | Toggle between insert and overtype modes. In overtype mode any character typed will over-write the existing character at that position on the line. In insert mode any character typed will 'push' the rest of the line (if any) to the right to make room. The mode is indicated by the shape of the cursor: normally an underline indicates insert mode and a solid block indicates overtype mode. |
Backspace | Delete the character immediately to the left of the cursor, and move the cursor left one position. The rest of the line (if any) is moved left to fill the gap. If the cursor is already at the beginning of a line, the key has no effect. |
Delete | Delete the character at the cursor position and move the rest of the line (if any) left to fill the gap. If the cursor is already at the end of the line the key has no effect. |
Home | Move the cursor to the start of the line. |
End | Move the cursor to the end of the line. |
Move the cursor one character to the left; if the cursor is already at the beginning of a line, the key has no effect. | |
Move the cursor one character to the right; if the cursor is already at the end of the line, the key has no effect. | |
Ctrl+U | Delete everything to the left of the cursor. If the cursor is already at the beginning of the line the key has no effect. |
Ctrl+V | Paste in the contents of the clipboard (if any). If there is more than one line of text in the clipboard only the first line is pasted; the rest is discarded. |
Tab | Enter Copy key editing mode. |
Shift/Tab | Exit Copy key editing mode. |
In situations when use of the INPUT statement is unacceptable (for example if a proportional-spaced font has been selected, or it's important that event interrupts not be delayed) you can use a replacement written in BASIC. Examples are the FNpsinput routine listed here or the FNinput routine contained in the NOWAIT library.
When you press the Tab (copy) key, two cursors are displayed. The large 'block' cursor shows where anything you enter will appear; it is called the write cursor. The other small flashing cursor is the one that can be moved around by the cursor control keys. For reasons which will become clear later, this is called the read cursor.
You can exit the copy-key editing mode (without pressing Enter) by pressing Shift/Tab; this restores the normal line-editing features. Thus, you can switch between the two forms of editing by using Tab and Shift/Tab.
You don't have to confine yourself to copying from a single line. You can move the read cursor around the screen and copy any character or sequence of characters displayed in any position on the screen. You can copy the same thing over and over again, or skip bits of a line by moving the read cursor over words you don't want in the new line.
Printer output is enabled using VDU 2 and disabled using VDU 3. Therefore to output to both the printer and the screen you would do:
Note the VDU 1,12; that tells the printer that the page is complete and instructs it to print and eject the sheet. Without it the page won't be printed until your program exits or a timeout occurs.VDU 2 : REM Turn printer on PRINT "Here is some output to the printer." PRINT "It will also appear on the screen." VDU 1,12,3 : REM Eject page and turn printer off
If you want to output only to the printer and not to the screen you can redirect output to the printer using *OUTPUT 15:
If your printout isn't the right size, or you want to select a different font, add a *PRINTERFONT command at the beginning of your program, for example:*OUTPUT 15 PRINT "Here is some output to the printer." PRINT "It will not appear on the screen." *OUTPUT 0 VDU 2,1,12,3
The *MARGINS command resets the margins to the default size of 10 mm.*PRINTERFONT Courier New,12 *MARGINS 10,10,10,10
Note that the label must be enclosed in parentheses, and be the first thing on a program line. Labels and variables share the same namespace so you should not normally use the same name for a label and a variable in your program.A = 5 (loop) PRINT A A = A + 1 IF A < 10 THEN GOTO (loop)
Labels are limited in much the same way as line numbers: they are global to the whole program and cannot be used in INSTALLed or CALLed modules. You are advised only to use labels for debugging, or when converting a program from another dialect of BASIC.
Once UTF-8 mode has been selected, all text output to the screen or the printer uses the multibyte UTF-8 character set; each character requiring 1, 2 or 3 bytes depending on its position within the Basic Multilingual Plane. The following code snippet illustrates its use:
In BBC BASIC for SDL 2.0 substitute this font selection:VDU 23,22,640;512;8,16,16,8 *FONT Arial,24 aleph$ = CHR$(&D7)+CHR$(&90) thorn$ = CHR$(&C3)+CHR$(&9E) euro$ = CHR$(&E2)+CHR$(&82)+CHR$(&AC) PRINT aleph$ " " thorn$ " " euro$
Because UTF-8 text uses a variable number of bytes for each character, features that rely on counting characters (for example COUNT, WIDTH and TAB(X)) won't work reliably, even when a fixed-pitch font is selected.OSCLI "FONT """+@lib$+"DejaVuSans"",24"
CONTENTS |
CONTINUE |