Important Change

  • The assignment has changed from the course packet. Now, your goal is to compute the expected value of the option (instead of the probability that it is in the money). Here's the updated assignment.

  • The Java TOY simulator from the lecture page is available for download here. It should work on both Windows and Unix machines. After you have saved the "toy.zip" file on your computer:
  • Make a new folder (or directory).
  • Extract the files from "toy.zip" and put it in this directory.
  • Remember where these files are.
  • Open the toy.html file from this directory using your favorite web browser. The java demo should now be running.
  • To modify or add TOY machine language programs, edit the obvious parts in the "toy.html" file; then reopen the file "toy.html" in your browser or using appletviewer.

  • Goals

  • Simulate the behavior of a stock using a random walk.

  • Learn to program in the TOY machine language.

  • Appreciate the programming and debugging advantages of a structured programming language over machine language!

  • Part 0:   preparation   (0 points)

  • Review the TOY lecture notes and also the more detailed TOY notes included in your course packet.

  • Copy the following files from /u/cs126/files/toy/ to an empty directory:
    toy.c   bits.toy
    
    You may do this with the following command:
    cp /u/cs126/files/toy/* .
    

  • To execute a TOY program first compile the TOY simulator with:
    gcc toy.c
    
    Then, simulate your TOY program with:
    a.out < bits.toy
    

  • Part 1:   option.toy   (9 points)

  • You may start from the TOY program bits.toy. It prints out 30 pseudo-random bits using a linear feedback shift register. (The TOY simulator also prints out a "core dump", i.e., a listing of all the nonzero memory locations and registers. You may find this useful in debugging, e.g., to make sure your TOY code was read in properly.) Pay careful attention to the mechanics of a "for-loop" since you will have to write one later.

  • Here's a few hints for getting started.

  • Step 0. Write pseudocode on paper, and figure out which registers will get used for each task. Step 1. Modify the bits.toy program so that it perform 1 experiment. Initialize one register, say R4, to the hex equivalent of $50. Then increment or decrement R4 thirty times, depending on the value returned by the LFBSR function call. If you print R4, you should get the following output.

  • Step 2. Determine the value of the option: it is the maximum of 0 and R4 - 55.

  • Step 3. Repeat this experiment 255 times. Use R5 to record the cumulative profit. Here are the values for R5 after each experiment. An estimate of the option's expected value is R5 / 255. Note: you are not required to write TOY code to compute this value.

  • Debugging Hints

    Here are some debugging hints that may help you out.

  • Remember that all values, "line numbers", and arithmetic are in hex. This is by far the most common error.

  • The pc is initially set to 10, so this should be the first line of your TOY program.

  • Comment your TOY code. Also, this may sound silly, but don't accidentally update your comments and forget to update the actual code! The Java simulator does not currently support comments, but be sure to include them in your final submission. We'll award 1 extra credit point to any Java whiz who wants to fix this annoying shortcoming.

  • You have only 8 registers to work with, so you may need to reuse some registers for different purposes. Be grateful that you have 8; many early computers had only 2, so you would need to store variables in memory, and read them in as needed. Recall that there is no register R8, and that all registers are gloabal variables, so be careful.

  • Check the initial "core dump" to be sure that your program was read in correctly. This will fix common errors like having two or more instructions with the same line number, or forgetting the colon after the memory address.

  • Watch out for jump statements - if you insert a new line of code between existsing lines, the location that you want to jump to may change. Also, after updating the line numbers, check that there are no inadvertent "gaps" in line numbering. You need to be careful to use consecutive "line numbers", since if you don't specify the initial contents of some memory address, it is set to 0000 which means halt. You may use "dummy" instructions as in the TOY notes.

  • If you use the C version of the TOY simulator, use opcode 4 to print out the value of a particular register. This is the analog of printf debugging in C. In the Java version, the output goes to the Java console. To view this in Netscape 4.7, select Communicator-Tools-Java Console; in IE 5, select Tools-Internet Options-Advanced-Java Console Enabled.

  • If you are using the C version of the simulator, you may find it helpful to add extra dump() and dumpreg() commands in the simulator for debugging.

  • Submission and readme   ( 1 point)

  • Use the following submit command:
    /u/cs126/bin/submit 5 readme option.toy
    
    Do not use different file names.

  • Your option.toy program should be cleaned up so that it only prints the 255 successive sums - the ith value is the total profit obtained in the first i option experiments. Remove all other system call instructions.

  • The readme file should contain

  • Name, precept number.

  • High level description of code,any serious problems you encountered, and whatever outside help that you received.

  • Include a table indicating what each of the registers are used for.

  • Give your estimate of the value of 1 option in dollars and cents. Use a calculator to do the division. (If you feel adventuresome, feel free to write TOY code to do it.)

  • Indicate whether you used the C or Java TOY simulator to develop your code.



  • Kevin Wayne