Goals
Simulate the behavior of a stock using a random walk.
Learn to program in the TOY machine language.
Checklist
Use the submit command:
/u/cs126/bin/submit 9 readme toy.c option.toy
. Do not use different file names.
Do not use
5a instead of 9, as this will submit your files into the Assignment 5 directory. (We don't have access to the source code for the submit program, so this is the easiest "hack.")
Review Lectures 4, 5, and 6.
readme
name, precept number
description of problems encountered
high level description of code
include the output of your TOY program, including the initial and final "core dump" (print out a running count of the number times that your option is in the money after each of the 255 simulations)
indicate any help (if any) you received
executing
compile
toy.c
with
lcc
and type
a.out < option.toy
(you may obtain errors with the
cc
compiler since
//
is used for commenting)
toy.c
first, make sure you understand the TOY simulator in C
once you understand the C code, the "upgrade" should be easy
option.toy
It's possible to write the whole TOY program in 16 instructions (not including the LFBSR code provided). Keep in mind that you only have 8 registers (variables) to work with, so these are a scarce resource. (In the old days, programmers only had 4!) So, you will need to carefully plan out your program.
You will want to remove the print statement that displays each random bit - this will ultimately produce 30 * 255 lines of output, which aren't very useful.
Start from the
bits.toy
program that prints out 30 pseudo-random bits. The TOY "for-loop" is created by setting R6 to 30 (actually 1E in hex) and using opcode 7 (jump and count) to decrement R6. Pay careful attention to the mechanics of a "for-loop" since you will have to write one later.
Now, add code to perform 1 simulation. Store the current stock price of $50 in register R4. For each of the 30 pseudo-random bits, increment the stock price by $1 if the bit is 1; decrement it by 1 if the bit is 0. There is no
if-else
construct in TOY; you can obtain the same effect using opcode 6 (jump if positive).
Use register R5 to count the number of experiments in which the option is in the money. If after 30 iterations, the stock price is $56 or more, increment R5 by 1. Here, it will be especially convenient to use the updated opcode 3 (compare and increment).
Once everything else is working, write an outer loop to run through this experiment 255 times. Use register R7 to countdown from 255 to 0. Be sure to do this in hex!
Using the LFBSR to generate pseudo-random bits, the option will be in the money 49 of 255 times. This provides a good approximation to the true mathematical answer of 194129627/1073741824 or roughly 18%. Part of the inaccuracy is due to performing only 255 trials; the rest could be fixed by a better pseudo-random number generator.
TOY debugging hints
Comment your TOY code. Update your comments if you modify your code.
The
pc
is initially set to
10
.
Remember that
all values, "line numbers", and arithmetic are in hex
. This is by far the most common error.
8 registers is not many, so you may need to reuse a register for multiple purposes. All registers are global variables, so be careful.
check the initial "core dump" to be sure that the TOY simulator read in your program as expected (this should 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. A common mistake is to update only the "comments", not the actual instructions. Also, after updating the line numbers, check that there are no inadvertent "gaps" in line numbering.
Be careful to use consecutive "line numbers" - if you don't specify the initial contents of some memory address, it is set to
0000
which means halt. A useful work-a-round is to insert "dummy" instructions to act as placeholders. A good choice is
D000
which does absolutely nothing.
Use opcode 4 to print out the value of a particular register.
Add extra
dump()
and
dumpreg()
commands in your TOY simulator to print out the contents of the pc, memory, and registers, as necessary. Remove these before submitting
toy.c
.