First develop commit. Adding all existing files.

This commit is contained in:
2014-03-14 20:12:38 +01:00
parent bf68ba4560
commit 73e62547ff
441 changed files with 247478 additions and 2 deletions

14
resources/TOY/add.toy Normal file
View File

@@ -0,0 +1,14 @@
program Add
// Input: Stored in memory location 00 and 01
// Output: Sum of two integers 5 + 8 = D saved in memory location 02.
// Remarks:
// -----------------------------------------------------------------------------
00: 0008 (0000 0000 0000 1000, 8)
01: 0005 (0000 0000 0000 0101, 5)
02: 0000 (0000 0000 0000 0000, 0)
10: 8A00 R[A] <- mem[00]
11: 8B01 R[B] <- mem[01]
12: 1CAB R[C] <- R[A] + R[B]
13: 9C02 mem[02] <- R[C]
14: 0000 halt

View File

@@ -0,0 +1,37 @@
TOY REFERENCE CARD
INSTRUCTION FORMATS
| . . . . | . . . . | . . . . | . . . .|
Format 1: | opcode | d | s | t | (0-6, A-B)
Format 2: | opcode | d | addr | (7-9, C-F)
ARITHMETIC and LOGICAL operations
1: add R[d] <- R[s] + R[t]
2: subtract R[d] <- R[s] - R[t]
3: and R[d] <- R[s] & R[t]
4: xor R[d] <- R[s] ^ R[t]
5: shift left R[d] <- R[s] << R[t]
6: shift right R[d] <- R[s] >> R[t]
TRANSFER between registers and memory
7: load address R[d] <- addr
8: load R[d] <- mem[addr]
9: store mem[addr] <- R[d]
A: load indirect R[d] <- mem[R[t]]
B: store indirect mem[R[t]] <- R[d]
CONTROL
0: halt halt
C: branch zero if (R[d] == 0) pc <- addr
D: branch positive if (R[d] > 0) pc <- addr
E: jump register pc <- R[d]
F: jump and link R[d] <- pc; pc <- addr
Register 0 always reads 0.
Loads from mem[FF] come from stdin.
Stores to mem[FF] go to stdout.

25
resources/TOY/chop.toy Normal file
View File

@@ -0,0 +1,25 @@
program Chop
// Input: N
// Output: The unique set of powers of 2 which sum to N
// Remarks: This program uses the fact that numbers in the toy machine are
// represented by 16-bit signed two's complement numbers. To learn
// more about signed two's complement numbers, please see Professor
// Wayne's notes on the TOY machine. Note: a non-positive value of N
// will cause undefined behavior.
// -----------------------------------------------------------------------------
// Initialize
10: 82FF read R[2]
// Check for non-positive values
11: D213 if (R[2] > 0) goto 13
12: 0000 halt
// Loop
13: 2302 R[3] <- -R[2]
14: 3423 R[4] <- R[2] & R[3]
15: 94FF write R[4]
16: 2224 R[2] <- R[2] - R[4]
17: D211 if (R[2] > 0) goto 11
18: 0000 halt

30
resources/TOY/crazy8.toy Normal file
View File

@@ -0,0 +1,30 @@
program Crazy 8
// Input: A list of up to 16 positive integers terminated by a 0000
// Output: The positive integers in reverse order
// Remarks: The data is stored starting at memory location 00.
// If you enter more than 16 integers, you will overwrite
// the program itself. To see the crazy 8 virus, enter
// 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 (16 1's)
// 8888 8810 98FF C011
// -----------------------------------------------------------------------------
10: 7101 R[1] <- 0001 R[1] always 1
11: 7A00 R[A] <- 0000 memory address of array a[]
12: 7B00 R[B] <- 0000 # elements in array = n
// Read in sequence of positive integers
13: 8CFF read R[C] while (1) {
14: CC19 if (R[C] == 0) goto 19 if ((read R[C]) == 0) break
15: 16AB R[6] <- R[A] + R[B] a + n
16: BC06 mem[R[6]] <- R[C] a[n] = c
17: 1BB1 R[B] <- R[B] + R[1] n++
18: C013 goto 13 }
// Print out results in reverse order
19: CB20 if (R[B] == 0) goto 20 while (n != 0) {
1A: 16AB R[6] <- R[A] + R[B] a + n
1B: 2661 R[6] <- R[6] - R[1] a + n - 1
1C: AC06 R[C] <- mem[R[6]] c = a[n-1]
1D: 9CFF write R[C] print c
1E: 2BB1 R[B] <- R[B] - R[1] n--
1F: C019 goto 19 }
20: 0000 halt

View File

@@ -0,0 +1,16 @@
program Fibonacci (print to stdout)
// Input: None
// Output: Prints positive fibonacci numbers to standard output.
// -----------------------------------------------------------------------------
00: 0000 0
01: 0001 1
10: 8A00 RA <- mem[00] a = 0
11: 8B01 RB <- mem[01] b = 1
while(a > 0) {
12: 9AFF print RA System.out.println(a)
13: 1AAB RA <- RA + RB a = a + b
14: 2BAB RB <- RA - RB b = a - b
15: DA12 if (RA > 0) goto 12 }
16: 0000 halt

30
resources/TOY/gcd.toy Normal file
View File

@@ -0,0 +1,30 @@
program Greatest Common Divisor
// Input: a, b
// Output: gcd(a, b)
// Remarks: Uses Euclid's inefficient subtraction algorithm does not handle
// negative integers
// -----------------------------------------------------------------------------
10: 8AFF read R[A] a
11: 8BFF read R[B] b
12: FF30 R[F] <- pc; goto 30
13: 9CFF write R[C] gcd(a, b)
14: 0000 halt
function gcd
// Input: R[A] and R[B] (should be passed by value)
// Return address: R[F]
// Output: R[C] = gcd(R[A], R[B])
// Temporary variables: R[D]
30: CB38 if (R[B] == 0) goto 38 while (b != 0) {
31: 2DAB R[D] <- R[A] - R[B] if (b >= a) {
32: DD36 if (R[D] > 0) goto 36 // swap and b
33: 1DA0 R[D] <- R[A] temp = a;
34: 1AB0 R[A] <- R[B] a = b;
35: 1BD0 R[B] <- R[D] b = temp;
// }
36: 2AAB R[A] <- R[A] - R[B] a -= b;
37: C030 goto 30 }
38: 1CA0 R[C] <- R[A] c = a;
39: EF00 goto R[F] return c;

View File

@@ -0,0 +1,18 @@
program Gray Code
// Input: Integer n (0 - E) from standard input.
// Output: Gray code of order n to standard output.
// -----------------------------------------------------------------------------
// Initialize
10: 8AFF read RA n = StdIn.readInt()
11: 7101 R1 <- 1 always 1
12: 5B1A RB <- 1 << RA i = 2^n
while (i != 0) {
13: 2BB1 RB <- RB - 1 i = 2^n - 1
14: 6CB1 RC <- RB >> R1 c = (i >> 1)
15: 4DBC RD <- RB ^ RC d = i ^ (i >> 1)
16: 9DFF write RD print RD
17: DB13 if (RB > 0) pc <- 13 }
18: 0000 halt

40
resources/TOY/horner.toy Normal file
View File

@@ -0,0 +1,40 @@
program Horner's method
// Input: x, n, a_n, . . ., a_2, a_1, a_0
// Output: p_n(x) = a_n x^n + . . . + a_2 x^2 + a_1 x + a_0
// Remarks: polynomial evaluation via Horner's method
// Idea: p_3(x) = ((a_3 * x + a_2) * x + a_1) * x + a_0
// Example 1 : to convert 765 decimal to hex, enter input
// A 2 7 6 5
// Example 2: to convert 100 1111 0011 from binary to hex, enter
// 2 A 1 0 0 1 1 1 1 0 0 1 1
// -----------------------------------------------------------------------------
10: 7C00 R[C] <- 0000 result c
11: 7101 R[1] <- 0001 always 1
12: 82FF read R[2] read x
13: 83FF read R[3] read n
14: 84FF read R[4] do { read a_i
15: 1A20 R[A] <- R[2] |
16: 1BC0 R[B] <- R[C] | c *= x
17: FF30 R[F] <- pc; goto 30 |
18: 1CC4 R[C] <- R[C] + R[4] c += a_i
19: C31C if (R[3] == 0) goto 1C |
1A: 2331 R[3] <- R[3] - R[1] | } while (i-- >= 0)
1B: C014 goto 14 |
1C: 9CFF write R[C]
1D: 0000 halt
function multiply
// Input: R[A] and R[B] (should be passed by value)
// Return address: R[F]
// Output: R[C] = R[A] * R[B]
// Temporary variables: R[1] = 1
30: 7C00 R[C] <- 0000
31: 7101 R[1] <- 0001
32: CA36 if (R[A] == 0) goto 36
33: 1CCB R[C] <- R[C] + R[B]
34: 2AA1 R[A] <- R[A] - R[1]
35: C032 goto 32
36: EF00 goto R[F]

View File

@@ -0,0 +1,9 @@
program Infinite Loop
// Input: -
// Output: -
// Remarks: This program contains an infinite loop.
// -----------------------------------------------------------------------------
10: 1000 no-op
11: 1000 no-op
12: C010 goto 10

View File

@@ -0,0 +1,30 @@
program Fast Multiply
// Input: integers a and b stored in mem[0A], mem[0B]
// Output: integer c = a * b stored in mem[0C]
// Remarks: Binary Multiplication
// -----------------------------------------------------------------------------
0A: 0003 (0000 0000 0000 0011, 3) input a
0B: 0009 (0000 0000 0000 1001, 9) input b
0C: 0000 (0000 0000 0000 0000, 0) output c
0D: 0000 (0000 0000 0000 0000, 0) constant 0
0E: 0001 (0000 0000 0000 0000, 1) constant 1
0F: 0010 (0000 0000 0001 0000, 16) constant 16
10: 8A0A R[A] <- mem[0A]
11: 8B0B R[B] <- mem[0B]
12: 8C0D R[C] <- mem[0D] result
13: 810E R[1] <- mem[0E] always 1
14: 820F R[2] <- mem[0F] i = 16
do {
15: 2221 R[2] <- R[2] - R[1] i--
16: 53A2 R[3] <- R[A] << R[2] a << i
17: 64B2 R[4] <- R[B] >> R[2] b >> i
18: 3441 R[4] <- R[4] & R[1] bi = ith bit of b
19: C41B if (R[4] == 0) goto 1B if bi is 1
1A: 1CC3 R[C] <- R[C] + R[3] add a << i to sum
1B: D215 if (R[2] > 0) goto 15 }
1C: 9C0C mem[0C] <- R[C]

View File

@@ -0,0 +1,35 @@
program Multiply-function
// Input: x, y, and z
// Output: x * y * z
// Remarks: Inefficient for large or negative values of x or y
// -----------------------------------------------------------------------------
10: 82FF read R[2] x
11: 83FF read R[3] y
12: 84FF read R[4] z
// Multiply x and y
13: 1A20 R[A] <- R[2] x
14: 1B30 R[B] <- R[3] y
15: FF30 R[F] <- pc; goto 30 x * y
// Multiply (x * y) and z
16: 1AC0 R[A] <- R[C] x * y
17: 1B40 R[B] <- R[4] z
18: FF30 R[F] <- pc; goto 30 (x * y) * z
19: 9CFF write R[C]
1A: 0000 halt
function multiply
// Input: R[A] and R[B] (should be passed by value)
// Return address: R[F]
// Output: R[C] = R[A] * R[B]
// Temporary variables: R[1] = 1
30: 7C00 R[C] <- 0000
31: 7101 R[1] <- 0001
32: CA36 if (R[A] == 0) goto 36
33: 1CCB R[C] <- R[C] + R[B]
34: 2AA1 R[A] <- R[A] - R[1]
35: C032 goto 32
36: EF00 goto R[F]

View File

@@ -0,0 +1,27 @@
program Multiply
// Input: integers a and b stored in mem[0A], mem[0B]
// Output: integer c = a * b stored in mem[0C]
// Remarks: Inefficient
// -----------------------------------------------------------------------------
0A: 0003 (0000 0000 0000 0111, 3)
0B: 0009 (0000 0000 0000 1001, 9)
0C: 0000 (0000 0000 0000 0000, 0)
0D: 0000 (0000 0000 0000 0000, 0)
0E: 0001 (0000 0000 0000 0001, 1)
10: 8A0A RA <- mem[0A] a
11: 8B0B RB <- mem[0B] b
12: 8C0D RC <- mem[0D] c = 0;
13: 810E R1 <- mem[0E] always 1
14: CA18 if (RA == 0) pc goto 18 while (a != 0) {
15: 1CCB RC <- RC + RB c = c + b;
16: 2AA1 RA <- RA - R1 a = a - 1;
17: C014 pc <- 14 }
18: 9C0C mem[0C] <- RC
19: 0000 halt

13
resources/TOY/powers2.toy Normal file
View File

@@ -0,0 +1,13 @@
program Powers of 2
// Input: None
// Output: Prints out the positive powers of 2
// -----------------------------------------------------------------------------
00: 0001 1
10: 8A00 RA <- mem[00] 1
while (a != 0) {
11: 9AFF write RA System.out.println(a)
12: 1AAA RA <- RA + RA a = a + a
13: DA11 if (RA > 0) goto 11 }
14: 0000 halt

26
resources/TOY/reverse.toy Normal file
View File

@@ -0,0 +1,26 @@
program Reverse
// Input: A list of positive integers terminated by a 0000
// Output: The positive integers in reverse order.
// Remarks: The data is stored starting at memory location 30.
// -----------------------------------------------------------------------------
10: 7101 R[1] <- 0001 R[1] always 1
11: 7A30 R[A] <- 0030 memory address of array a[]
12: 7B00 R[B] <- 0000 # elements in array = n
// read in sequence of positive integers
13: 8CFF read R[C] while (read R[C]) {
14: CC19 if (R[C] == 0) goto 19 if (c == 0) break
15: 16AB R[6] <- R[A] + R[B] a + n
16: BC06 mem[R[6]] <- R[C] a[n] = c
17: 1BB1 R[B] <- R[B] + R[1] n++
18: C013 goto 13 }
// print out results in reverse order
19: CB20 if (R[B] == 0) goto 20 while (n != 0) {
1A: 16AB R[6] <- R[A] + R[B] a + n
1B: 2661 R[6] <- R[6] - R[1] a + n - 1
1C: AC06 R[C] <- mem[R[6]] c = a[n-1]
1D: 9CFF write R[C] print c
1E: 2BB1 R[B] <- R[B] - R[1] n--
1F: C019 goto 19 }
20: 0000 halt

11
resources/TOY/stdin.toy Normal file
View File

@@ -0,0 +1,11 @@
program Stdin
// Input: Two integers from standard input
// Output: Sum of the two integers
// Remarks:
// -----------------------------------------------------------------------------
10: 8AFF read R[A] from stdin
11: 8BFF read R[B] from stdin
12: 1CAB R[C] <- R[A] + R[B]
13: 9CFF write R[C] to stdout
14: 0000 halt

View File

@@ -0,0 +1,14 @@
program Subtract
// Input: Stored in memory location 00 and 01
// Output: Difference of two integers 5 - 8 = FFFD saved in memory location 02.
// Remarks:
// -----------------------------------------------------------------------------
00: 0005 (0000 0000 0000 0101, 5)
01: 0008 (0000 0000 0000 1000, 8)
02: 0000 (0000 0000 0000 0000, 0)
10: 8A00 R[A] <- mem[00]
11: 8B01 R[B] <- mem[01]
12: 2CAB R[C] <- R[A] - R[B]
13: 9C02 mem[02] <- R[C]
14: 0000 halt

14
resources/TOY/sum.toy Normal file
View File

@@ -0,0 +1,14 @@
program Sum
// Input: Sequence of non-zero integers, followed by 0000
// Output: The sum of all the integers.
// -----------------------------------------------------------------------------
// Initialize
10: 7C00 RC <- 0000 sum = 0
while (true) {
11: 8AFF read RA read a
12: CA15 if (RA == 0) pc <- 15 if (a == 0) break
13: 1CCA RC <- RC + RA sum = sum + a
14: C011 pc <- 11 }
15: 9CFF write RC write sum
16: 0000 halt

20
resources/TOY/sum_1-n.toy Normal file
View File

@@ -0,0 +1,20 @@
program Sum 1-N
// Input: N
// Output: The sum of all integers between 1 and N inclusive
// Remarks: Please note that the highest value of N that can be processed
// without overflow is 00FF.
// -----------------------------------------------------------------------------
// Initialize
10: 7101 R[1] <- 0001
11: 82FF read R[2]
12: 7300 R[3] <- 0000
// Add, decrement, and loop
13: 1332 R[3] <- R[3] + R[2]
14: 2221 R[2] <- R[2] - R[1]
15: D213 if (R[2] > 0) goto 13
// Print the sum
16: 93FF write R[3]
17: 0000 halt

BIN
resources/TOY/toy.pdf Normal file

Binary file not shown.