In the thread "Mini--x86" Rod P. posted a ddj ref to a mini interpreter, and I had alook at it... the article is well worth a read and speaks well of assembler.
It seems to me to be an example of threaded code, Forth like in a way, anyway I did a nasm syntax conversion and afew kludges to get the animation to work, here's the nasm source for a .com file for a dos box, or XP's cmd.exe...
Enjoy,
Steve
;;--------------------------------------------------------60 ;; File: MINITERP.NSM ;; modified for NASM syntax. s_dubrov...@yahoo.com 14OCT2009 ;; -f bin -l MINITERP.LST -o MINITERP.COM MINITERP.NSM ;; Note: has some int21h calls, use a dos box or cmd.exe ;; Ref: http://www.ddj.com/184408206?pgno=3 ; ; This program demonstrates the use of a mini-interpreter to produce ; code that is compact, flexible and easy to modify. The mini- ; program draws and labels a maze and animates an arrow through ; the maze. ; ; Note: This program must be run in 80-column text mode. ; ; By Dan Illowsky & Michael Abrash 2/18/89 ; Public Domain ; ;Stak segment para stack 'stack' ;allocate stack space ; db 200h dup (?) ;Stak ends ; ;_TEXT segment para public 'code' ; assume cs:_TEXT, ds:_TEXT ; ; ; Overall animation delay. Selected for an AT: set higher to slow ; animation more for faster computers, lower to slow animation less ; for slower computers. ; DELAY_COUNT equ 0FFFFh ; ; Equates for mini-language commands, used in the data ; sequences that define mini-programs. The values of these ; equates are used by Interp as indexes into the jump table ; Function_Table in order to call the corresponding subroutines. ; ; Lines starting with ">>" describe the parameters that must ; follow the various commands. ; Done# equ 0 ; Ends program or subprogram. ; >>No parms. SubProg# equ 1 ; Executes a subprogram. ; >>Parm is offset of subprogram. SetXY# equ 2 ; Sets the cursor location (the location at ; which to output the next character). ; >>Parms are X then Y coordinates (both ; bytes). SetXYInc# equ 3 ; Sets the distance to move after displaying ; each character. ; >>Parms are X then Y amount to move after ; displaying character (both bytes). SetX# equ 4 ; Sets the X part of the cursor location. ; >>Parm is the X coordinate (byte). SetY# equ 5 ; Sets the Y part of the cursor location. ; >>Parm is the Y coordinate (byte). SetXInc# equ 6 ; Sets the X part of the amount to move after ; displaying each character. ; >>Parm is the X amount to move after ; character is displayed (byte). SetYInc# equ 7 ; Sets the Y part of the amount to move after ; displaying each character. ; >>Parm is the Y amount to move after ; character is displayed (byte). SetAtt# equ 8 ; Sets the screen attribute of characters to ; be displayed. ; >>Parm is attribute (byte). TextUp# equ 9 ; Displays a string on the screen. ; >>Parm is an ASCII string of bytes, ; which must be terminated by an EndO# byte. RepChar# equ 10 ; Displays a single character on the screen ; a number of times. ; >>Parms are char to be displayed followed ; by byte count of times to output byte. Cls# equ 11 ; Clears screen and makes text cursor ; invisible. ; >>No parms. SetMStart# equ 12 ; Sets location of maze start. ; >>Parms are X then Y coords (both bytes). Mup# equ 13 ; Draws maze wall upwards. ; >>Parm is byte length to draw in characters. Mrt# equ 14 ; Draws maze wall right. ; >>Parm is byte length to draw in characters. Mdn# equ 15 ; Draws maze wall downwards. ; >>Parm is byte length to draw in characters. Mlt# equ 16 ; Draws maze wall left. ; >>Parm is byte length to draw in characters. SetAStart# equ 17 ; Sets arrow starting location. ; >>Parms are X then Y coordinates ; (both bytes). Aup# equ 18 ; Animates arrow going up. ; >>No parms. Art# equ 19 ; Animates arrow going right. ; >>No parms. Adn# equ 20 ; Animates arrow going down. ; >>No parms. Alt# equ 21 ; Animates arrow going left. ; >>No parms. DoRep# equ 22 ; Repeats the command that follows ; a specified number of times. ; >>Parm is repetition count (one byte). ; EndO# equ 0 ; used to indicate the end of a ; string of text in a TextUp# ; command.
[SECTION .cseg vstart=0100h] ;; <sd> .com section ;; ;; --== M a i n ==-- ;; ; ; Program start point. ; Start: ;; proc far push cs ; code and data segments are the pop ds ; same for this program mov sp,0FFFEh ;; seed SP to top of segment <sd> mov si, DemoScreen$ ; point to mini-program call Interp ; execute it
mov ah,1 ; wait for a key before clearing the int 21h ; the screen and ending mov ah,15 ; get the current screen mode int 10h ; so it can be set to force sub ah,ah ; the screen to clear and the int 10h ; cursor to reset mov ah,4ch int 21h ; end the program ;; Start endp ; ; Mini-interpreter main loop and dispatcher. Gets the next ; command and calls the associated function. ; Interp: ;; proc near cld GetNextCommand: lodsb ; get the next command mov bl,al xor bh,bh ; convert to a word in BX shl bx,1 ; *2 for word lookup call [bx+Function_Table] ; call the corresponding ; function jmp short GetNextCommand ; do the next command ; ; The remainder of the listing consists of functions that ; implement the commands supported by the mini-interpreter. ; ; Ends execution of mini-program and returns to code that ; called Interp. ; _@Done_: pop ax ; don't return to Interp ret ; done interpreting mini-program or subprogram ; so return to code that called Interp ; ; Executes a subprogram. ; _@SubProg_: lodsw ; get the address of the subprogram push si ; save pointer to where to ; resume the present program mov si,ax ; address of subprogram call Interp ; call interpreter recursively ; to execute the subprogram pop si ; restore pointer and resume ret ; the program ; ; Sets the screen coordinates at which text will be drawn. ; _@SetXY_: lodsw mov word [Cursor_X_Coordinate],ax ret ; ; Sets the amount by which the cursor will move after each ; character is output to the screen. ; _@SetXYInc_: lodsw mov word [Cursor_X_Increment],ax ret ; ; Sets individual X coordinate, Y coordinate, X movement after ; character is output to the screen, Y movement, or character ; attribute depending on function number. ; _@Set_: shr bx,1 ; calculate the command number lodsb ; get the new value mov [bx+Text_Out_Data-SetX#],al ; store in location ; corresponding to ; the command number Return: ret ; ; Displays a string of text on the screen. ; _@TextUp_: GetNextCharacter: lodsb ; get next text character or al,al ; see if end of string je Return ; if so, next command call OutputCharacter ; else output character jmp short GetNextCharacter ; next character ; ; Displays a single character on the screen multiple times. ; _@RepChar_: lodsw ; get the character in AL ; and the count in AH RepCharLoop: push ax ; save the character and count call OutputCharacter ; output it once pop ax ; restore count and character dec ah ; decrement count jne RepCharLoop ; jump if count not now 0 ret ; ; Clears the screen and turns off the cursor. ; _@Cls_: mov ax,600h ; BIOS clear screen parameters mov bh,[Character_Attribute] xor cx,cx mov dx,184fh int 10h ; clear the screen mov ah,01 ; turn off cursor mov cx,2000h ; by setting bit 5 of the int 10h ; cursor start parameter ret ; ; Sets the start coordinates for maze-drawing. ; _@SetMStart_: lodsw ; get both X and Y coordinates and store mov word [Cursor_X_Coordinate], ax mov [Last_Maze_Direction], byte 0ffh ; indicate no ret ; last direction
> In the thread "Mini--x86" Rod P. posted a ddj ref to a mini > interpreter, and I had alook at it...
> the article is well worth a read and speaks well of assembler.
> It seems to me to be an example of threaded code, Forth like in a way,
Sort of an "unrolled" STC FORTH... with a single stack comprised of execution tokens and data.
> anyway I did a nasm syntax conversion and afew kludges to get the > animation to work, here's the nasm source for a .com file for a dos > box, or XP's cmd.exe...
It works.
Steve,
Did you make a connection with Small C? The quantity of instruction sequences emitted by Small C is small. It's possible that one could implement each sequence as a routine in such a mini-interpreter. Then, Small C could emit byte-code or actually word-code. It could be useful in portability...
> > In the thread "Mini--x86" Rod P. posted a ddj ref to a mini > > interpreter, and I had alook at it...
> > the article is well worth a read and speaks well of assembler.
> > It seems to me to be an example of threaded code, Forth like in a way,
> Sort of an "unrolled" STC FORTH... with a single stack comprised of > execution tokens and data.
> > anyway I did a nasm syntax conversion and afew kludges to get the > > animation to work, here's the nasm source for a .com file for a dos > > box, or XP's cmd.exe...
> It works.
> Steve,
> Did you make a connection with Small C? =A0The quantity of instruction > sequences emitted by Small C is small. =A0It's possible that one could > implement each sequence as a routine in such a mini-interpreter. =A0Then, > Small C could emit byte-code or actually word-code. =A0It could be useful= in > portability...
> Rod Pemberton
Hi Rod,
Hey, thanks again for posting that link.
I sort of made a connection to small-c, I guess you mean emitting an intermediate code?
The syntactic actions in small-c are embedded in the parser as an expression is evaluated, and are generated as an expression is decoded. The syntactic actions are generic stack machine operations; zpush(), zpop(), push_primary(), pop_secondary(), ..and the like. This form of syntactic action is highly portable because the elements like push, pop, etc. are of an Abstract Data Type :: Stack. This is one thing that piqued my interest in small-c, along with single-pass, and self-compiling. The parser tho has.. errata.., but I'd rather not mess with it and still call it small-c, y'know? ;-)
But, yes it did cross my mind to institute, at least, the parser in such a mini-interp way. Let's call that future project small-c2. So, for small-c2, you have my vote! It seems like it would aid in code generation optimizing.
Coincindentally, I've been revisiting the original 'Dragon Book' (from time to time) to see how small-c would handle the example C code from the book, and what kludges allow small-c to be used on it. Also to gather or refresh ideas for just such a small-c2, or for another language I'd been thinking about.
The mini-interp article lit up a few lights.. " The basic operation of a mini-interpreter is simple, then: A data sequence, or "miniprogram," provides function numbers, which are used to vector through a jump table to functions. The function numbers are basically commands in the "minilanguage" defined by the functions in the jump table. The functions then acquire their own parameters from the data sequence as needed.
Another way to view a mini-interpreter is as a control program that allows you to call various functions in any order and with any parameters. The jump table defines the functions that can be called -- in effect defining a minilanguage -- and the data sequence defines the calling order and the parameters, thereby serving as a miniprogram. The same result could, of course, be accomplished simply by writing code that calls the desired routine with the desired parameters. The great advantage of using a mini-interpreter over writing equivalent code is that a mini-interpreter makes for more compact code that's also easier to write and maintain.
Now, consider this: Pointers to the jump table and/or the data sequence can be parameters passed to the interpreter, so the operation of the interpreter can be changed instantly. By passing in a pointer to a different data sequence, the functions in the jump table can be combined in different orders and with different parameters; in other words, a different miniprogram can be run. By passing in a pointer to a different jump table, the very minilanguage that the mini- interpreter supports can be altered.
In other words, not only the miniprogram that the mini-interpreter is running but also the minilanguage that it supports can easily be changed, even in mid-program -- the ultimate in flexibility. "
First, this smacks of a simple system for multitasking.
Second, the data is in the form of: operator.operand_list,++ . -which reminds me of Lisp notation of (cons X Y), ie, operator.operand_list.
Third, " In a miniprogram, however, a mere byte of miniprogram code would suffice, with the text built right into the miniprogram:
db 15,'Hello',O " Reminds me of old code I've seen quite a few times for inline strings:
call print_routine db 'Hello There',0 cont:
where the print_routine expects the address of the string on the stack, (which it is as a result of the call storing 'next'), i.e. the db string. Such a print_routine counts the characters in the string as it prints and modifies the stack ret accordingly to: cont:. (pop reg ! add reg, chr_count ! push reg ! ret)
Fourth, you know Function_Table could just as well be the function table of a generic driver replaceable with specific driver function table when specific hardware is recognized as long as table index functionally was mapped correctly; 0->open_device, 1->close_device, 3-
> I sort of made a connection to small-c, I guess you mean emitting an > intermediate code?
Yes. I was thinking bytecode, tokens, or similar...
> The syntactic actions in small-c are embedded in the parser as an > expression is evaluated, and are generated as an expression is > decoded. The syntactic actions are generic stack machine operations; > zpush(), zpop(), push_primary(), pop_secondary(), ..and the like. > This form of syntactic action is highly portable because the elements > like push, pop, etc. are of an Abstract Data Type :: Stack.
I'd think that'd be suitable to tokens.
> But, yes it did cross my mind to institute, at least, the parser in > such a mini-interp way. Let's call that future project small-c2. So, > for small-c2, you have my vote! It seems like it would aid in code > generation optimizing.
Parser? ...
> Coincindentally, I've been revisiting the original 'Dragon Book' (from > time to time) to see how small-c would handle the example C code from > the book, and what kludges allow small-c to be used on it.
Great. I'd be interested in what does/doesn't work. I'd also be interested in any test program that tests all those early C features.
> The mini-interp article lit up a few lights..
Yes. It might be more useful if you add a stack. You could then separate the parameters from the function calls. That might allow you to implement variables instead of hardcoded constants for parameters. You might have to rework calls to into computed jumps or use xchg to separate the control flow from data.
> The mini-interp article lit up a few lights..
It's a much simpler method of implementing features similar to FORTH, without FORTH. At it's core, FORTH has an interpreter - well, two actually: inner and outer, some "primitives" - which are routines in assemlby (or C), compiled "words" - subroutines - which are lists of addresses "called" by the "inner" interpreter until the assembly in the "primitives" gets executed, and two stacks: one for data and one for return stack - i.e., control flow data. Of course, there are a number of complexities, higher level stuff I've skipped - like dictionaries, variables and constants etc., and the "outer" interpreter which is a text parser - and subtleties to actually implementing FORTH with those techniques. I'm still working on my one in C...
> > I sort of made a connection to small-c, I guess you mean emitting an > > intermediate code?
> Yes. =A0I was thinking bytecode, tokens, or similar...
Ok.
> > The syntactic actions in small-c are embedded in the parser as an > > expression is evaluated, and are generated as an expression is > > decoded. =A0The syntactic actions are generic stack machine operations; > > zpush(), zpop(), push_primary(), pop_secondary(), ..and the like. > > This form of syntactic action is highly portable because the elements > > like push, pop, etc. are of an Abstract Data Type :: Stack.
> I'd think that'd be suitable to tokens.
I think the later versions of Hendrix's small-c did that.
> > But, yes it did cross my mind to institute, at least, the parser in > > such a mini-interp way. =A0Let's call that future project small-c2. =A0= So, > > for small-c2, you have my vote! =A0It seems like it would aid in code > > generation optimizing.
> Parser? ...
Parser, Lexical Analyzer, they are both wrapped up together in the recursive descent parser. Perhaps the threaded code isn't such a good candidate for this, the article went on to comment that coditional flow control wasn't a strong point of the method.
> > Coincindentally, I've been revisiting the original 'Dragon Book' (from > > time to time) to see how small-c would handle the example C code from > > the book, and what kludges allow small-c to be used on it.
> Great. =A0I'd be interested in what does/doesn't work. =A0I'd also be int= erested > in any test program that tests all those early C features.
Ok. It's been ad-hoc discovery sofar. Things like '|' instead of '||' and '&' instead of '&&' in the small-c syntax. Test code for K&R syntax is hard to find.
> > The mini-interp article lit up a few lights..
> Yes. =A0It might be more useful if you add a stack. =A0You could then sep= arate > the parameters from the function calls. =A0That might allow you to implem= ent > variables instead of hardcoded constants for parameters. =A0You might hav= e to > rework calls to into computed jumps or use xchg to separate the control f= low > from data.
> It's a much simpler method of implementing features similar to FORTH, > without FORTH. =A0At it's core, FORTH has an interpreter - well, two actu= ally: > inner and outer, some "primitives" - which are routines in assemlby (or C= ), > compiled "words" - subroutines - which are lists of addresses "called" by > the "inner" interpreter until the assembly in the "primitives" gets > executed, and two stacks: one for data and one for return stack - i.e., > control flow data. =A0Of course, there are a number of complexities, high= er > level stuff I've skipped - like dictionaries, variables and constants etc= ., > and the "outer" interpreter which is a text parser - and subtleties to > actually implementing FORTH with those techniques. =A0I'm still working o= n my > one in C...
Yeah, an interpretive programming/control shell is a goal of mine. I don't think C will be the syntax for it, I'm not sure what it will be, I'm still mulling that over.