Wombat Programming assignments W1-0 & W1-1
In-Lab Exercise, Friday, February 15
Due: Monday, Feb. 18, at class time
The purpose of this lab is to introduce you to CPU Sim. Therefore, we will just be redoing exercises W1-0 and W1-1 that you did in the Pre-lab exercises, except this time we will use assembly language instead of machine language and we will actually run the programs you wrote.
Starting up CPU Sim
To start up CPU Sim, look in the Macintosh Dock for a file called "Start CPU Sim" (You can also find it in the CPUSim3.5.1 folder inside the Applications folder.) It is an AppleScript document that starts up CPU Sim. Double-click on it and then wait several seconds and a large window should appear. This window is the main "desktop" window for CPU Sim. Except for some dialog boxes, all windows used by CPU Sim are windows internal to the desktop window. Such internal windows include text windows for writing and editing assembly programs and display windows for viewing and editing the contents of registers and memory. The bottom part of the desktop is a console for doing input and output. When the program needs input from you, it will prompt you in the console. When the program has data to output, it outputs it to the console window. You can optionally change it to do input and output via dialog boxes or via text files instead of the console if you wish, but you don't need to worry about those options now.
The title of the CPU Sim desktop window is "Wombat1" since, when you open CPU Sim using the "Start CPU Sim" AppleScript file, the Wombat1 CPU specifications are automatically loaded into CPU Sim. You should also see three internal windows in the CPU Sim desktop. They are described below.
Loading a CPU into CPU Sim
You can load a different CPU specification anytime you want by choosing "Open machine..." from the File menu and then choosing a file that stores the specifications of that CPU. Such files usually have names ending in ".cpu". You can also create a whole new CPU with no registers and no memory by choosing "New machine" from the File menu. (Note: CPU specifications are stored in text files containing XML code. You can edit this file with any text editor but beware that if you change it improperly, CPU Sim will not be able to load the specified CPU.)
The Wombat 1 Register and RAM windows
The smallest internal window, entitled "Registers", allows you to see all the registers in the Wombat1's CPU, including their widths (that is, the number of bits in the register) and current values. To see the registers' values displayed in base 2 (binary), base 16 (hexadecimal), or ASCII, select the appropriate item from the popup menu at the top of this window. The decimal is converted to binary or hexadecimal or ASCII from a two's complement representation. The values of the registers can be edited in any of these bases by double-clicking in the appropriate cell of the table, typing in a new value, and then pressing the Enter or Return key. (Try doing some editing of these cells now to see what happens. If you type in an illegal value, the cell is surrounded with a red border.)
The larger window, entitled "RAM Main", allows you to see the contents of the Wombat1's RAM (or main memory) which has been named "Main". As with the registers, you can display the values and addresses in base 2, 10, or 16. You can also view the contents of the RAM in ASCII. Furthermore, you can view the contents in units of 1-8 bytes instead of just 1 byte. Since the Wombat1 uses 16-bit instructions, it is helpful to view the RAM in 16-bit (or 2-byte) units, which is the reason a cell size of 2 is selected in the popup menu at the top of the RAM window. The values in the RAM window are all editable except for those in the address column. Leave the register window and RAM window open for the rest of this lab.
Note: The columns in the tables in any of these windows can be reordered by dragging the column headers left or right to a new position. The column widths can also be adjusted by dragging the line dividing the columns. Furthermore, in all windows except the RAM windows, the rows can be sorted by column by clicking in any column header.
Creating or Opening Assembly Programs
Now that CPU Sim is simulating the Wombat1, we can start writing and executing programs for it. We will use assembly language for our programs instead of the 0's and 1's of machine language that you were forced to use in the pre-lab, which should make life much easier for you.
The assembly language version of the program that was given to you in the pre-lab exercise W1-0 has already been loaded into CPU Sim. It is displayed in the third internal window in the CPU Sim desktop. (To open additional assembly language programs, choose "Open text…" from the File menu.) This window is a basic text editor window, in which you can type or edit text, including the usual cut/copy/paste/undo/find/replace features.
As I said above, the program displayed in the text window corresponds to the machine language program given to you in pre-lab exercise W1-0, except that the binary numbers are replaced with words (mnemonics). Here is a description of the various words and values you see in the window.
You should appreciate how much easier it is to write assembly language programs than machine language programs, as you had to do in the pre-lab. One of the nicest features of CPU Sim is that it automatically converts ("assembles") your assembly language program into the corresponding machine language program, so you never need to write machine language programs again.
Assembling Programs and Loading Them into Memory
Before you can run the program "W1-0.a", you must have CPU Sim assemble it into machine language instructions that the Wombat1 can understand and then you must load those instructions into the Wombat1's memory. To do so, choose "Assemble & Load" from the Execute menu. (Note: If this menu item is disabled, then the W1-0.a window is not the currently selected--that is, highlighted--window. In that case, click anywhere on the W1-0.a window first to select it and then choose "Assemble & Load".) At this point, if there are any syntax errors in your program, the assembler will flash an error message and highlight the part of your assembly program that it can't understand. The W1-0.a program shouldn't have any errors in it and so you should see numbers and comments appear in the first few rows of the table in the RAM window. If you display the data in the RAM window in binary, the "data" column should look just like the machine language program you saw in the prelab W1-0 except that spaces have been added after every 4 bits to make the values more readable.
Running Assembly Programs
Now the program in the main memory is ready to be run. Make sure all the registers have been cleared (set to 0). If some of them are not 0, then either edit the values directly to make them 0 or choose "Clear all registers & arrays" from the Execute menu. Then choose "Run" from the Execute menu. The program will begin execution with the instruction whose address is stored in the program counter pc. The machine runs by repeatedly executing machine cycles consisting of (a) the fetch sequence, which loads the instruction whose address is in the pc into the ir, increments the pc, and then decodes the instruction, followed by (b) the execute sequence, which executes the machine instruction that was just decoded.
In our program, the first instruction is a READ instruction for getting input. Therefore when we begin execution, the program should present you with a prompt in the console at the bottom of the window asking for input. Type in a positive integer and press the Return or Enter key. Do this several times and then type in a negative number. The program will print a value in the console window that is the sum of all the positive numbers you typed in. This will be followed by a dialog box indicating that the program has ceased execution for the reason that a condition bit was set to 1. Click the "OK" button to close this message window. When execution is complete, you can see the final state of the registers and RAM in the registers and RAM windows.
You've done it! You've now successfully (a) started CPU Sim, (b) loaded into CPU Sim a machine to be simulated, (c) opened an assembly language program, (d) assembled the program and loaded the resulting machine language program into the simulated computer's main memory, and (e) ran the program. This will be your usual routine from now on when using CPU Sim.
If you wish to run the same program again with different input, just choose "Clear, assemble, load, and run" from the Execute menu.
Using CPU Sim to help you debug your code
CPU Sim has extensive features to help you debug your programs. You can step through the execution of your program one machine instruction or one microinstruction at a time so that you can observe, (and edit, if desired) the values in the main memory and in the registers after each instruction. You can even back up as many machine instructions or microinstructions as you wish to restore the machine to a previous state.
Let's try it. First clear everything (by choosing "Clear everything" from the Execute menu), select the text window, and then choose "Assemble & load" from the Execute menu. Then select "Debug mode" from the Execute menu. You will see a toolbar appear at the top of the display. When you are in debug mode, you cannot edit the machine's innards and so the Modify menu is disabled. However, you can still edit the contents of registers or RAM.
Let us now step through the execution of the W1-0.a program. Notice that the right end of the toolbar says "Fetch sequence" followed by a scroll box. That indicates that the fetch sequence is what is currently being executed (the scroll box displays the microinstructions that make up the fetch sequence). If you click "Step by Instr" from the toolbar, the fetch sequence and the instruction it fetches will both be executed. Do so now. The console should prompt you for input. After typing in the input and pressing return, you will see that the value that you typed is now in the acc register. You can also see on the right end of the toolbar that the next instruction is again the fetch sequence. In summary, each click of "Step by Instr" causes a full machine cycle to be executed. As you are stepping through the execution, you can edit the contents of any of the registers or RAM.
Step forward a few more instructions, watching what happens to the values in the registers and in main memory. Also, notice that the next instruction to be executed is always highlighted in the main memory. Now try backing up a few instructions. To do so, choose "Backup one Instr" from the toolbar a few times.
To finish, you can continue stepping forward or you can choose "Go" from the toolbar, which will execute the program without stopping. To start execution all over again from the beginning, choose "Reset all" from the toobar.
Infinite Loops
If your program gets into an infinite loop, the way to break out of it is to choose "Stop" from the Execute menu (or press the Ctrl and period "." keys), which will cause CPU Sim to stop running the current program. If you get into an infinite loop when in Debug mode, CPU Sim may crash due to out-of-memory errors. Therefore, try testing your programs first when not in Debug mode.
Viewing all the Details of the Machine
Sometimes it is helpful to be able to see all the details of the current machine at once in some nice, human-readable form. To get such a form, choose "Save machine in HTML" from the File menu, which will bring up a dialog for naming an HTML file into which the machine details will be saved. If you want to save it, you should then type in a name for the file and click the "Save" button. However, you don't need to do so for the Wombat1 since there is already such a file called "Wombat1.html" in the Sample Assignments folder in the CPUSim3.5.0 folder in the Applications folder. To view such an HTML file, switch to any web browser and open the file in the browser.
Getting Help
CPU Sim contains an extensive set of features to aid you in editing, testing, and debugging your programs and machines. The best resource for learning these features is the user's manual. Most of the information in the user's manual is available online as part of the CPU Sim package. To access the online help, just choose "General CPU Sim Help" from CPU Sim's Help menu. Most dialog boxes also have "Help" buttons, which when clicked will bring up the online help for that dialog box. Feel free to ask me if you have any questions, especially if you wish to learn about short cuts for performing various tasks.
CPU Sim bugs and features
Although the author has removed from CPU Sim all the bugs that he is aware of, it almost surely contains other bugs that you will encounter. When that happens, please let me know and I will try to get the author fix them as quickly as he can. Ideally, give me directions on how to reproduce the bugs; otherwise it might be very hard for the author to find them and fix them.
I would also like to be made aware of any missing features of CPU Sim that you feel should be added to the package. If the feature is relatively simple, I can often get the author to add it within a few days.
Exercises to do
Now that you have loaded and run the assembly language program corresponding to the machine language program given to you in Prelab Exercise W1-0, I want you to modify the assembly language program, as you were asked to do in the prelab, so that it computes the product of the integers instead of the sum. Test run your program with several different inputs. You do not need to hand in your solution.
Next, write the assembly language program corresponding to your solution to Prelab Exercise W1-1, in which positive integers are read in and the average is written out, with a negative number indicating the end of input. You should need to make only a few changes to the W1-0.a assembly language program to create your W1-1.a assembly language program. You should save your new program in a file entitled "W1-1.a". Then you should assemble it, load it, and test it by running it with various inputs.
What to hand in
;-----------------------------------
; Dale Skrien
; February 15, 2008
; W1-1
; This program computes the average of a list of positive numbers.
;-----------------------------------
This exercise is due Monday, Feb. 18, at the start of class.
Extra credit: What does "Wombat" stand for in computer-ese?
Quitting CPU Sim and Logging out
To quit CPU Sim, just choose "Exit CPU Sim" from the File menu or click the close box in the upper-right corner of the desktop window. You may be asked whether you want to save the changes to the Wombat1 or an assembly language program. Click "No" for each of these and then CPU Sim will quit running.
Addendum A: Backup copies
Whenever you hand in a project, you must keep a backup copy in case the copies (electronic or hard) that you hand in to me get lost. Therefore, if you regularly work in a public lab, it is a good idea to either bring a removeable storage device like a USB flash memory device onto which you save your files or email a copy of your files to yourself for backup. If you save only a hard copy, you will need to be prepared to recreate an electronic copy from the hard copy, if necessary.
Addendum B: Logging out
Don't forget to always log out of computers in public places before you leave. Not only is it the polite thing to do, but it also minimizes the opportunities for others to succumb to the temptation to abuse your account or to cheat.
Addendum C: Running CPU Sim from other places
For future projects, you may prefer to work at home or in a different lab. If those machines do not have CPU Sim installed on them (CPU Sim should have been installed on all machines in the Olin and Mudd labs), feel free to go to the CPU Sim home page and download the CPU Sim code to the machine. I'll be happy to help you if you encounter problems.