INTERACTIVE VISUAL SIMULATORS F O R B E G I N N I N G P R O G R A M M I N G STUDENTS Stuart C. S h a p i r o Douglas P. W i t m e r C o m p u t e r Science D e p a r t m e n t Indiana University Bloomington, Indiana i. Introduction complexities of a m o d e r n c o m p u t e r w o u l d obscure the points to be made. Therefore, various "hypothetical" computers have been i n v e n t e d that consist of a basic set of operations and registers. Students generally write programs for these h y p o t h e t i c a l computers and find out if their p r o g r a m s are correct either from a human i n s t r u c t o r or by e x e c u t i n g them using a s i m u l a t o r in a b a t c h environment. In either case, it is d i f f i c u l t for them to u n d e r s t a n d the s e q u e n t i a l operation of their programs or the effect of any bugs that might have been in them. In order to provide a b e t t e r way for i n t r o d u c t o r y c o m p u t i n g students to gain this basic u n d e r s t a n d i n g , we have w r i t t e n an i n t e r a c t i v e s i m u l a t o r of one h y p o t h e tical computer. This s i m u l a t o r displays all the storage and active registers of the h y p o t h e t i c a l c o m p u t e r on the screen of a CRT terminal, allows the student to load and p a t c h programs, load data, and execute the program. The s i m u l a t o r traces the p r o g r a m by changing the i n f o r m a t i o n on the screen at a slow enough rate that the student can watch how his p r o g r a m works~ He can then make any m o d i f i c a t i o n s he feels are n e c e s s a r y and see the effect of those changes. This p a p e r discusses two p r o g r a m s that have been w r i t t e n to be aids to introductory p r o g r a m m i n g students. They both embody the b e l i e f that C o m p u t e r A s s i s t e d I n s t r u c t i o n can be a w o r t h w h i l e aid to students when p r o p e r l y used and that one of the best uses is to present visually to the student a process that he has some control over and which he w o u l d not otherwise be able to observe. Section 2 of this paper discusses HYCOMP1, an interactive visual computer simulator. Section 3 discusses IVF, the Interactive Visual FORTRAN interpreter. They were b o t h w r i t t e n in SNOBOL4 ± and run under the KRONOS Time Sharing S y s t e m on a CDC 6600 using an Applied Digital Data Systems, Inc. ADDS Consul 880 terminal, which is an ASCII terminal with a CRT display and an a d d r e s s a b l e cursor. 2. An Interactive V i s u a l C o m p u t e r Simulator 2 2.1 I n t r o d u c t i o n It is often felt desirable to include in an i n t r o d u c t o r y c o m p u t i n g course a small section of m a t e r i a l on machine language, even when the course will mostly use a language such as BASIC, FORTRAN or ALGOL. The purpose of the section on machine language is to give the students an u n d e r s t a n d i n g of what a programmable, digital, s e q u e n t i a l comp u t e r is, what its basic operations are, and how a program, made up of very basic operations can effect a fairly complicated calculation. To a c c o m p l i s h this purpose, it is not n e c e s s a r y to use a real c o m p u t e r as an example. Indeed, the 2.2 The simulator, H Y C O M P I was w r i t t e n to simulate the HYCOMP c o m p u t e ~ of Terry W a l k e r ' s i n t r o d u c t o r y text. ~ W a l k e r ' s H Y C O M P has 1000 words of memory, each h o l d i n g a sign plus five decimal digits. Its i n s t r u c t i o n code ignores the sign, uses the two high o r d e r digits for an o p e r a t i o n code and the r e m a i n i n g t h r e e digits for an address. In order to display all of memory on the CRT, we implem e n t e d only 100 words for HYCOMP1, and use the two low order digits for the address, i g n o r i n g the middle digit. HYCOMP and i Griswold, R.E., Poage, J.F., Polonsky, I.P., The SNOBOL4 P r o g r a m m i n g Language, 2nd edition, Prentice-Hall, E n g l e w o o d Cliffs, N.J., 1971. 2 HYCOMPI 3 Walker, Terry M., I n t r o d u c t i o n to Computer Science: An I n t e r d i s c i p l i n a r y Approach, Allyn and Bacon, Inc., Boston, 1972. H Y C O M P I was p r o g r a m m e d by David A. Grace. ii HYCOMPI have the following active registers: a sign plus 5 digit arithmetic register (KEG); a 5 digit control unit (CU); a program instruction counter (IC) which is three digits in HYCOMP and two digits on HYCOMPI. They also have an overflow (OF) switch, underflow (UF) switch, and an end-of-file switch (EOF). Floating point numbers are stored using the two high order digits as a biased exponent. There are forty nine alphanumeric characters coded two digits per character and stored two characters per word in the low order four digits. The 24 instructions include numeric and alphanumeric I/O, integer and floating point arithmetic and test and branch instructions. HYCOMP does not have index registers, but in the future, they may be added to HYCOMPI using the middle digit of instructions for the index register field. 2.3 HYCOMPI simulator gives the student the same options he would have if he were sitting at a HYCOMP1 console, but in addition, he can observe the execution of his program. There are two commands with which the student can execute his HYCOMPI program RUN and CYCLE. With RUN, the student specifies the starting address of his program, and it is executed until it halts or produces an execution error. If the student uses CYCLE, one machine cycle is executed so that he can study the results of each instruction at his own speed. A machine cycle consists of the following steps: la. The CRT cursor underlines the contents of the word whose address is in the IC. b. That instruction is copied into the CU and displayed there on the screen. 2. IC is incremented by i, the new value being displayed properly on the screen. 3. The instruction in the CU is executed. This may ~ause various things to happen on the screen, for example: a. A test instruction causes the displayed contents of REG to blink for a short time. b. A branch instruction changes the IC. Usin~ H YCOMPI 4 When the student executes HYCOMPI, the screen is cleared and written on as shown in fig. i. He may then load his program (only machine language is accepted by HYCOMPI) and his data and run the program. He may then patch or change his program and continue trying and changing it until he is satisfied or tired. The O0: 07: 14: 21: 28: 35: 42: 49: 01: 08: 15: 22: 29: 36: 02: 09: 16: 23: 30: 37: 43: 50: 44: 56: 63: 70: 77: 57: 64: 71: 78: 51: 58: 65: 72: 79: 84: 85: 86: 91: 92: 99: 93: OF- 98: 03: I0: 17: 04: 24: 31: 25: 32: 05: 12: 19: 26: 33: 38: 45: 39: 46: 40: 47: 34: 41: 48: 52: 59: 66: 73: 53: 60: 54: 61: 55: 62: 67: 74: 81: 88: 68: 69: 75: 76: 80: 87: 94: UF- 11: 18: 06: 13: 20: 27: 82: ~: 83: 90: 97: 95: CU" IC" REG" EOF1 TO SEE A LIST OF AVAILABLE COMMANDS, TYPE HELP TO PROCEED AFTER AN ERROR MESSAGE APPEARS, HIT NEW LINE TO LEAVE A MODE, JUST TYPE NEW COMMAND. ? MEANS READY INPUT APPEARS ON LINE 22, OUTPUT ON LINE 23 ? Fig. 4 A user Stu&rt to the No. 8, Univ., ! i. CRT screen after initialization C. Whenever data is read, either from KEG, a memory word or on the Input llne, the cursor underlines the data. d. Data may be written into REG, a memory word or the Output llne. e. A floating point instruction may set s guide is available as: Shapiro, C. and Grace, David A., "A Guide Use of HYCOMPl",~,Technical Report Computer S c i e n c e D e p t . Indiana Bloomington, In., Dec. 10, 1973. 12 UF or OF. f. An input i n s t r u c t i o n may set EOF. If an e x e c u t i o n e r r o r occurs, an a p p r o p r i ate m e s s a g e is w r i t t e n on the screen. W h e n the student is ready, he can c l e a r this m e s s a g e and e n t e r a d d i t i o n a l commands. The valid commands are: ±nnnnn - The s i g n e d five digit n u m b e r is put into the w o r d whose address is the current value of IC and IC is inc r e m e n t e d by 1. If the sign is omitted, + is assumed. LOAD nn - IC is set to nn, OF, UF, and E O F are i n i t i a l i z e d to 0, and the input line is p o s i t i o n e d at the first "card" of data. If nn is omitted, 00 is assumed. RUN nn - IC is set to nn and the program is run to t e r m i n a t i o n or until an e x e c u t i o n e r r o r occurs. If nn is omitted, the current value of IC is retained. IC XX - XX is a two digit number. IC is set to XX. UF X - X is 0 or 1. UF is set to X. OF X - X is 0 or 1. OF is set to X. E O F X - X is 0 or 1. EOF is set to X. REG n - n is an optionally s i g n e d integer with no more than five digits. REG is set to n. CY - One m a c h i n e cycle is executed. QUIT - The H Y C O M P 1 s i m u l a t o r is terminated. DATA - Allows data to be entered, one "card" at a time. A card w i t h # in column 1 signals the end of data entry and serves as the E O F flag. DATA ADD - Allows data to be added to the end of the e x i s t i n g data "deck". The e x i s t i n g E O F card is a u t o m a t i cally removed. HELP - A list and b r i e f d e s c r i p t i o n of the commands is displayed. 2.4 able task for the b e g i n n i n g student. He must not only m e m o r i z e the key words and s t a t e m e n t syntax a s s o c i a t e d w i t h the language, but must learn to assemble m e a n i n g ful p r o g r a m s from these b u i l d i n g blocks. In this regard, it is e s s e n t i a l for the student to be able to visualize the stepb y - s t e p o p e r a t i o n of his programs. The I n t e r a c t i v e Visual FORTRAN I n t e r p r e t e r (IVF) was d e s i g n e d as an aid to such a visualization. 3.2 Summary The I n t e r a c t i v e preter VIsualFORTRAN Inter- 3.1 I n t r o d u c t i o n L e a r n i n g to p r o g r a m language such as F O R T R A N Description The IVF accepts a FORTRAN p r o g r a m and g r a p h i c a l l y s i m u l a t e s its e x e c u t i o n on an i n t e r a c t i v e t i m e - s h a r i n g t e r m i n a l with a cathode ray tube (CRT) screen. D u r i n g the s i m u l a t i o n the e x e c u t a b l e statements are d i s p l a y e d on the screen. An arrow is caused to point at each statement in the order in w h i c h e x e c u t i o n w o u l d n o r m a l l y take place. Current values of variables are d i s p l a y e d in the unused space to the right of the program, and are u p d a t e d w i t h each move of the arrow. Thus the immediate state of the p r o g r a m is v i s u a l l y apparent at all times in terms of current values of variables and flow of control. Figure 2 shows a short p r o g r a m as it w o u l d a p p e a r d u r i n g s i m u l a t i o n on the IVF. The arrow indicates that e x e c u t i o n of the statement "X = (I ** 2) / 2" is b e i n g simulated. The current values of "I" and "X" are d i s p l a y e d on the right side of the screen. In this example the arrow will next m o v e to the "IF" statement. Simulation of the "IF" s t a t e m e n t succeeds and the arrow moves to the statement w h os e label is "4". Now the value of "I" is inc r e m e n t e d and the new value is d i s p l a y e d on the right. The s i m u l a t i o n p r o c e ed s in this m a n n e r until e x e c u t i o n of the "IF" statement is s i m u l a t e d with the current value of "I" equal to I0. At this time the arrow moves to the "END" statement and the s i m u l a t i o n is terminated. Upon t e r m i n a t i o n of the e x e c u t i o n phase, the IVF displays an a p p r o p r i a t e message and the user is given the option to exit the IVF or attempt a n o t h e r simulation. The FORTRAN p r o g r a m Just s i m u l a t e d is not lost in e i t h e r case. The user may have it s i m u l a t e d again (as d i s c u s s e d later) or dispose of it in some o t h e r fashion. The IVF assumes a naive user, and is therefore self-explanatory. Instructions may be r e c e i v e d at the b e g i n n i n g of each session by r e s p o n d i n g a p p r o p r i a t e l y to the p r o g r a m ' s inquiries. In addition, all p o r t i o n s of the p r o g r a m r e q u i r i n g a user response w i l l recognize errors and prompt as necessary. A l t h o u g h the IVF is p r i m a r i l y an aid to the v i s u a l i z a t i o n o f FORTRAN p r o g r a m e x e c u t i o n , it is also an i n t e r a c t i v e FORTRAN s t a t e m e n t a c c e p t e r with a n u m b e r of e r r o r c h e c k i n g facilities. The IVF will accept FORTRAN lines and c o n t i n u a t i o n lines until a complete s t a t e m e n t has b e e n entered. It then checks for syntax errors, HYCOMP1 has not yet had e x t e n s i v e c l a s s r o o m use. We did have several students from an i n t r o d u c t o r y p r o g r a m m i n g class use it d u r i n g the latter stages of its development. E a r l i e r in the semester, these students had s t u d i e d H Y C O M P and had w r i t t e n several H Y C O M P programs w h i c h w e r e graded by a h u m a n grader. Their reaction to H Y C O M P 1 was e x t r e m e l y favorable and they r e p o r t e d that it w o u l d have b e e n v e r y helpful in their study of m a c h i n e language. One student finally u n d e r s t o o d that there is no inherent d i f f e r e n c e b e t w e e n i n s t r u c tions and data. This k i n d of insight, if not gained i n t e l l e c t u a l l y , could hardly be gained by any means o t h e r than an interactive, visual simulation. 3. Basic in a h i g h e r level can be a formld- 13 .> I-0 I-I+ 1 X " (I * * 2) / 2 IF(I .LT. i0) GO TO 4 END I: 1 X: 0.4999999999 Figure 2 S i m u l a t i o n of a FORTRAN p r o g r a m is a c c o m p l i s h e d by first d i s p l a y i n g its e x e c u t a b l e s t a t e m e n t s on the CRT screen, and then e x e c u t i n g the c o m p i l e d S N O B O L statements d i s c u s s e d above. D u r i n g simulation the IVF displays output from the FORTRAN p r o g r a m on the b o t t o m line of the screen, and a c c e p t s i n p u t on the line above. The p o s i t i o n of the. e x e c u t a b l e FORTRAN s t a t e m e n t s on the CRT screen is n e v e r changed. The CRT's cursor (which u n d e r l i n e s the next p r i n t a b l e position) is m o v e d about as n e c e s s a r y to prevent erasure of v a l u a b l e i n f o r m a t i o n and to facilitate input and output. errors in "DO" loop nesting, and m u l t i p l y d e f i n e d s t a t e m e n t numbers. If the IVF detects an error, the statement is reJ e c t e d and an a p p r o p r i a t e error message is displayed. The user is thus made aware of his m i s t a k e i m m e d i a t e l y and is g i v e n a n o t h e r o p p o r t u n i t y to enter a correct statement. 3.3 A d d i t i o n a l Features A user may o p t i o n a l l y bracket a portion of his FORTRAN p r o g r a m by p l a c i n g a "+" in the c o n t i n u a t i o n field of a n o n c o n t i n u a t i o n line to turn the "STEPPED MODE" on. The "STEPPED MODE" is turned off in an analogous fashion w i t h a "-". W i t h i n the b r a c k e t e d section the user must press the "RETURN" key after the e x e c u t i o n of each F O R T R A N line has b e e n simulated. This permits a s l o w i n g of the s i m u l a t i o n over a p o r t i o n of the user's p r o g r a m for m o r e careful study. The IVF saves a copy of the FORTRAN p r o g r a m simulated, and makes it available on a file r e s e r v e d for that purpose. The u s e r may o p t i o n a l l y submit complete p r o g r a m s to the IVF rather than enter them one line at a time. E r r o r c h e c k i n g is p e r f o r m e d on each s t a t e m e n t as with on-line entry. The complete p r o g r a m may have o r i g i n a t e d from any source, i n c l u d i n g a session w i t h the IVF. 3.4 3.5 Summary V i s u a l i z a t i o n of the e x e c u t i o n of his p r o g r a m s is of utmost importance to the b e g i n n i n g programmer. It is h o p e d that the IVF will serve as an e f f e c t i v e aid to such visualization. It is f u r t h e r h o p e d that the IVF w i l l be found flexible in its features and yet simple enough for use by a b e g i n n i n g programmer. IVF can readily be a d a p t e d to run on a n u m b e r of c o m p u t i n g systems w h i c h support t i m e - s h a r i n g terminals and p r o v i d e interactive execution. The p r i m a r y r e s t r i c t i o n s are the n e e d for a CRT display at the t i m e - s h a r i n g terminal and a s u f f i c i e n t l y large c h a r a c t e r set to support control of the display. The IVF is w r i t t e n in a straightforward m a n n e r w i t h n u m e r o u s C o m m e n t s . An e x p e r i e n c e d p r o g r a m m e r should, t h e r e f o r e be able to e x p a n d the scope of the IVF to suit the needs of his local users. He might even cause the IVF to simulate some language other than FORTRAN. Im~lementatlon The IVF is w r i t t e n in the SNOBOL4 p r o g r a m m i n g language. Each FORTRAN statement a c c e p t e d by the IVF is t r a n s l a t e d into a s e q u e n c e of S N O B O L statements. Steps are taken to adjust o p e r a t o r p r e c e d e n c e and make type c o n v e r s i o n s as necessary. This is b r a c k e t e d by statements w h i c h move the arrow and update disp l a y e d i n f o r m a t i o n d u r i n g the simulation. This b l o c k of S N O B O L statements is conc a t e n a t e d into a string of such blocks for later compilation, u t i l i z i n g the CODE function. 14