Quantcast
Channel: stardot.org.uk
Viewing all articles
Browse latest Browse all 3322

programming • Re: Understanding how Basic programs are encoded

$
0
0
On compiling BBC BASIC into an intermediate code, there could be issues with

Code:

ON v% GOTO
and the same for GOSUB. But BASIC does do some things like a kind of virtual machine. For example, there is the integer work area which is a a bit like a 32-bit accumulator. There are subroutines in the ROM to do the various arithmetic operations between this and an integer elsewhere in memory as well as to load/store this value to somewhere else in memory. Those can be called by routines to push/pop this value onto BASIC's stack and also loads/stores into variables. Likewise there are pair of areas for floating point that work the same way with routines to unpack a value in memory to FPA etc. and one to re-pack the value and put it back in memory as well as the FP operations between FPA and FPB. You could assign opcodes to those functions and you have an intermediate code. Assuming you keep the tokens for the command keywords and built-in functions, once you compile into intermediate code, the printing ASCII codes become available for your new opcodes as there is no longer a need to include variable names or the digits of a number in the final form of the program.

There are some interesting quirks in BBC BASIC you would need to take account of. Most compiled languages have lexically scoped control structures like FOR...NEXT, REPEAT...UNTIL etc. so a NEXT refers back to the FOR that precedes in the program as written. BBC BASIC uses a stack instead so this works by execution order. When a FOR is executed, it puts a record on a stack and NEXT accesses that entry and, if it is the last time around, POPs it. That means there can be more than one NEXT for the same FOR, in different parts of the program. IF...ELSE is lexically scoped, though.

Statistics: Posted by Coeus — Sat Nov 09, 2024 12:36 am



Viewing all articles
Browse latest Browse all 3322

Trending Articles