After you have telneted to an ECE machine, type the following commands.
0001011001000010You can also type the same instruction as:
0001 011 001 000 010This makes the fields of the instruction more meaningful and your program will be easier to debug.
First line of your programs
The first line of your instruction must contain the address of your program in binary format. For example, if you want the simulator to load your program starting at memory address x3000, the first line of your program should contain
0011 0000 0000 0000Adding comments to your program
You can add comments to your program by using the semi-colon (;). Any text entered after a semi-colon will be ignored by the convert program which you will use to convert your program to a format that is readable by the LC-2 simulator. An example:
0101 001 010 000 011 ; and r2 and r3, store result in r1A sample program in LC-2 machine language (in binary format) The following program OR's the contents of register 1 and register 2 and puts the result in register 3.
0011 0000 0000 0000 ; start at x3000 1001 100 001 111111 ; r4 <- not(r1) 1001 101 010 111111 ; r5 <- not(r2) 0101 110 100 000 101 ; r6 <- (r4) and (r5) 1001 011 110 111111 ; r3 <- not(r6) 1111 0000 00100101 ; trap x25 (halt)
A sample program in LC-2 machine language (in hex format) This is the same sample program we used to illustrate the binary format.
3000 ; start at x3000 987f ; r4 <- not(r1) 9abf ; r5 <- not(r2) 5d05 ; r6 <- (r4) and (r5) 97bf ; r3 <- not(r6) f025 ; trap x25 (halt)Note that you cannot put x's to in front of the instructions. (x987f is not an acceptable format) Also note that you cannot mix the binary and hex formats, that is you cannot write some instructions in binary and some instructions in hex. Hex digits a, b, c, d, e, f can be uppercase or lowercase as you wish.
convert -b2 <program_name> <output_filename>If the name of your program is myprog.bin and you want the output filename to be myprog.obj, all you need to type is
convert -b16 <program_name> <output_filename>to invoke the convert program. Similarly, if you named your program myprog.hex and want to name the output myprog.obj, all you need to do is type
convert -b16 myprog.hex myprog.objFor the convert program to work correctly, you need to set your path as described in the "Setting your path" section. If that doesn't work instead of convert use
A sample program in LC-2 assembly language. This is the same sample
program illustrated above in machine language.
For the assemble program to work correctly, you need to set your path
as described in the "Setting your path" section. If you haven't done that
step, use the command
If this does not work try setting your path or typingsimulate &
After you hit enter, you will see two windows popping up. One is a white window with a bunch of numbers on it. This is the LC-2 simulator window which you need to get familiar with. The other window is a black window. It is the console and important messa ges will be printed on that window. Now let's load the program you converted into the LC-2 simulator./usr/local/packages/lc2/bin/simulate &
Filemenu on the menu bar. The menu will drop down and you will see several options. Click on the
Load Program...option. A file selection box will pop up. Find the file you want to load into the simulator (e.g. myprog.obj) highlight it by clicking on it and then click OK. You will see that the middle part of the simulator window, which represents the memory of LC-2 will be loaded with 1's and 0's. These are the instructions in your program. For example, if you have loaded the program we demonstrated above and converted, the first few lines of the Memory section on the simulator will be
:1001100001111111 987f ; not R4, R1 :1001101010111111 9abf ; not R5, R2 :0101110100000101 5d05 ; and R6, R4, R5 :1001011110111111 97bf ; not R3, R6 :1111000000100101 f025 ; trap $25 :0000000000000000 0000 ; brnop $3000 :0000000000000000 0000 ; brnop $3000 :0000000000000000 0000 ; brnop $3000The numbers in brackets represent the hex address of the location and its contents. In this case memory location x3000 contains the not instruction we wrote previously. Instructions are given in three formats, binary, hex, and assembly to make it easier t o debug.
Now we have successfully loaded our simple program into LC-2's memory.
Let's try running and debugging it.
A good program should start by initializing the registers it is going to use and not rely on them being 0 at the beginning. Apparently we have not written a very good program, because we never initalize the values in the registers we are using. LC-2 si mulator gives you an option of initialing the registers to 0. To do this, click on
Set Valueson the menu bar. In the menu, click on
Clear RegistersAfter doing this you will see that the contents of all registers except for the PC are 0. PC is x3000 (Why?). The values in registers R0-R7 are given both in hex and decimal. The values of PC and IR are given in hex. Would giving a decimal representation of the contents of PC and IR provide any useful information to you?
Setting Registers and Memory
To test your program you might want to set the values of some registers and memory locations before you execute your program. You can do this again using the "Set Values" menu on the menubar. As our program OR's the values in registers 1 and 2, let's p ut some values into those registers before running our program. To do this, click on
Set Valueson the menu bar. In the menu, click on
Set Register or MemoryTo set r1, type "r1" (without the quotes) into the box corresponding to Register/Memory Location. Let's set r1's value to 1. Type x0001 in the box corresponding to Value. (You can also type the value in decimal if you want. For example if yo u want to put decimal 14 in a register you need to type #14). Click on
Setafter you fill in the appropriate values. In the CPU section of the simulator you'll see that R1 contains 0001. This means that you've successfully set R1. To set R2, follow the same steps and put x0011 into R2.
To test this simple program you do not need to set any memory locations. But to test your programs you probably will. To do this again click on "Set Values" on the menu bar. Then click on "Set Register or Memory". In the box for "Register/Memory location" put the memory address you want to set (for example x3200). In the "Value" box type the value you wish to be put in that location in memory (for example xFFAD). Click on "Set" or press enter to set that memory location. Now in your screen you probably ca nnot see the contents of memory location x3200. In order to see whether you have set that location, you can scroll down the Memory part of the simulator window using the scroll bar on the right. To do that you can use either the left or the middle mouse button. Left mouse button scrolls down by one location. Middle mouse button scrolls down arbitrarily. You can adjust this arbitrary place by clicking on the scroll bar with the middle button and sliding the scroll bar down until you see the memory l ocation you want to observe.
Displaying Memory Locations
There's also another way to display a memory location. To experiment with this click on
Displayon the menu bar. Then click on
MemoryA dialog box will ask you the starting (From:) and ending (To:) addresses of the part of memory you want to see. If you want to see only one memory location just type an address in the "From:" box and click on "Print". In this case, let's type x3200 in th e "From:" box and click "Print". Look down in the Information window part of the LC-2 simulator to see the value of location x3200. Make sure that the value stored in that location is ffad.
To display multiple memory locations enter two separate addresses in the "From:" and "To:" fields. The memory locations will be displayed in the Memory window.
Running the Program
Now we have all the values ready to test if our program is working. What would you expect to be stored in R3 if you run this program in this setting? As this program is supposed to OR registers R1 and R2, you would expect that the value of R3 after the en d of the execution of program would be x0011. (Verify that x0001 OR x0011 = x0011). To see whether this is the case click on
Runon the menu. Then click on
Run Program...For the "Start address" type 0x3000 and click "Run". Some changes will happen in the CPU and Information windows. Verify that final result stored in R3 is x0011.
Stepping Through Your Program
Now you have just learned how to run a program on LC-2. A more useful tool for debugging (finding the errors in the program) your program is stepping through the instructions in your program. This means that you run one instruction at a time and observe t he changes each instruction makes in the LC-2. This way you can find problems in the individual instructions and you can exactly pinpoint the place somthing goes wrong in the program. Let's give this a try. But first let's reinitialize the LC-2 and load o ur program again. To do that, click on
Set Valueson the menu bar. Then click on
Reinitialize Machineto zero out all the registers and memory locations. Now following the steps you learned, load the program into LC-2 again and set R1 to be x0001 and R2 to be x0011. To step through your program instruction by instruction click on "Run" on the menu bar and then click on
Step Program...Choose the "number steps" to be 1 (which means the machine will execute 1 instrcution and then stop so that you can observe the changes made by that instruction). Click "Step" and observe what happened in the CPU window. Notice that PC is now x3001 , which points to the next instruction. LC-2 has just executed the first instruction. The result of the first instruction is stored in the destination register designated by that instruction (R4). Verify that the value in R4 is the complement of the value in R1. This was excatly what the first instruction supposed to do. You can now step again and observe the effects of the second instruction. Notice the changes in registers R0-R7 and also PC, IR, and Condition Codes. Are they what you would expect from t he instruction? (they'd better be :)
If you are already tired of stepoping instruction by instruction you can click on "Continue" in the "Step Program" mini pop-up window. This will execute the rest of the instructions in the program and in the end the mini-window will disappear. If not you can step your program until you reach the end.
Making Life Easier: Using Breakpoints
You write a program and you are sure that some part is working and you do not want to step one by one all those instructions that you are sure are working. LC-2 simulator gives you the option to use breakpoints so that you don't have to step through all i nstructions in your program. The idea is, you set a breakpoint at some instruction in your program and when you run your program LC-2 simulator will execute all the instructions up to that break point and stop at the breakpoint so that you can observe wha t has happened until then. You can set multiple breakpoints in your program. When you run the program, the LC-2 simulator will execute the instructions between two breakpoints and stop at each breakpoint encountered. Let's see how you can set a breakpoint in our program. First reinitialize the machine and load our program again into the LC-2 simulator. Set the value of R1 to x1100 and R2 to x0011. Now, to set a breakpoint click on "Run" on the menu bar. Then click on
Breakpoints...A window asking you the address of the instruction you want to put the breakpoint at will pop up. Using that window, you can set and delete breakpoints and also list the current breakpoints in the program. Let's set a breakpoint at the and instruction. Ty pe x3002 in the box corresponding to "Address" and click "Add". In the memory window you will see a B appear next to the memory location x3002. This indicates that there's a breakpoint at that location. Now, when you run your program the executin w ill stop before the processing of that instruction. Let's see if this is the case. Click on "Run" on menubar and then "Run Program..." When you hit enter, the program will pop up a window saying "Breakpoint encountered at 0x3002". The execution of the pro gram stops at that instruction. Now observe what's in registers R5 and R5 and verify that the first two NOT instructions are correctly executed by our Little Computer.
After stopping at a breakpoint you can continue execution by clicking on "Continue" on the little window. This will execute the program until the next breakpoint or until the end of your program.
Why Would You Ever Want to Use the Right Mouse Button?
Yes, the right mouse button has a function in the simulator. Anywhere on the LC-2 simulator window (the "white" window) if you click on the right mouse button and hold it, you will see a pull-down menu of "Common Operations". Pulling down your mouse to the appropriate command, you can load, run, step through your programs, and set breakpoints. This is just an alternative to the menubar.
Last Few Comments
This is a very fast-written document, so there might be minor (hopefully not major) errors in it. If you find a bug in this document, please contact any of the TAs. A section on assembly programming will be added later. Enjoy your ride through the Little Computer...