The Execute Menu

Execution of an assembly language program in CPU Sim involves the following steps:

  1. load into CPU Sim the machine on which your program is to be run,
  2. initialize the contents of the registers to the values you desire,
  3. create or open a text file that contains the assembly program to be run,
  4. assemble and load the program into the memory of your simulated machine,
  5. run the program.

You have the choice of running the program without interruptions (other than interruptions caused by errors in your code) by choosing "Clear, assemble, load & run, "Assemble, load & run", "Clear registers & run", or just "Run" from the Execute menu (see Figure 13), or you can step through the program one machine instruction or one microinstruction at a time by using debug mode.


Figure 13. The Execute menu.

Debug Mode

The Debug Mode menu item causes the debug toolbar to be displayed.

Assemble

The "Assemble" menu item checks your code for legality. If the code is legal, nothing happens. If the code is illegal, an error message will appear and the offending line of assembly code will be highlighted for you to edit. Note that the code in any text window must be saved to a file before it can be assembled. If you try to assemble it before saving it, a dialog box will ask you to save it first. Note also that CPU Sim does not create executable files or object files. Every time you want to run a program, that program must be reassembled.

Assemble & load

The "Assemble & load" menu item assembles your code and, if it is legal, loads the assembled (machine) code into the simulated machine's memory. The RAM into which the code is loaded is not first cleared (set to 0), and so the only memory locations that are changed are those into which the new code is loaded. If you want to clear the rest of RAM, you need to do so first by choosing the appropriate menu item from the Execute menu.

Note 1: You can use the Options submenu of the Execute menu (discussed below) to specify which RAM is to be loaded with the machine code and what starting address is to be used.

Note 2: When you load the assembled code into RAM, all the code is treated as one long string of bits that is loaded into the cells of RAM from left to right. Therefore, for example, if you have two 8-bit instructions as your code and RAM has cells of size 5 bits, then the first 15 bits of the 16 bits of code will be loaded into three cells and the remaining code bit will be loaded as the high order bit in the next cell.

Assemble, load & run and Clear, assemble, load & run

The "Assemble, load, & run" menu item is a convenience item that is equivalent to choosing the "Assemble & load" menu item followed by the "Run" menu item. Similarly, the "Clear, assemble, load & run" menu item is a convenience item that is equivalent to choosing the "Clear everything" menu item followed by "Assemble, load & run".

Run

The "Run" menu item causes the machine to begin execution. The program will always begin execution with the first microinstruction of the fetch sequence. The program will stop when one of the following events occurs:

  1. One or more condition bits that are designated as halt bits have their value set to 1.
  2. The user selects "Stop" from the Execute menu.
  3. The user selects "Cancel" when an input dialog is displayed.
  4. The user presses enter or return instead of a value when input is requested in the console panel.
  5. An error occurs.

Stop

The "Stop" menu item causes the currently executing program to halt. This menu item is particularly useful if the program gets into an infinite loop.

Note that this menu item is one of several ways to stop a program in an infinite loop. An alternative is to set the value of a condition bit to 1 by manually editing the contents of the register containing the condition bit. Or you can edit the contents of RAM to get the program to execute a halt instruction or to crash. If the program is repeatedly asking for input in an infinite loop in the console panel, you can get the program to halt by just pressing the Return/Enter key without entering any value. If the program is repeatedly asking for input from a dialog box, you can select the Cancel button in the dialog.

Reset Everything

The "Reset everything" menu item causes all registers, register arrays, and RAMs to be cleared. It also resets the control unit (to begin execution at the start of the fetch sequence), and resets all the IO channels. If the IO channel is a file, the file is closed. Note that you can also clear any individual register or cell of RAM manually by editing its contents in the register or RAM windows.

Options|Highlighting...

The Highlighting menu item in the Options submenu brings up a dialog box in which the user can choose which lines of code are to be highlighted while stepping through the code. Note that this highlighting occurs only when the code is executing in debug mode. The highlighting is specified via pairs consisting of a register and an RAM. The row of the RAM whose address corresponds to the value of the register is highlighted.

For example, the user may wish to highlight the current top of the stack while stepping through the code. To do so, the user needs to create a new register/RAM pair where the register is the stack top pointer register (containing the address of the top of the stack) and the RAM is the stack RAM.

The user may also wish to highlight the cell of main memory containing the currently executing instruction. This row normally corresponds to the address in the program counter register. In this case, the user would create a new register/RAM pair with the program counter register and the code store RAM. NOTE: If the user creates such a pair, then, when stepping through the code, the row of source code (in a source code file) corresponding to the highlighted row of the code store RAM will also be highlighted.

If the user wishes the highlighting to be updated after each microinstruction step, then the "dynamic" checkbox should be checked. Otherwise, the highlighting is updated only at the beginning of each machine cycle.

Options|Loading...

The Loading menu item in the Options submenu brings up a dialog box in which the user can specify which RAM is the code store, i.e., the RAM in which assembled code is to be loaded. The user can also specify which address of the code store is to be the starting address for loading.

Options|IO connections...

The IO connections menu item in the Options submenu brings up a dialog box in which the user can specify where each IO microinstruction will get or put data (see Figure 14).


Figure 14. The IO connections dialog.

The table has one row for each IO microinstruction in the current machine. The "name" column gives the name of the IO microinstruction. The "connection" column gives the current source or destination for the data when the microinstruction is executed. If the connection is "[User]" then the data is sent to or read from the user via a dialog box, one data value at a time. If the connection is "[Console]" then the data is sent to or read from the user, one data value at a time, via the console panel at the bottom of the main desktop window. The console panel is highlighted in yellow whenever it is waiting for the user to type in a value. If the connection is a file name, then the data is sent to or read from that file. The file must be a text file.

If you select "File..." from the popup menu, a dialog box will appear in which you can choose a new text file to be the source or destination of the data. If two IO microinstructions both read from the same file or both write to the same file, they do so using the same data stream. That is, if one IO microinstruction reads a value from the file, then the other IO microinstruction cannot read that value from the file and instead will read the next value.

When CPU Sim attempts to read a base-10 integer from a text file, it reads past any white space (space, new line, carriage return, and tab characters), reads an optional '+' or '-' character, and then reads and appends digits (0-9) until a non-digit character is encountered. When CPU Sim attempts to read a character (ASCII or Unicode) from a file, it just reads the next character of the file. If you want to store binary data in a text file to be read by an IO microinstruction, precede the binary digits with a "0b". For example, if you want to store the binary value of "-1101" then the text file should have the value "-0b1101". If you want to store hexademical data in a file for later reading by an IO microinstruction, precede the hexadecimal value with the prefix "0x".

When CPU Sim attempts to write an integer to a text file, it first writes a space character so that successively written integers can be distinguished from each other. It then writes the integer in base 10 format. When CPU Sim attempts to write a character (ASCII or Unicode) to a file, it writes the character immediately with no additional spacing.

When a program is run using the "Assemble, load, & run", "Run", "Clear registers & run", or "Clear, assemble, load & run" menu items in the Execute menu, the data files are opened just before running and are closed when execution is halted (for whatever reason). If the user enters debug mode, the files are opened when execution begins for the first time (by clicking the "Go", "Step by Instr", or "Step by Micro" buttons). When in debug mode, the files are not closed until the user exits from debug mode, the user clicks the "Start over" button in the debug toolbar, or the user selects a new connection (using the "Options|IO connections..." menu item) for the IO microinstruction connected to the file.

NOTE: Clicking the "Backup one Instr" or "Backup one Micro" buttons in the debug toolbar does not back up the reading or writing of text files. That is, the current position of reading from or writing to text files does not change when you click the "Backup one Instr" or "Backup one Micro" button.