Assembler/Interprete• ListingI (*Aasembler and interpreter of Paecel code*)2 ("K. Jenson, N. Wirth, Ch. Jacobi, ETH May 76")34 program pcode(input s output,prd,prO156 (" Note for the implementation.7I This interpreter ie written for the caee where all the fundamental types9 take one storage unit.10 In an actual implementation, the handling of the ep pointer has to take11 into account the fact that the types may have lengths different from one'12 in push and pop operations the sp has to be increased and decreaeud not13 by 1, but by a number depending on the typo concerned;14 However, where the number of unite of storage has been computed by the15 compiler, the value must not be corrected, since the lengths of the types16 involved have already been taken into account.17A)1819202122 label 1;23 co<strong>net</strong> codemax • 8650;24 pereax • 17500;25 maxstk . 13650; (* site of variable store A)26 overt • 13655; (A size of integer constant table • 5 A)27 ovorr • 13660; (A size of real constant table • 5 A) •28 overs - 13730; (A site of set constant table • 70 A)29 overb - 13820;30 OVeral • 10000;31 maxatr, • 18001;32 largoint . 26144;33 begincod• - 3;34 inputadr • 5;35 oitputadr • 6;36 prdadr • 7137 prradr • 8;38 duminst . 6213940 typo bit4 .-0..15141 'bit6 • 0..127;42 bit20 . - 26143..26143143 datatypo - (undo(' int,reol,boolopettoidr,mark,car);44 address ■ -1:.maxatr;45 beta46.. packed orrey11..231 of char; (*error meeaoge 0 )47 var code s orray(0..codemax) of (A the program '948 packed record opl tbit6;49DI tbit4,50 ql tbit20151' opt !bite;52 p2 tbit4;3354 end'q2 1bit2055 pc 1 0..pcmAx; (Aprogram address register*)36 op 1, bit6, p t bit4, q 1 bit20; (Ainatruers,,,, ,......, • .6782 Pascal Implementation: Compiler and Assembler/Interpreter953 end;r:54955 46 ("1110)1 begin sp 1. sp-1;stere(•p).vs 1. •tore(sp).vs A •tore(op+11.vs956957 end;958959 47 ("uni")1 begin ep :. sp-1;960etore(spl.ve 1• store(sp).vs + store(sp+11.vs961 end;962963 48 ("inn")t begin964 sp t. op - 1; i t- store(spkvi;storelepl.vb 1- i in store(sp+11.vs;965966 end;9u7968 49 ("mod")1 begin ep :. op-1,storelepl.vi :. etore(spl•vi mod etore(sp+1).vi969970 und;971972 50 (*odd"): storelep).vb ► • odd(stors(sp).vi);97)974 51 ("mpi")t begin sp i. sp-1;etoreispl.vi :. •tore(sp).vi * storelsp+11.vi915976 end;977978 52 ("mpr")1 begin •p I» sp-1;979 store(•pl.vr 1... store(sp).vr * otore(ep+1).vr900 end;981902 53 ("dvi*): begin ep :- sp-I,storelsp).vi t• storelspkvi div store(sp+1).vi983984 end;98598o 54 ( 0dvr$91 begin sp :.. sp-1;atore(sp).vr c. store(sp).vr / otore(sp+11.vr987988 end;989993 55 (*mov*)1 begin it :. store(sp-1).va;991 ' 12 i• etorelspl.va; sp :. sp-2;for I :. 0 to q-1 do storelil+il :- store(12+i)992993 ( 5 q is a number of storage units A)994 end;995996 56 ("lca 0 )t begin ep I. •p+1;997 •torelsp).va t. q;998 end;9991000 100,101,102,103,104,1001 57 (*dec")1 store(ep).vi t. store(spl.vi - q;10021003 58 (*stp")t interpreting z ■ false;10041005 59 (*ord 0 )s ("only used to change the tagfield")1006begin1007 end;10001009 60 ("chr")s begin1010end;101 ►1012 61 (Auje 0 )t erred(' case error ');1013 and1014 end; ("while interpreting")10151016 I i,,,,
68Pascal Implementation: Compiler and Asset&/InterpreterAssembler/Interpreter Listing 695/5859store ; array 10..overml ofrecord case datatypa of60 int .(v1 :integer), 12461 reel .(vr :real), 12562 bool :(vb .boolean), 12663 sett :(vs :set of 0..47)1 12764 car 1(c :char); 12865 adr :(va :address); 12966 ("address in store") 13061 mark t(vm :integer) 13160 end; 13269 mp,sp,np,ep : address. (A address registers A) 13370 ("mp points to beginning of a data segment 13471 sp points to top of the stack 13572 ep points to the maximum extent of the stack 136)) np points to top of the dynamically allocated area") 13774 13075 interpreting. boolean; 13976 prd,prr ; text;("prd for read only prr for write only A) 14077 14178 inatr : array(bit61 of alfa; (A mnemonic instruction codes A) 14279 cop I array(bit6) of integer; 14380 sptable I array(0..201 of alfa; ("standard functions and procedures") 14481 14582 ("locally used for interpreting one instruction") 14683 ad,adl : address; 14784 b 1 boolean; 14885 i,j,i1,12 ; integer; 14986 c 1 char) 15087 15188 (" A)15289 15390 procedure load; 15491 co<strong>net</strong> maxlabel 1050; 15592 type labelst (entered,deftned); ("label situation") 15693 labelrg « 0..moxlebel; ("label range") 157-94 lebelrec record15095 val.: address; 15996 at: labelst 16097 end; 16198 var icp,rcp,scp,bcp,mcp : addreae; ("pointers to next free position") 16299 word . array(1..101 of char; 1 integer; ch : char; 1631UU labeltab: arrayllabolrg) of labelrec; 164101 labelvalue: address, 1651U2 166103 procedure lnit; 167104 var i: integer; 168105 begin instr( 011 ■ 'Iod instr( 1 1-'ldo 169106 instr( inatr( 3 1..'sro 170107 instr( instr( 5 t.i'lao171100 instr( 6)1.e i sto instr( 7 t.'ldc1172;109 instr( 8)1.'... instr( 9 :m i ind173110 instr(1011m'inc instr(11 174instr(12)t.'cup instr(13 ...ant 175112 instr(14)1-'rat i<strong>net</strong>r115 :-'cep 176113 instr(16)1- l ixe i<strong>net</strong>r(17 177114 instr(1811..'nee instr(19 1.. 1 gaq 178115 instr(20): ■ 'grt instr121 1-'1eq 179116 instr(2211•'les instr123 t•'u.i1) 180Ill instr(24)1-'f)p instr(25 i. e xjp 181•118 i<strong>net</strong>r(26)swichk instr(27 102119 instr(2811 ■ 'sdi instrI29 ps'adr 183120 instr(30): ■ 'sbl i<strong>net</strong>rI31 s-'sbr 184121122123instr(321:-'sgsinstr(33);"'fltinstr(341:..'floinstr(35)s.'trcinstr(36):-'n8ii<strong>net</strong>r(37)10'ngrinstr(3011- • eqi i<strong>net</strong>r(39):-.'sqri<strong>net</strong>r14011A'abiinstr(41):.'abri<strong>net</strong>r(42):-'notinstr(431t.'andinstr14411.. s ior instr(45):-'difinstr(46)P.'inti<strong>net</strong>r(47)r-'uniinstr(48): ■ i inn instr14911 ■ 'modinstr(50)1-'oddinstr(51);.'mplinstr(5211..'mprinstr(53):s'dviinstr(54)1m'dvrinatr1551: ■ 'movInstr(56):-.'lca ; instr(57):...decinstr(501:-'etpinstr(59):..'ordinstr(60):-'chrinstr(61):-'ujcsptable( 0eptable( 2sptable( 4sptable( 6eptable( 8eptable(10aptable(12sptable114eptablo116eptable(18eptable120:-'retts i wrieptable( 1eptable( 3sptable( 5eptable( 7eptable( 9eptable(111- i rdr eptable(13aptablo(15'; aptable(17eptable(19cop( 0) 105; cop( 11 ;- 65;cop( 2) 70; cop( 3) 1- 75;cop( 61 ;is 60; cop( 9) 1.. 85;cop(10) 1- 90; cop(26)cop(571 100;95;pc begincodelicp ta umiak + 1;rcp 1. overt + 11acp overr + 1;bcp overa + 2;mep overb + 1;for 11. 1 to 10 do word(i):-. 'for 0 to maxlabel dowith labeltabli1 do begin vall-r-1;roset(prd);end,("init"):A'put:-'rin:A'wln;.'wrr; ■ 'rdi: - 'rdc:.'logprocedure errorl(stringt beta), ("error in loading")begin writoln;write(atring),haltend; ("errorl")entered end;proceduri update(xt labelrg); (*when • label definition lx is found")var curr,succs -1..pcmax; ("rasp. current element and successor elementof • list of future references")endlisti boolean;beginif labeltab(x).st.definod then errorl.(' duplicated labelelse beginif labeltab(x).val0-1 then ("forword'roference(s)")begin cum.. labeltablx).vall endlists. false;while not endlist dowith cods(curr cli,v 2) dobegin