TITLE "mp4.a - interpreter for a plotter language" ; plots as commanded by DATA, an external string ; by Douglas Jones -- Oct 15, 2010 S START USE "hawk.macs" USE "monitor.h" EXT UNUSED EXT DATA SUBTITLE "Main Program" START: LIL R2,UNUSED ; set up the stack LIL R1,DSPINI JSRS R1,R1 ; initialize the display ; --- begin aplication code LIL R3,DATA ; -- parameter string pointer LIS R4,0 ; -- parameter x coord LIS R5,0 ; -- parameter y coord JSR R1,INTERPRETER ; interpreter( data, 0, 0 ) ; --- end aplication code LIL R1,EXIT JSRS R1,R1 ; stop! SUBTITLE "The Interpreter" ; activation record for INTERPRETER ;RETAD = 0 STRING = 4 XCOORD = 8 YCOORD = 12 ARSIZE = 16 INTERPRETER: ; expects R3, s string pointer ; expects R4, x coordinate ; expects R5, y coordinate ; returns R3, s updated ; wipes out R4-7 ; call dspch for each char in string STORES R1,R2 INTERLP: ; for (;;) { LOADS R6,R3 ; -- get word holding char EXTB R6,R6,R3 ; char = *s ; -- through rest of loop, ; -- R3=s, R4=x, R5=y, R6=char BZS INTERQT ; if (char == NULL) exit CMPI R6,')' BEQ INTERQT ; if (char == ')') exit CMPI R6,'z' BNE INTERNZ ; if (char == 'z') { LIS R4,0 ; x = 0 LIS R5,0 ; x = 0 BR INTEREI INTERNZ: CMPI R6,'u' BNE INTERNU ; } else if (char == 'u') { ADDSI R5,-1 ; y-- BR INTEREI INTERNU: CMPI R6,'d' BNE INTERND ; } else if (char == 'd') { ADDSI R5,1 ; y++ BR INTEREI INTERND: CMPI R6,'l' BNE INTERNL ; } else if (char == 'l') { ADDSI R4,-1 ; x-- BR INTEREI INTERNL: CMPI R6,'r' BNE INTERNR ; } else if (char == 'r') { ADDSI R4,1 ; x++ BR INTEREI INTERNR: CMPI R6,'(' BNE INTERNP ; } else if (char == '(') { STORE R4,R2,XCOORD STORE R5,R2,YCOORD ; -- save coords through call ADDSI R3,1 ; s++ ADDI R2,R2,ARSIZE JSR R1,INTERPRETER ; s = interpreter( s, x, y ) ADDI R2,R2,-ARSIZE ; -- tricky bit: ADDSI R3,-1 ; s-- -- undo extra increment LOAD R4,R2,XCOORD LOAD R5,R2,YCOORD ; -- restore coords after call BR INTEREI INTERNP: ; } else { STORE R3,R2,STRING STORE R4,R2,XCOORD STORE R5,R2,YCOORD ; -- save everything through calls MOVE R3,R4 ; -- shove params into place MOVE R4,R5 ADDI R2,R2,ARSIZE LIL R1,DSPAT JSRS R1,R1 ; dspat( x,y ) LOAD R4,R2,STRING-ARSIZE LOADS R3,R4 EXTB R3,R3,R4 ; char = *s -- instead of saving it LIL R1,DSPCH JSRS R1,R1 ; dspch( char ) ADDI R2,R2,-ARSIZE LOAD R3,R2,STRING LOAD R4,R2,XCOORD LOAD R5,R2,YCOORD ; -- restore everything after calls INTEREI: ; } ADDSI R3,1 ; s++; BR INTERLP ; INTERQT: ; } ADDSI R3,1 ; s++; LOADS R1,R2 JUMPS R1 ; return s END