From 3571f998b28fbc8d9250ba04c983935f10a16c15 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 20 Jan 2018 15:02:59 +0100 Subject: removed dat files --- lecture 00 introduction.pdf | Bin 0 -> 228477 bytes lecture 01 Boolean logic.pdf | Bin 0 -> 364179 bytes lecture 02 Boolean arithmetic.pdf | Bin 0 -> 296783 bytes lecture 03 sequential logic.pdf | Bin 0 -> 117285 bytes lecture 04 machine language.pdf | Bin 0 -> 219405 bytes lecture 05 computer architecture.pdf | Bin 0 -> 459238 bytes lecture 06 assembler.pdf | Bin 0 -> 199483 bytes lecture 07 virtual machine I.pdf | Bin 0 -> 491237 bytes lecture 08 virtual machine II.pdf | Bin 0 -> 285458 bytes lecture 09 high level language.pdf | Bin 0 -> 201903 bytes lecture 10 compiler I.pdf | Bin 0 -> 298040 bytes lecture 11 compiler II.pdf | Bin 0 -> 318842 bytes lecture 12 OS.pdf | Bin 0 -> 862887 bytes projects/11/K/Board.jack | 305 +++++++++ projects/11/K/Board.vm | 1183 ++++++++++++++++++++++++++++++++++ projects/11/K/K.txt | 25 + projects/11/K/KGame.jack | 34 + projects/11/K/KGame.vm | 74 +++ projects/11/K/Main.jack | 9 + projects/11/K/Main.vm | 11 + projects/12/Array.jack | 3 +- projects/12/Keyboard.jack | 44 +- projects/12/Math.jack | 19 +- projects/12/Memory.jack | 32 +- projects/12/Output.jack | 73 +-- projects/12/Screen.jack | 22 +- projects/12/String.jack | 49 +- projects/12/Sys.jack | 19 +- 28 files changed, 1766 insertions(+), 136 deletions(-) create mode 100644 lecture 00 introduction.pdf create mode 100644 lecture 01 Boolean logic.pdf create mode 100644 lecture 02 Boolean arithmetic.pdf create mode 100644 lecture 03 sequential logic.pdf create mode 100644 lecture 04 machine language.pdf create mode 100644 lecture 05 computer architecture.pdf create mode 100644 lecture 06 assembler.pdf create mode 100644 lecture 07 virtual machine I.pdf create mode 100644 lecture 08 virtual machine II.pdf create mode 100644 lecture 09 high level language.pdf create mode 100644 lecture 10 compiler I.pdf create mode 100644 lecture 11 compiler II.pdf create mode 100644 lecture 12 OS.pdf create mode 100644 projects/11/K/Board.jack create mode 100644 projects/11/K/Board.vm create mode 100644 projects/11/K/K.txt create mode 100644 projects/11/K/KGame.jack create mode 100644 projects/11/K/KGame.vm create mode 100644 projects/11/K/Main.jack create mode 100644 projects/11/K/Main.vm diff --git a/lecture 00 introduction.pdf b/lecture 00 introduction.pdf new file mode 100644 index 0000000..fc3bbac Binary files /dev/null and b/lecture 00 introduction.pdf differ diff --git a/lecture 01 Boolean logic.pdf b/lecture 01 Boolean logic.pdf new file mode 100644 index 0000000..5c008cc Binary files /dev/null and b/lecture 01 Boolean logic.pdf differ diff --git a/lecture 02 Boolean arithmetic.pdf b/lecture 02 Boolean arithmetic.pdf new file mode 100644 index 0000000..e3d36d4 Binary files /dev/null and b/lecture 02 Boolean arithmetic.pdf differ diff --git a/lecture 03 sequential logic.pdf b/lecture 03 sequential logic.pdf new file mode 100644 index 0000000..cd62945 Binary files /dev/null and b/lecture 03 sequential logic.pdf differ diff --git a/lecture 04 machine language.pdf b/lecture 04 machine language.pdf new file mode 100644 index 0000000..be12c90 Binary files /dev/null and b/lecture 04 machine language.pdf differ diff --git a/lecture 05 computer architecture.pdf b/lecture 05 computer architecture.pdf new file mode 100644 index 0000000..93de285 Binary files /dev/null and b/lecture 05 computer architecture.pdf differ diff --git a/lecture 06 assembler.pdf b/lecture 06 assembler.pdf new file mode 100644 index 0000000..efd84f4 Binary files /dev/null and b/lecture 06 assembler.pdf differ diff --git a/lecture 07 virtual machine I.pdf b/lecture 07 virtual machine I.pdf new file mode 100644 index 0000000..c3df8e3 Binary files /dev/null and b/lecture 07 virtual machine I.pdf differ diff --git a/lecture 08 virtual machine II.pdf b/lecture 08 virtual machine II.pdf new file mode 100644 index 0000000..a2cd652 Binary files /dev/null and b/lecture 08 virtual machine II.pdf differ diff --git a/lecture 09 high level language.pdf b/lecture 09 high level language.pdf new file mode 100644 index 0000000..d97e6f9 Binary files /dev/null and b/lecture 09 high level language.pdf differ diff --git a/lecture 10 compiler I.pdf b/lecture 10 compiler I.pdf new file mode 100644 index 0000000..3719280 Binary files /dev/null and b/lecture 10 compiler I.pdf differ diff --git a/lecture 11 compiler II.pdf b/lecture 11 compiler II.pdf new file mode 100644 index 0000000..4b4aa2b Binary files /dev/null and b/lecture 11 compiler II.pdf differ diff --git a/lecture 12 OS.pdf b/lecture 12 OS.pdf new file mode 100644 index 0000000..24f4f87 Binary files /dev/null and b/lecture 12 OS.pdf differ diff --git a/projects/11/K/Board.jack b/projects/11/K/Board.jack new file mode 100644 index 0000000..49d42d5 --- /dev/null +++ b/projects/11/K/Board.jack @@ -0,0 +1,305 @@ +class Board { + field Array grid; + field int nTurn; + field int seed; + + constructor Board new() { + var int i, j; + var Array t; + let grid = Array.new(4); + let i = 0; + while (i < 4) { + let j = 0; + let grid[i] = Array.new(4); + let t = grid[i]; + while (j < 4) { + let t[j] = 32; + let j = j + 1; + } + let i = i + 1; + } + let t = grid[0]; + let t[0] = 65; + let nTurn = 0; + let seed = 0; + return this; + } + + method void arrange(Array xs, boolean isHorizontal) { + if (isHorizontal) { + do deepcopy(xs, grid); + } else { + do transpose(xs, grid); + } + do dispose4(xs); + return; + } + + + method void copy(Array xs, Array ys) { + var int i; + let i = 0; + while (i < 4) { + let ys[i] = xs[i]; + let i = i + 1; + } + return; + } + + method void deepcopy(Array xs, Array ys) { + var int i; + let i = 0; + while (i < 4) { + do copy(xs[i], ys[i]); + let i = i + 1; + } + return; + } + + method void transpose(Array xs, Array ys) { + var int i, j; + var Array t, s; + let i = 0; + while (i < 4) { + let j = 0; + let t = ys[i]; + while (j < 4) { + let s = xs[j]; + let t[j] = s[i]; + let j = j + 1; + } + let i = i + 1; + } + return; + } + + method void dispose4(Array xs){ + var int i; + var Array t; + let i = 0; + while (i < 4) { + let t = xs[i]; + do t.dispose(); + let i = i + 1; + } + do xs.dispose(); + return; + } + + method Array new4(){ + var Array xs; + var int i; + let xs = Array.new(4); + let i = 0; + while (i < 4) { + let xs[i] = Array.new(4); + let i = i + 1; + } + return xs; + } + + method Array getStrips(boolean isHorizontal){ + var Array xs; + let xs = new4(); + if (isHorizontal) { + do deepcopy(grid, xs); + } else { + do transpose(grid, xs); + } + return xs; + } + + method Array align(Array xs, boolean left){ + var int i, j; + let i = 0; + let j = 0; + if (left) { + while (i < 4) { + if (xs[i] > 64) { + let xs[j] = xs[i]; + let j = j + 1; + } + let i = i + 1; + } + while (j < 4) { + let xs[j] = 32; + let j = j + 1; + } + } else { + while (i < 4) { + if (xs[3 - i] > 64) { + let xs[3 - j] = xs[3 - i]; + let j = j + 1; + } + let i = i + 1; + } + while (j < 4) { + let xs[3 - j] = 32; + let j = j + 1; + } + } + return xs; + } + + method Array reduce(Array xs, boolean left){ + if ((xs[0] = xs[1]) & (xs[2] = xs[3]) & (xs[0] > 64) & (xs[2] > 64)) { + if (left) { + let xs[0] = xs[0] + 1; + let xs[1] = xs[2] + 1; + let xs[2] = 32; + let xs[3] = 32; + } else { + let xs[3] = xs[3] + 1; + let xs[2] = xs[1] + 1; + let xs[1] = 32; + let xs[0] = 32; + } + return xs; + } + if ((xs[0] = xs[1]) & (xs[0] > 64)) { + if (left) { + let xs[0] = xs[0] + 1; + let xs[1] = xs[2]; + let xs[2] = xs[3]; + let xs[3] = 32; + } else { + let xs[1] = xs[1] + 1; + let xs[0] = 32; + } + return xs; + } + if ((xs[2] = xs[3]) & (xs[2] > 64)) { + if (left) { + let xs[2] = xs[2] + 1; + let xs[3] = 32; + } else { + let xs[3] = xs[3] + 1; + let xs[2] = xs[1]; + let xs[1] = xs[0]; + let xs[0] = 32; + } + return xs; + } + if ((xs[1] = xs[2]) & (xs[1] > 64)) { + if (left) { + let xs[1] = xs[1] + 1; + let xs[2] = xs[3]; + let xs[3] = 32; + } else { + let xs[2] = xs[2] + 1; + let xs[1] = xs[0]; + let xs[0] = 32; + } + return xs; + } + return xs; + } + + method void addTile(){ + var Array t; + var int r, c, parity, newTile; + /* + let t = grid[1]; + if (t[1] = 32) { + let t[1] = 65; + } + */ + let seed = seed * 25173 + 13849; + if (seed < 0) { + let seed = - seed; + } + + if (seed - (seed / 2 * 2) = 0) { + let parity = 1; + } else { + let parity = -1; + } + + let seed = seed - (seed / 16 * 16); + let r = seed / 4; + let c = seed - (4 * r); + let t = grid[r]; + let newTile = 65; + + while (t[c] > 64){ + let seed = seed + parity; + if (seed < 0) { + let seed = 15; + } + let seed = seed - (seed / 16 * 16); + let r = seed / 4; + let c = seed - (4 * r); + let t = grid[r]; + let newTile = 131 - newTile; + } + let t[c] = newTile; + return; + } + + method void transform(char dir){ + var boolean isHorizontal, left; + var Array xs; + var int i; + if ((dir = 0) | (dir = 1)) { + let left = true; + } else { + let left = false; + } + if ((dir = 0) | (dir = 2)) { + let isHorizontal = true; + } else { + let isHorizontal = false; + } + let xs = getStrips(isHorizontal); + let i = 0; + while (i < 4) { + let xs[i] = reduce(align(xs[i], left), left); + let i = i + 1; + } + do arrange(xs, isHorizontal); + return; + } + + method void next(char dir){ + let nTurn = nTurn + 1; + do transform(dir); + do addTile(); + return; + } + + method void draw(){ + var int r, c, i, j; + var Array t; + let r = 9; + let c = 30; + + do Output.moveCursor(r - 1, c - 1); + do Output.printString("+----+"); + do Output.moveCursor(r + 4, c - 1); + do Output.printString("+----+"); + + let i = 0; + while (i < 4) { + let j = 0; + do Output.moveCursor(r + i, c - 1); + do Output.printString("|"); + let t = grid[i]; + while (j < 4) { + do Output.printChar(t[j]); + let j = j + 1; + } + do Output.printString("|"); + let i = i + 1; + } + + do Output.moveCursor(r + 6, c - 2); + do Output.printString("Turn: "); + do Output.printInt(nTurn); + return; + } + + method void dispose() { + do Memory.deAlloc(this); + return; + } +} diff --git a/projects/11/K/Board.vm b/projects/11/K/Board.vm new file mode 100644 index 0000000..272b52d --- /dev/null +++ b/projects/11/K/Board.vm @@ -0,0 +1,1183 @@ +function Board.new 3 +push constant 3 +call Memory.alloc 1 +pop pointer 0 +push constant 4 +call Array.new 1 +pop this 0 +push constant 0 +pop local 0 +label new.While0 +push local 0 +push constant 4 +lt +not +if-goto new.EndWhile0 +push constant 0 +pop local 1 +push constant 4 +call Array.new 1 +push this 0 +push local 0 +add +pop pointer 1 +pop that 0 +push this 0 +push local 0 +add +pop pointer 1 +push that 0 +pop local 2 +label new.While0.While0 +push local 1 +push constant 4 +lt +not +if-goto new.While0.EndWhile0 +push constant 32 +push local 2 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto new.While0.While0 +label new.While0.EndWhile0 +push local 0 +push constant 1 +add +pop local 0 +goto new.While0 +label new.EndWhile0 +push this 0 +push constant 0 +add +pop pointer 1 +push that 0 +pop local 2 +push constant 65 +push local 2 +push constant 0 +add +pop pointer 1 +pop that 0 +push constant 0 +pop this 1 +push constant 0 +pop this 2 +push pointer 0 +return +function Board.arrange 0 +push argument 0 +pop pointer 0 +push argument 2 +not +if-goto arrange.Else0 +push pointer 0 +push argument 1 +push this 0 +call Board.deepcopy 3 +pop temp 0 +goto arrange.Endif0 +label arrange.Else0 +push pointer 0 +push argument 1 +push this 0 +call Board.transpose 3 +pop temp 0 +label arrange.Endif0 +push pointer 0 +push argument 1 +call Board.dispose4 2 +pop temp 0 +push constant 0 +return +function Board.copy 1 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label copy.While0 +push local 0 +push constant 4 +lt +not +if-goto copy.EndWhile0 +push argument 1 +push local 0 +add +pop pointer 1 +push that 0 +push argument 2 +push local 0 +add +pop pointer 1 +pop that 0 +push local 0 +push constant 1 +add +pop local 0 +goto copy.While0 +label copy.EndWhile0 +push constant 0 +return +function Board.deepcopy 1 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label deepcopy.While0 +push local 0 +push constant 4 +lt +not +if-goto deepcopy.EndWhile0 +push pointer 0 +push argument 1 +push local 0 +add +pop pointer 1 +push that 0 +push argument 2 +push local 0 +add +pop pointer 1 +push that 0 +call Board.copy 3 +pop temp 0 +push local 0 +push constant 1 +add +pop local 0 +goto deepcopy.While0 +label deepcopy.EndWhile0 +push constant 0 +return +function Board.transpose 4 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label transpose.While0 +push local 0 +push constant 4 +lt +not +if-goto transpose.EndWhile0 +push constant 0 +pop local 1 +push argument 2 +push local 0 +add +pop pointer 1 +push that 0 +pop local 2 +label transpose.While0.While0 +push local 1 +push constant 4 +lt +not +if-goto transpose.While0.EndWhile0 +push argument 1 +push local 1 +add +pop pointer 1 +push that 0 +pop local 3 +push local 3 +push local 0 +add +pop pointer 1 +push that 0 +push local 2 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto transpose.While0.While0 +label transpose.While0.EndWhile0 +push local 0 +push constant 1 +add +pop local 0 +goto transpose.While0 +label transpose.EndWhile0 +push constant 0 +return +function Board.dispose4 2 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +label dispose4.While0 +push local 0 +push constant 4 +lt +not +if-goto dispose4.EndWhile0 +push argument 1 +push local 0 +add +pop pointer 1 +push that 0 +pop local 1 +push local 1 +call Array.dispose 1 +pop temp 0 +push local 0 +push constant 1 +add +pop local 0 +goto dispose4.While0 +label dispose4.EndWhile0 +push argument 1 +call Array.dispose 1 +pop temp 0 +push constant 0 +return +function Board.new4 2 +push argument 0 +pop pointer 0 +push constant 4 +call Array.new 1 +pop local 0 +push constant 0 +pop local 1 +label new4.While0 +push local 1 +push constant 4 +lt +not +if-goto new4.EndWhile0 +push constant 4 +call Array.new 1 +push local 0 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto new4.While0 +label new4.EndWhile0 +push local 0 +return +function Board.getStrips 1 +push argument 0 +pop pointer 0 +push pointer 0 +call Board.new4 1 +pop local 0 +push argument 1 +not +if-goto getStrips.Else0 +push pointer 0 +push this 0 +push local 0 +call Board.deepcopy 3 +pop temp 0 +goto getStrips.Endif0 +label getStrips.Else0 +push pointer 0 +push this 0 +push local 0 +call Board.transpose 3 +pop temp 0 +label getStrips.Endif0 +push local 0 +return +function Board.align 2 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +push constant 0 +pop local 1 +push argument 2 +not +if-goto align.Else0 +label align.Else0.While0 +push local 0 +push constant 4 +lt +not +if-goto align.Else0.EndWhile0 +push argument 1 +push local 0 +add +pop pointer 1 +push that 0 +push constant 64 +gt +not +if-goto align.Else0.While0.Else0 +push argument 1 +push local 0 +add +pop pointer 1 +push that 0 +push argument 1 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +label align.Else0.While0.Else0 +push local 0 +push constant 1 +add +pop local 0 +goto align.Else0.While0 +label align.Else0.EndWhile0 +label align.Else0.While1 +push local 1 +push constant 4 +lt +not +if-goto align.Else0.EndWhile1 +push constant 32 +push argument 1 +push local 1 +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto align.Else0.While1 +label align.Else0.EndWhile1 +goto align.Endif0 +label align.Else0 +label align.If0.While0 +push local 0 +push constant 4 +lt +not +if-goto align.If0.EndWhile0 +push argument 1 +push constant 3 +push local 0 +sub +add +pop pointer 1 +push that 0 +push constant 64 +gt +not +if-goto align.If0.While0.Else0 +push argument 1 +push constant 3 +push local 0 +sub +add +pop pointer 1 +push that 0 +push argument 1 +push constant 3 +push local 1 +sub +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +label align.If0.While0.Else0 +push local 0 +push constant 1 +add +pop local 0 +goto align.If0.While0 +label align.If0.EndWhile0 +label align.If0.While1 +push local 1 +push constant 4 +lt +not +if-goto align.If0.EndWhile1 +push constant 32 +push argument 1 +push constant 3 +push local 1 +sub +add +pop pointer 1 +pop that 0 +push local 1 +push constant 1 +add +pop local 1 +goto align.If0.While1 +label align.If0.EndWhile1 +label align.Endif0 +push argument 1 +return +function Board.reduce 0 +push argument 0 +pop pointer 0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +eq +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +eq +and +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +not +if-goto reduce.Else0 +push argument 2 +not +if-goto reduce.Else0.Else0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +goto reduce.Else0.Endif0 +label reduce.Else0.Else0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +label reduce.Else0.Endif0 +push argument 1 +return +label reduce.Else0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +eq +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +not +if-goto reduce.Else1 +push argument 2 +not +if-goto reduce.Else1.Else0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +goto reduce.Else1.Endif0 +label reduce.Else1.Else0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +label reduce.Else1.Endif0 +push argument 1 +return +label reduce.Else1 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +eq +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +not +if-goto reduce.Else2 +push argument 2 +not +if-goto reduce.Else2.Else0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +goto reduce.Else2.Endif0 +label reduce.Else2.Else0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +label reduce.Else2.Endif0 +push argument 1 +return +label reduce.Else2 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +eq +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push constant 64 +gt +and +not +if-goto reduce.Else3 +push argument 2 +not +if-goto reduce.Else3.Else0 +push argument 1 +push constant 1 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 3 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 3 +add +pop pointer 1 +pop that 0 +goto reduce.Else3.Endif0 +label reduce.Else3.Else0 +push argument 1 +push constant 2 +add +pop pointer 1 +push that 0 +push constant 1 +add +push argument 1 +push constant 2 +add +pop pointer 1 +pop that 0 +push argument 1 +push constant 0 +add +pop pointer 1 +push that 0 +push argument 1 +push constant 1 +add +pop pointer 1 +pop that 0 +push constant 32 +push argument 1 +push constant 0 +add +pop pointer 1 +pop that 0 +label reduce.Else3.Endif0 +push argument 1 +return +label reduce.Else3 +push argument 1 +return +function Board.addTile 5 +push argument 0 +pop pointer 0 +push this 2 +push constant 25173 +call Math.multiply 2 +push constant 13849 +add +pop this 2 +push this 2 +push constant 0 +lt +not +if-goto addTile.Else0 +push this 2 +neg +pop this 2 +label addTile.Else0 +push this 2 +push this 2 +push constant 2 +call Math.divide 2 +push constant 2 +call Math.multiply 2 +sub +push constant 0 +eq +not +if-goto addTile.Else1 +push constant 1 +pop local 3 +goto addTile.Endif1 +label addTile.Else1 +push constant 1 +neg +pop local 3 +label addTile.Endif1 +push this 2 +push this 2 +push constant 16 +call Math.divide 2 +push constant 16 +call Math.multiply 2 +sub +pop this 2 +push this 2 +push constant 4 +call Math.divide 2 +pop local 1 +push this 2 +push constant 4 +push local 1 +call Math.multiply 2 +sub +pop local 2 +push this 0 +push local 1 +add +pop pointer 1 +push that 0 +pop local 0 +push constant 65 +pop local 4 +label addTile.While2 +push local 0 +push local 2 +add +pop pointer 1 +push that 0 +push constant 64 +gt +not +if-goto addTile.EndWhile2 +push this 2 +push local 3 +add +pop this 2 +push this 2 +push constant 0 +lt +not +if-goto addTile.While2.Else0 +push constant 15 +pop this 2 +label addTile.While2.Else0 +push this 2 +push this 2 +push constant 16 +call Math.divide 2 +push constant 16 +call Math.multiply 2 +sub +pop this 2 +push this 2 +push constant 4 +call Math.divide 2 +pop local 1 +push this 2 +push constant 4 +push local 1 +call Math.multiply 2 +sub +pop local 2 +push this 0 +push local 1 +add +pop pointer 1 +push that 0 +pop local 0 +push constant 131 +push local 4 +sub +pop local 4 +goto addTile.While2 +label addTile.EndWhile2 +push local 4 +push local 0 +push local 2 +add +pop pointer 1 +pop that 0 +push constant 0 +return +function Board.transform 4 +push argument 0 +pop pointer 0 +push argument 1 +push constant 0 +eq +push argument 1 +push constant 1 +eq +or +not +if-goto transform.Else0 +push constant 1 +neg +pop local 1 +goto transform.Endif0 +label transform.Else0 +push constant 0 +pop local 1 +label transform.Endif0 +push argument 1 +push constant 0 +eq +push argument 1 +push constant 2 +eq +or +not +if-goto transform.Else1 +push constant 1 +neg +pop local 0 +goto transform.Endif1 +label transform.Else1 +push constant 0 +pop local 0 +label transform.Endif1 +push pointer 0 +push local 0 +call Board.getStrips 2 +pop local 2 +push constant 0 +pop local 3 +label transform.While2 +push local 3 +push constant 4 +lt +not +if-goto transform.EndWhile2 +push pointer 0 +push pointer 0 +push local 2 +push local 3 +add +pop pointer 1 +push that 0 +push local 1 +call Board.align 3 +push local 1 +call Board.reduce 3 +push local 2 +push local 3 +add +pop pointer 1 +pop that 0 +push local 3 +push constant 1 +add +pop local 3 +goto transform.While2 +label transform.EndWhile2 +push pointer 0 +push local 2 +push local 0 +call Board.arrange 3 +pop temp 0 +push constant 0 +return +function Board.next 0 +push argument 0 +pop pointer 0 +push this 1 +push constant 1 +add +pop this 1 +push pointer 0 +push argument 1 +call Board.transform 2 +pop temp 0 +push pointer 0 +call Board.addTile 1 +pop temp 0 +push constant 0 +return +function Board.draw 5 +push argument 0 +pop pointer 0 +push constant 9 +pop local 0 +push constant 30 +pop local 1 +push local 0 +push constant 1 +sub +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push constant 6 +call String.new 1 +push constant 43 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 43 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +push local 0 +push constant 4 +add +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push constant 6 +call String.new 1 +push constant 43 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 43 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +push constant 0 +pop local 2 +label draw.While0 +push local 2 +push constant 4 +lt +not +if-goto draw.EndWhile0 +push constant 0 +pop local 3 +push local 0 +push local 2 +add +push local 1 +push constant 1 +sub +call Output.moveCursor 2 +pop temp 0 +push constant 1 +call String.new 1 +push constant 124 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +push this 0 +push local 2 +add +pop pointer 1 +push that 0 +pop local 4 +label draw.While0.While0 +push local 3 +push constant 4 +lt +not +if-goto draw.While0.EndWhile0 +push local 4 +push local 3 +add +pop pointer 1 +push that 0 +call Output.printChar 1 +pop temp 0 +push local 3 +push constant 1 +add +pop local 3 +goto draw.While0.While0 +label draw.While0.EndWhile0 +push constant 1 +call String.new 1 +push constant 124 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +push local 2 +push constant 1 +add +pop local 2 +goto draw.While0 +label draw.EndWhile0 +push local 0 +push constant 6 +add +push local 1 +push constant 2 +sub +call Output.moveCursor 2 +pop temp 0 +push constant 6 +call String.new 1 +push constant 84 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 58 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +push this 1 +call Output.printInt 1 +pop temp 0 +push constant 0 +return +function Board.dispose 0 +push argument 0 +pop pointer 0 +push pointer 0 +call Memory.deAlloc 1 +pop temp 0 +push constant 0 +return diff --git a/projects/11/K/K.txt b/projects/11/K/K.txt new file mode 100644 index 0000000..dc75c82 --- /dev/null +++ b/projects/11/K/K.txt @@ -0,0 +1,25 @@ +from a to k + +- a title frame showing game name and instructions. space to continue. +- a main frame showing the game running +- a endofgame frame showing game results: win or lose + +- a Board class with cell being a 4 by 4 two-d array. +- arrange strings according to an orientation: arrange(4 strings, dir)=cells + - dir: 0: align to left, 1: align to right, 2: align to top, 3:align to bottom + - e.g.: arrange({"abc", "bc", "dc", "efeh", 0}) gives the following board: + abc_ + bc__ + dc__ + efeh + - arrange({"abc", "bc", "dc", "efeh", 0}, 3) gives the following + ___e + a__f + bbde + ccch +- a new tile of 'a' or 'b' appears each turn somewhere, using some quasirandomisation e.g. (23 * n^2 + 79) mod 16. + - addtile(char, x, y) +- the board transformation: board.trans(dir) = arrange(dir) . (fmap reduce) . getstring(dir). calls getstrings, then reduce on each row / column +- board.getstrings(dir)= the strings according to direction dir. Inverse of arrange. +- reduce(string)=string: reduce("baac")=="bbc"; reduce("aabb")=="bc"; and reduce("cbb")== "cc" instead of "d". +- when one of the hjkl keys is pressed get the direction dir. diff --git a/projects/11/K/KGame.jack b/projects/11/K/KGame.jack new file mode 100644 index 0000000..796f5b5 --- /dev/null +++ b/projects/11/K/KGame.jack @@ -0,0 +1,34 @@ +class KGame{ + field Board board; + + constructor KGame new() { + let board = Board.new(); + do board.draw(); + return this; + } + + method void dispose() { + do board.dispose(); + do Memory.deAlloc(this); + return; + } + + method void run() { + var int key, dir; + var boolean exit; + let key = 0; + let exit = false; + while (~exit) { + while ((key < 130) | (key > 133)) { + let key = Keyboard.keyPressed(); + } + let dir = key - 130; + while (~(key = 0)) { + let key = Keyboard.keyPressed(); + } + do board.next(dir); + do board.draw(); + } + return; + } +} diff --git a/projects/11/K/KGame.vm b/projects/11/K/KGame.vm new file mode 100644 index 0000000..eef2df0 --- /dev/null +++ b/projects/11/K/KGame.vm @@ -0,0 +1,74 @@ +function KGame.new 0 +push constant 1 +call Memory.alloc 1 +pop pointer 0 +call Board.new 0 +pop this 0 +push this 0 +call Board.draw 1 +pop temp 0 +push pointer 0 +return +function KGame.dispose 0 +push argument 0 +pop pointer 0 +push this 0 +call Board.dispose 1 +pop temp 0 +push pointer 0 +call Memory.deAlloc 1 +pop temp 0 +push constant 0 +return +function KGame.run 3 +push argument 0 +pop pointer 0 +push constant 0 +pop local 0 +push constant 0 +pop local 2 +label run.While0 +push local 2 +not +not +if-goto run.EndWhile0 +label run.While0.While0 +push local 0 +push constant 130 +lt +push local 0 +push constant 133 +gt +or +not +if-goto run.While0.EndWhile0 +call Keyboard.keyPressed 0 +pop local 0 +goto run.While0.While0 +label run.While0.EndWhile0 +push local 0 +push constant 130 +sub +pop local 1 +label run.While0.While1 +push local 0 +push constant 0 +eq +not +not +if-goto run.While0.EndWhile1 +call Keyboard.keyPressed 0 +pop local 0 +goto run.While0.While1 +label run.While0.EndWhile1 +push this 0 +push local 1 +call Board.next 2 +pop temp 0 +push this 0 +call Board.draw 1 +pop temp 0 +goto run.While0 +label run.EndWhile0 +push constant 0 +return diff --git a/projects/11/K/Main.jack b/projects/11/K/Main.jack new file mode 100644 index 0000000..4136e80 --- /dev/null +++ b/projects/11/K/Main.jack @@ -0,0 +1,9 @@ +class Main { + function void main() { + var KGame game; + let game = KGame.new(); + do game.run(); + do game.dispose(); + return; + } +} diff --git a/projects/11/K/Main.vm b/projects/11/K/Main.vm new file mode 100644 index 0000000..0a56ae9 --- /dev/null +++ b/projects/11/K/Main.vm @@ -0,0 +1,11 @@ +function Main.main 1 +call KGame.new 0 +pop local 0 +push local 0 +call KGame.run 1 +pop temp 0 +push local 0 +call KGame.dispose 1 +pop temp 0 +push constant 0 +return diff --git a/projects/12/Array.jack b/projects/12/Array.jack index c712b69..343c25c 100644 --- a/projects/12/Array.jack +++ b/projects/12/Array.jack @@ -20,6 +20,7 @@ class Array { /** Disposes this array. */ method void dispose() { - return Memory.deAlloc(this); + do Memory.deAlloc(this); + return; } } diff --git a/projects/12/Keyboard.jack b/projects/12/Keyboard.jack index ceb3438..a35704a 100644 --- a/projects/12/Keyboard.jack +++ b/projects/12/Keyboard.jack @@ -46,13 +46,15 @@ class Keyboard { var int key, key1; let key = 0; while (key = 0) { - key = Memory.peek(24576); + let key = Memory.peek(24576); } - key1 = key; + let key1 = key; while (key1 = key) { - key1 = Memory.peek(24576); + let key1 = Memory.peek(24576); + } + if ((key > 31) & (key < 127) | (key = 128) | (key = 129)) { + do Output.printChar(key); } - Output.printChar(key); return key; } @@ -64,8 +66,8 @@ class Keyboard { function String readLine(String message) { var int c; var String s; - let s = ""; - Output.printString(message); + let s = String.new(140); + do Output.printString(message); let c = Keyboard.readChar(); while (~(c = 128)) { if (c = 129) { @@ -87,35 +89,9 @@ class Keyboard { * entered text is detected). Also handles user backspaces. */ function int readInt(String message) { - var int n, i; - var char t; var String s; - let n = 0; - let neg = false; + do Output.printString(message); let s = Keyboard.readLine(""); - let i = 0; - if (s.charAt(0) = 45) { - let i = 1; - let neg = true; - } - let t = s.charAt(i); - if ((t < 48) | (t > 57)) { - do Sys.error(3); - Output.printString("Keyboard.readInt: the input data is not number!"); - } - let done = false; - while (~done) { - if ((s.charAt(i) > 47) & (s.charAt(i) < 58)) { - n = n * 10 + (t - 48); - } else { - done = true; - } - let i = i + 1; - done = done | (i < s.length()); - } - if (neg) { - n = - n; - } - return n; + return s.intValue(); } } diff --git a/projects/12/Math.jack b/projects/12/Math.jack index 61e09fc..01bce8f 100644 --- a/projects/12/Math.jack +++ b/projects/12/Math.jack @@ -28,6 +28,18 @@ class Math { return ~(x & twoToThe[i] = 0); } + function int sign(int x) { + if (x > 0) { + return 1; + } else { + if (x < 0) { + return -1; + } else { + return 0; + } + } + } + /** Returns the absolute value of x. */ function int abs(int x) { if (x > 0){ @@ -43,11 +55,12 @@ class Math { * the Jack expressions x*y and multiply(x,y) return the same value. */ function int multiply(int x, int y) { - var int z, res, shiftedX, i; + var int res, shiftedX, i; let shiftedX = x; let i = 0; let res = 0; - while ((twoToThe[i] > x) & (i < 16)) { + //while ((~(twoToThe[i] > y)) & (i < 16)) { + while (i < 16) { if (Math.bit(y, i)){ let res = res + shiftedX; } @@ -117,7 +130,7 @@ class Math { while (~(k < 0)) { let z = y + twoToThe[k]; let w = z * z; - if ((w < x) & (w > 0)) { + if ((~(w > x)) & (w > 0)) { let y = z; } let k = k - 1; diff --git a/projects/12/Memory.jack b/projects/12/Memory.jack index b1e67ef..54e04aa 100644 --- a/projects/12/Memory.jack +++ b/projects/12/Memory.jack @@ -11,7 +11,7 @@ class Memory { static Array memory; static Array heap; - static int first; + static int last; /** Initializes the class. */ function void init() { @@ -19,6 +19,8 @@ class Memory { let heap = 2048; let heap[0] = -1; let heap[1] = 14334; + let last = 0; + return; } /** Returns the RAM value at the given address. */ @@ -29,6 +31,7 @@ class Memory { /** Sets the RAM value at the given address to the given value. */ function void poke(int address, int value) { let memory[address] = value; + return; } /** Finds an available RAM block of the given size and returns @@ -43,8 +46,9 @@ class Memory { let current = heap[current]; } if (current = -1) { - String.println("Insufficient space for allocation!"); - Sys.error(1); + do String.println("Insufficient space for allocation!"); + do Sys.error(1); + return -1; } else { let heap[current + 1] = heap[current + 1] - size2; let newLast = current + heap[current + 1] + 2; @@ -58,23 +62,11 @@ class Memory { /** De-allocates the given object (cast as an array) by making * it available for future allocations. */ function void deAlloc(Array o) { - var int base; - var int current; - var int prev; - let prev = 0; - let current = heap[prev]; + var int oBase; let oBase = o - heap - 2; - while ((current > -1) & (~(current = oBase))){ - let prev = current; - let current = heap[current]; - } - if (current = -1) { - String.println("The impossible happened"); - Sys.error(1); - } else { - let heap[prev] = heap[current] - let heap[prev + 1] = heap[prev] - prev - 2; - return; - } + let heap[oBase] = -1; + let heap[last] = oBase; + let last = oBase; + return; } } diff --git a/projects/12/Output.jack b/projects/12/Output.jack index a0cb284..61b24de 100644 --- a/projects/12/Output.jack +++ b/projects/12/Output.jack @@ -22,6 +22,7 @@ class Output { /** Initializes the screen, and locates the cursor at the screen's top-left. */ function void init() { + do Output.initMap(); do Output.moveCursor(0, 0); return; } @@ -74,7 +75,7 @@ class Output { do Output.create(64,30,51,51,59,59,59,27,3,30,0,0); // @ do Output.create(63,30,51,51,24,12,12,0,12,12,0,0); // ? - do Output.create(65,12,12,30,30,51,51,63,51,51,0,0); // A ** TO BE FILLED ** + do Output.create(65,12,12,30,30,51,51,63,51,51,0,0); // A do Output.create(66,31,51,51,51,31,51,51,51,31,0,0); // B do Output.create(67,28,54,35,3,3,3,35,54,28,0,0); // C do Output.create(68,15,27,51,51,51,51,51,27,15,0,0); // D @@ -146,9 +147,9 @@ class Output { // Creates the character map array of the given character index, using the given values. function void create(int index, int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k) { - var Array map; + var Array map; - let map = Array.new(11); + let map = Array.new(11); let charMaps[index] = map; let map[0] = a; @@ -180,8 +181,8 @@ class Output { * and erases the character displayed there. */ function void moveCursor(int i, int j) { var int x, y; - let x = cursorI * 8; - let y = cursorJ * 11; + let x = cursorJ * 8; + let y = cursorI * 11; do Screen.setColor(false); do Screen.drawRectangle(x, y, x + 7, y + 10); let x = j * 8; @@ -198,19 +199,32 @@ class Output { function void printChar(char c) { var int k, x, y, addr; var Array cm; - if (c = 8) { //backspace + if (c = 129) { do Output.backSpace(); return; - } - if (c = 10) { //newline + } + if (cursorI = 22) { + if (c = 128) { + return; + } + if (cursorJ = 63) { + return; + } + } + if (c = 128) { do Output.println(); return; } let k = 0; - let x = cursorI / 2; - let y = cursorI - (x * 2); // y = 0: lsb, y = 1: msb - let addr = cursorJ * 352 + x + 16384; + let x = cursorJ / 2; + let y = cursorJ - (x * 2); // y = 0: lsb, y = 1: msb + let addr = cursorI * 352 + x + 16384; let cm = Output.getMap(c); + if (cursorJ = 63) { + do Output.moveCursor(cursorI + 1, 0); + } else { + do Output.moveCursor(cursorI, cursorJ + 1); + } while (k < 11) { if (y = 0){ do Memory.poke(addr, Memory.peek(addr) & (-128) + cm[k]); @@ -220,11 +234,6 @@ class Output { let k = k + 1; let addr = addr + 32; } - if (cursorJ = 63) { - do Output.moveCursor(cursorI + 1, 0); - } else { - do Output.moveCursor(cursorI, cursorJ + 1); - } return; } @@ -245,43 +254,27 @@ class Output { /** Displays the given integer starting at the cursor location, * and advances the cursor appropriately. */ function void printInt(int i) { - var int n, k; var String s; - if (i = 0) { - do Output.printChar(48); - return; - } - let n = i; - if (i < 0) { - do Output.printChar(45); - let n = -n; - } - let s = ""; - while (n > 0) { - let k = n / 10; - do s.appendChar(48 + n - (k * 10)); - let n = k; - } - let k = s.length(); - while (k > 0) { - do Output.printChar(s.charAt(k)); - let k = k - 1; - } + let s = String.new(6); + do s.setInt(i); + do Output.printString(s); return; } /** Advances the cursor to the beginning of the next line. */ function void println() { - do Output.moveCursor(cursorI, 0); + if (cursorI < 22) { + do Output.moveCursor(cursorI + 1, 0); + } return; } /** Moves the cursor one column back. */ function void backSpace() { if (cursorJ > 0) { - do Output.moveCursor(cursorI - 1, 63); - } else { do Output.moveCursor(cursorI, cursorJ - 1); + } else { + do Output.moveCursor(cursorI - 1, 63); } return; } diff --git a/projects/12/Screen.jack b/projects/12/Screen.jack index 337a7e2..a370ab9 100644 --- a/projects/12/Screen.jack +++ b/projects/12/Screen.jack @@ -26,6 +26,7 @@ class Screen { let i = i + 1; } let screen = 16384; + let color = true; return; } @@ -34,7 +35,7 @@ class Screen { var boolean c; let c = color; let color = false; - do drawRectangle(0, 0, 511, 255); + do Screen.drawRectangle(0, 0, 511, 255); let color = c; return; } @@ -59,17 +60,17 @@ class Screen { if (color) { let screen[addr] = screen[addr] | twoToThe[x - (t * 16)]; } else { - let screen[addr] = screen[addr] & (- (twoToThe[x - (t * 16)] + 1)) + let screen[addr] = screen[addr] & (- (twoToThe[x - (t * 16)] + 1)); } return; } /** Draws a line from pixel (x1,y1) to pixel (x2,y2), using the current color. */ function void drawLine(int x1, int y1, int x2, int y2) { - var int dx, dy, x, y, diff; + var int dx, dy, x, y, a, b, diff; let x = x1; let y = y1; - do drawPixel(x, y); + do Screen.drawPixel(x, y); if (x1 = x2) { if (y1 = y2) { return; @@ -97,19 +98,22 @@ class Screen { let y = y + b; let diff = diff - dx; } - do drawPixel(x, y); + do Screen.drawPixel(x, y); } + return; } /** Draws a filled rectangle whose top left corner is (x1, y1) * and bottom right corner is (x2,y2), using the current color. */ function void drawRectangle(int x1, int y1, int x2, int y2) { + var int x; if ((x1 > x2) | (y1 > y2)) { return; } let x = x1; while (~(x > x2)) { - do drawLine(x, y1, x, y2); + do Screen.drawLine(x, y1, x, y2); + let x = x + 1; } return; } @@ -118,15 +122,15 @@ class Screen { function void drawCircle(int x, int y, int r) { var int dx, dy, r2; if (r > 181) { - String.println("drawCircle: radius too big!"); - Sys.error(2); + do String.println("drawCircle: radius too big!"); + do Sys.error(2); return; } let dy = -r; let r2 = r * r; while (~(dy > r)) { let dx = Math.sqrt(r2 - (dy * dy)); - do drawLine(x - dx, y + dy, x + dx, y + dy); + do Screen.drawLine(x - dx, y + dy, x + dx, y + dy); let dy = dy + 1; } return; diff --git a/projects/12/String.jack b/projects/12/String.jack index 398fdfe..3ef40b2 100644 --- a/projects/12/String.jack +++ b/projects/12/String.jack @@ -18,7 +18,9 @@ class String { /** constructs a new empty string with a maximum length of maxLength * and initial length of 0. */ constructor String new(int maxLength) { - let s = new Array(maxLength); + if (maxLength > 0) { + let s = Array.new(maxLength); + } let maxLen = maxLength; let len = 0; return this; @@ -26,7 +28,9 @@ class String { /** Disposes this string. */ method void dispose() { - do s.dispose(); + if (maxLen > 0) { + do s.dispose(); + } do Memory.deAlloc(this); return; } @@ -39,8 +43,8 @@ class String { /** Returns the character at the j-th location of this string. */ method char charAt(int j) { if ((j < 0) | (j + 1 > len)){ - Output.printString("String.charAt: index out of range!"); - Sys.error(5); + do Output.printString("String.charAt: index out of range!"); + do Sys.error(5); } return s[j]; } @@ -48,8 +52,8 @@ class String { /** Sets the character at the j-th location of this string to c. */ method void setCharAt(int j, char c) { if ((j < 0) | (j + 1 > len)){ - Output.printString("String.setCharAt: index out of range!"); - Sys.error(5); + do Output.printString("String.setCharAt: index out of range!"); + do Sys.error(5); } let s[j] = c; return; @@ -58,8 +62,8 @@ class String { /** Appends c to this string's end and returns this string. */ method String appendChar(char c) { if (len = maxLen) { - Output.printString("String.appendChar: reached max length!"); - Sys.error(5); + do Output.printString("String.appendChar: reached max length!"); + do Sys.error(5); } let s[len] = c; let len = len + 1; @@ -69,8 +73,8 @@ class String { /** Erases the last character from this string. */ method void eraseLastChar() { if (len = 0){ - Output.printString("String.eraseLastChar: string is already empty!"); - Sys.error(5); + do Output.printString("String.eraseLastChar: string is already empty!"); + do Sys.error(5); } let len = len - 1; return; @@ -91,15 +95,15 @@ class String { let neg = false; } let c = s[i]; - if ((t < 48) | (t > 57)) { + if ((c < 48) | (c > 57)) { do Sys.error(3); do Output.printString("String.intValue: the input data is not number!"); } let done = false; while ((~done) & (i < len)) { - let t = s[i]; - if ((t > 47) & (t < 58)) { - let n = n * 10 + (t - 48); + let c = s[i]; + if ((c > 47) & (c < 58)) { + let n = n * 10 + (c - 48); } else { let done = true; } @@ -115,14 +119,14 @@ class String { /** Sets this string to hold a representation of the given value. */ method void setInt(int val) { //change Output.printInt after this - var int l; + var int x, i, y; var boolean neg; if (val < 0) { let neg = true; - len = 2; + let len = 2; } else { let neg = false; - len = 1; + let len = 1; } let x = Math.abs(val); if (x > 9999) { @@ -135,21 +139,22 @@ class String { let len = len + 1; }}}} if (len > maxLen) { - Output.printString("String.setInt: val is too big for the string!"); - Sys.error(5); + do Output.printString("String.setInt: val is too big for the string!"); + do Sys.error(5); } if (x = 0) { - setCharAt(0, 48); + do setCharAt(0, 48); return; } if (neg) { - setCharAt(0, 45); + do setCharAt(0, 45); } let i = len - 1; while (x > 0) { let y = x / 10; - setCharAt(i, x - (y * 10) + 48); + do setCharAt(i, x - (y * 10) + 48); let x = y; + let i = i - 1; } return; } diff --git a/projects/12/Sys.jack b/projects/12/Sys.jack index d6b99f6..53c078c 100644 --- a/projects/12/Sys.jack +++ b/projects/12/Sys.jack @@ -10,14 +10,14 @@ class Sys { /** Performs all the initializations required by the OS. */ function void init() { - do Array.init(); do Keyboard.init(); do Math.init(); do Memory.init(); do Output.init(); do Screen.init(); - do String.init(); do Main.main(); + do Sys.halt(); + return; } /** Halts the program execution. */ @@ -29,18 +29,23 @@ class Sys { /** Waits approximately duration milliseconds and returns. */ function void wait(int duration) { - Output.printString("waiting") + var int i, j; let i = 0; - while (i < 50000){ + while (i < duration){ let i = i + 1; + let j = 0; + while (j < 318){ + let j = j + 1; + } } - Output.printString("done") + return; } /** Displays the given error code in the form "ERR", * and halts the program's execution. */ function void error(int errorCode) { - Output.printString("ERR"); - Output.printInt(errorCode); + do Output.printString("ERR"); + do Output.printInt(errorCode); + return; } } -- cgit v1.2.3