Home | Reviews | GUIpedia | Forum | Fun500
Todd | Low-level programmingI know most GUIs nowadays are implementing a scripting language as a form of writing applications. The problem in writing the scripting engine is that for maximum performance, the language should be very low-level.
So let's say you have something like this:
[code]PRINT "Hello, what's your name?"
INPUT n$
PRINT "Hello " + n$[/code]
And your scripting engine can interpret it, so it would go and perform the following tasks:
[code]1. Load script/read directly from file
2. Match keyword "PRINT" and perform PRINT routine
3. Copy `"Hello, what's your name?"` into variable
4. Evaluate variable value for expressions
5. Print variable value
6. Read next line
7. Match keyword "INPUT" and perform INPUT routine
8. Copy "n$" as variable name
9. Input data into variable array under "n$"
10. Read next line
11. Match keyword "PRINT" and perform PRINT routine
12. Copy `"Hello " + n$` into variable
13. Evaluate variable value for expressions
14. Match "+" as appendage to string
15. Replace "n$" with variable value
16. Print final value
[/code]
Now with a virtual machine, the code would look like this:
[code]
/*
1 - Loads data into buffer 1
2 - Prints data from buffer 1 and clears buffer 1
3 - Inputs data to buffer 1
4 - Transfers data from buffer 1 to buffer 2
5 - Transfers data from buffer 2 to buffer 1
6 - Prints new line (carriage return)
*/
1Hello, what's your name?
2
6
3
4
1Hello
2
5
2
[/code]
The virtual machine would run this:
[code]1. Load script/read directly from file
2. First character of every line indicates a command
3. Matches 1 and loads value into buffer 1
4. Matches 2 and prints buffer 1
5. Matches 6 and prints new line
6. Matches 3 and inputs string to buffer 1
7. Matches 4 and transfers buffer 1 to buffer 2
8. Matches 1 and loads "Hello " into buffer 1
9. Matches 2 and prints buffer 1
10. Matches 5 and transfers buffer 2 to buffer 1
11. Matches 2 and prints buffer 1
[/code]
As you notice, the code is much smaller and simpler for the interpreter/VM. This is a method we should use when developing GUIs and anything that uses a scripting language. You should offer a higher-level language which can parse and interpret the BASIC code into VM code. That way on runtime, the parser will not be needed since the interpreter will be reading simple VM code.
Here's a link with an example VM I made demonstrating this capability and purpose: http://www.sixbynine.net/filequick/113 | 2008-09-16 | 2:46 PM |
ksr | Re:Low-level programmingThis low-level 'dumb' code is often called bytecode. I made a little ASM-like language once, and had a 'packer' that took an input, such as this factors program:
[code]/// factors.asm
/// lists all factors of a given number.
LDA 12 /// input number (A)
STA $4 /// store in $4
STA $1 /// also store in $1
LBL 0
LDA $4
STA $1
LDA $2
INC 1
STA $2
LBL 1
LDA $1
DEC $2
STA $1
STA $3
LDA $3
LBL 2
INC 1
CSE $2
JMP 3
JMP 4
LBL 3
CSE 15
JMP 2
JMP 1
LBL 4
LDA $2
CSE $4
JMP 5
LDA $2
STA $0
END
LBL 5
LDA $1
CSE 0
JMP 0
LDA $2
STA $0
JMP 0[/code]
And outputted a hex file:
[code]
1C3431A02431224132A12172313323A241E2B3B4A3DFB2B1A4
22E4B52230C0A521D0B02230B0FFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFF[/code]
Because this was a simple 4-bit language, each opcode+operand took up 2 hex characters. Executing the program was then a matter of reading one byte at a time (in binary mode, very fast) and acting on it.
PS: Did you write your VM in Pascal? I recognise that black-on-white interface :P | 2008-09-16 | 5:24 PM |
aguma | Re:Low-level programmingDoes it work in Vista? I don't wanna reboot everything just to find out that it won't work, lol | 2008-09-16 | 6:14 PM |
Todd | Re:Low-level programmingYeah byte-code is an advantage to P-code, which is used in VB and QB.
I wrote the VM in C++. Turbo Pascal and Turbo C++ both use EasyWin, which is the 16-bit Windows-based console window.
And aguma, it should work on Vista. But then again, what does these days? lol I think it should since it's 16-bit and I tried out some 16-bit apps on Vista and they ran better than the 32-bit apps. | 2008-09-16 | 6:41 PM |
Other
2021 Brandon Cornell