The following text is a slightly revised version of what I presented on the CoCoTalk video podcast.
Some of the text was simplified for a re-recording of the show and I may have added one new section. The rest of the discussion was off script.
Some of the text was simplified for a re-recording of the show and I may have added one new section. The rest of the discussion was off script.
Why is Microsoft BASIC so slow?
A brief introduction to the inner workings of
Microsoft BASIC
on the CoCo and MC-10
Index
- What is an interpreter?
- The Tokenizer
- 8 bit vs 16 bit and the 6800 legacy
- Floating Point Math
- Variable Storage
- Runtime math formula evaluation
- Garbage Collection
- Running a program step by step
What is an interpreter?
From wikipedia:
“In computer science, an interpreter is a computer program that directly executes, i.e. performs, instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. An interpreter generally uses one of the following strategies for program execution:
parse the source code and perform its behavior directly
...”
The Microsoft BASIC Interpreter...
- Makes no attempt to convert the program to machine code
- The code is still human readable or can be quickly translated back to human readable code
- Makes minimal changes to the source code to speed up interpretation
- All syntax checking is left for runtime execution
The Tokenizer
- Changes the line number at the beginning of a line to a 16 bit integer
- Converts BASIC keywords to tokens
- Terminates the line with a zero
- Inserts the line of code into the program linked list
- Does not perform any syntax checking
- Does not convert other numbers from ASCII to computer readable form, therefore they must be converted every time they are needed, and conversion uses one of the slowest math functions, division
8 bit vs 16 bit and the 6800 legacy
- The CoCo and MC-10 interpreters are mostly based on Microsoft's 6800 version and have minimal optimizations that take advantage of the 6809's or 6803's additional 16 bit features, new registers, or hardware multiply instruction.
- The math library is largely unchanged from the 6800 version. It is still oriented around the use of 8 bit registers, making it slower and larger than it needs to be. It is so complex, it occupies ¼ of the CoCo Color BASIC and MC-10 ROMs
- Functions such as memory moves, screen scrolls, clearing screens, etc... all use 8 bit code even though 16 bit code would be significantly faster
Floating Point Math
- All numeric variables are stored as floating point
- All math is performed using floating point
- Floating point math is performed similar to integer math, but it uses larger numbers and requires extra steps such as normalization, converting numbers to the same exponent, and dealing with the sign
- Many fast floating point math functions such as calculating a square root, did not exist yet
- All math is performed using software floating point registers reserved in memory. This requires constantly coping numbers back and forth between the software registers and memory
- Floating point numbers are stored in variables using a packed form, but must be unpacked to load key floating point registers, then repacked to move them back to variables
- Some numbers do not have exact floating point equivalents making pre-conversion of constants impractical
Runtime Math Evaluation
- Math formulas must be parsed in order to determine proper order of operations.
- A list of operations is built and then is processed by the interpreter.
- This must be done every time you use a math function, even something as simple as A=A+1
Variable Storage
- Variables are added to system memory in the order they are first used, and then by variable type. Every time a new variable is created, space must be allocated for it in the proper area of the variable list. This can include copying a large block for RAM to make room
- The interpreter must search through the variable list every time a variable is used in order to use it
- The interpreter allocates space for the stack and variables at startup
- It must perform checks every time a variable is created, a string is modified, or the stack is used to insure that the program and variables are not overwritten
Garbage Collection
- As string variables change in size, additional space must be allocated or freed to keep from running out of variable storage space. This can involve moving large blocks of memory
- Every time the system does this, it requires copying the blocks of memory where a string is stored and any strings after it up or down in memory as needed
- All memory copies in the CoCo and MC-10 ROMs use 8 rather than 16 bits
Running a program step by step
- Every time the program looks for a new command, it starts by checking for the BREAK key, which is slow
- All characters from the program are read through a call to an inefficient subroutine
- Command tokens must be range checked before commands are executed
- The token must be used to look up the address of the command and then the command itself is called
- Commands must perform additional parsing and syntax checking, on completion, they return to the main loop
- Every GOTO and GOSUB requires searching through the linked list of lines
- Whenever a line such as an IF THEN skips the rest of a line, the interpreter must search for end of line byte by byte
- This sequence is repeated for every command in a line of code
No comments:
Post a Comment