From c3598a26ab4ba0562ede74b37dc2a7aa6907640a Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Wed, 17 Jan 2018 16:06:50 +0100 Subject: finished Keyboard --- projects/12/Keyboard.jack | 59 ++ projects/12/KeyboardTest/Keyboard.jack | 125 +++++ projects/12/KeyboardTest/Keyboard.vm | 310 +++++++++++ projects/12/KeyboardTest/Main.vm | 949 +++++++++++++++++++++++++++++++++ projects/12/OutputTest/Output.jack | 11 +- projects/12/OutputTest/Output.vm | 15 +- 6 files changed, 1462 insertions(+), 7 deletions(-) create mode 100644 projects/12/KeyboardTest/Keyboard.jack create mode 100644 projects/12/KeyboardTest/Keyboard.vm create mode 100644 projects/12/KeyboardTest/Main.vm diff --git a/projects/12/Keyboard.jack b/projects/12/Keyboard.jack index 03e1031..ceb3438 100644 --- a/projects/12/Keyboard.jack +++ b/projects/12/Keyboard.jack @@ -10,6 +10,7 @@ class Keyboard { /** Initializes the keyboard. */ function void init() { + return; } /** @@ -33,6 +34,7 @@ class Keyboard { * F1 - F12 = 141 - 152 */ function char keyPressed() { + return Memory.peek(24576); } /** @@ -41,6 +43,17 @@ class Keyboard { * of the pressed key. */ function char readChar() { + var int key, key1; + let key = 0; + while (key = 0) { + key = Memory.peek(24576); + } + key1 = key; + while (key1 = key) { + key1 = Memory.peek(24576); + } + Output.printChar(key); + return key; } /** @@ -49,6 +62,22 @@ class Keyboard { * and returns its value. Also handles user backspaces. */ function String readLine(String message) { + var int c; + var String s; + let s = ""; + Output.printString(message); + let c = Keyboard.readChar(); + while (~(c = 128)) { + if (c = 129) { + if (s.length() > 0) { + do s.eraseLastChar(); + } + } else { + do s.appendChar(c); + } + let c = Keyboard.readChar(); + } + return s; } /** @@ -58,5 +87,35 @@ 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; + 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; } } diff --git a/projects/12/KeyboardTest/Keyboard.jack b/projects/12/KeyboardTest/Keyboard.jack new file mode 100644 index 0000000..2aba72e --- /dev/null +++ b/projects/12/KeyboardTest/Keyboard.jack @@ -0,0 +1,125 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/12/Keyboard.jack + +/** + * A library for handling user input from the keyboard. + */ +class Keyboard { + + /** Initializes the keyboard. */ + function void init() { + return; + } + + /** + * Returns the character of the currently pressed key on the keyboard; + * if no key is currently pressed, returns 0. + * + * Recognizes all ASCII characters, as well as the following keys: + * new line = 128 = String.newline() + * backspace = 129 = String.backspace() + * left arrow = 130 + * up arrow = 131 + * right arrow = 132 + * down arrow = 133 + * home = 134 + * End = 135 + * page up = 136 + * page down = 137 + * insert = 138 + * delete = 139 + * ESC = 140 + * F1 - F12 = 141 - 152 + */ + function char keyPressed() { + return Memory.peek(24576); + } + + /** + * Waits until a key is pressed on the keyboard and released, + * then echoes the key to the screen, and returns the character + * of the pressed key. + */ + function char readChar() { + var int key, key1; + let key = 0; + while (key = 0) { + let key = Memory.peek(24576); + } + let key1 = key; + while (key1 = key) { + let key1 = Memory.peek(24576); + } + if ((key > 31) & (key < 127) | (key = 128) | (key = 129)) { + do Output.printChar(key); + } + return key; + } + + /** + * Displays the message on the screen, reads from the keyboard the entered + * text until a newline character is detected, echoes the text to the screen, + * and returns its value. Also handles user backspaces. + */ + function String readLine(String message) { + var int c; + var String s; + let s = String.new(140); + do Output.printString(message); + let c = Keyboard.readChar(); + while (~(c = 128)) { + if (c = 129) { + if (s.length() > 0) { + do s.eraseLastChar(); + } + } else { + do s.appendChar(c); + } + let c = Keyboard.readChar(); + } + return s; + } + + /** + * Displays the message on the screen, reads from the keyboard the entered + * text until a newline character is detected, echoes the text to the screen, + * and returns its integer value (until the first non-digit character in the + * entered text is detected). Also handles user backspaces. + */ + function int readInt(String message) { + var int n, i; + var char t; + var String s; + var boolean neg, done; + do Output.printString(message); + let n = 0; + let neg = false; + 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); + do Output.printString("Keyboard.readInt: the input data is not number!"); + } + let done = false; + while (~done) { + if ((s.charAt(i) > 47) & (s.charAt(i) < 58)) { + let n = n * 10 + (s.charAt(i) - 48); + } else { + let done = true; + } + let i = i + 1; + let done = done | (i + 1 > s.length()); + } + if (neg) { + let n = - n; + } + return n; + } +} diff --git a/projects/12/KeyboardTest/Keyboard.vm b/projects/12/KeyboardTest/Keyboard.vm new file mode 100644 index 0000000..bebd916 --- /dev/null +++ b/projects/12/KeyboardTest/Keyboard.vm @@ -0,0 +1,310 @@ +function Keyboard.init 0 +push constant 0 +return +function Keyboard.keyPressed 0 +push constant 24576 +call Memory.peek 1 +return +function Keyboard.readChar 2 +push constant 0 +pop local 0 +label WHILE_EXP0 +push local 0 +push constant 0 +eq +not +if-goto WHILE_END0 +push constant 24576 +call Memory.peek 1 +pop local 0 +goto WHILE_EXP0 +label WHILE_END0 +push local 0 +pop local 1 +label WHILE_EXP1 +push local 1 +push local 0 +eq +not +if-goto WHILE_END1 +push constant 24576 +call Memory.peek 1 +pop local 1 +goto WHILE_EXP1 +label WHILE_END1 +push local 0 +push constant 31 +gt +push local 0 +push constant 127 +lt +and +push local 0 +push constant 128 +eq +or +push local 0 +push constant 129 +eq +or +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push local 0 +call Output.printChar 1 +pop temp 0 +label IF_FALSE0 +push local 0 +return +function Keyboard.readLine 2 +push constant 140 +call String.new 1 +pop local 1 +push argument 0 +call Output.printString 1 +pop temp 0 +call Keyboard.readChar 0 +pop local 0 +label WHILE_EXP0 +push local 0 +push constant 128 +eq +not +not +if-goto WHILE_END0 +push local 0 +push constant 129 +eq +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push local 1 +call String.length 1 +push constant 0 +gt +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 +push local 1 +call String.eraseLastChar 1 +pop temp 0 +label IF_FALSE1 +goto IF_END0 +label IF_FALSE0 +push local 1 +push local 0 +call String.appendChar 2 +pop temp 0 +label IF_END0 +call Keyboard.readChar 0 +pop local 0 +goto WHILE_EXP0 +label WHILE_END0 +push local 1 +return +function Keyboard.readInt 6 +push argument 0 +call Output.printString 1 +pop temp 0 +push constant 0 +pop local 0 +push constant 0 +pop local 4 +push constant 0 +call String.new 1 +call Keyboard.readLine 1 +pop local 3 +push constant 0 +pop local 1 +push local 3 +push constant 0 +call String.charAt 2 +push constant 45 +eq +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push constant 1 +pop local 1 +push constant 0 +not +pop local 4 +label IF_FALSE0 +push local 3 +push local 1 +call String.charAt 2 +pop local 2 +push local 2 +push constant 48 +lt +push local 2 +push constant 57 +gt +or +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 +push constant 3 +call Sys.error 1 +pop temp 0 +push constant 47 +call String.new 1 +push constant 75 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +push constant 98 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 46 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 73 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 58 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 105 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 105 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 109 +call String.appendChar 2 +push constant 98 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 33 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +label IF_FALSE1 +push constant 0 +pop local 5 +label WHILE_EXP0 +push local 5 +not +not +if-goto WHILE_END0 +push local 3 +push local 1 +call String.charAt 2 +push constant 47 +gt +push local 3 +push local 1 +call String.charAt 2 +push constant 58 +lt +and +if-goto IF_TRUE2 +goto IF_FALSE2 +label IF_TRUE2 +push local 0 +push constant 10 +call Math.multiply 2 +push local 3 +push local 1 +call String.charAt 2 +push constant 48 +sub +add +pop local 0 +goto IF_END2 +label IF_FALSE2 +push constant 0 +not +pop local 5 +label IF_END2 +push local 1 +push constant 1 +add +pop local 1 +push local 5 +push local 1 +push constant 1 +add +push local 3 +call String.length 1 +gt +or +pop local 5 +goto WHILE_EXP0 +label WHILE_END0 +push local 4 +if-goto IF_TRUE3 +goto IF_FALSE3 +label IF_TRUE3 +push local 0 +neg +pop local 0 +label IF_FALSE3 +push local 0 +return diff --git a/projects/12/KeyboardTest/Main.vm b/projects/12/KeyboardTest/Main.vm new file mode 100644 index 0000000..aec89d6 --- /dev/null +++ b/projects/12/KeyboardTest/Main.vm @@ -0,0 +1,949 @@ +function Main.main 5 +push constant 0 +pop local 4 +push constant 16 +call String.new 1 +push constant 107 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +push constant 80 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 58 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +label WHILE_EXP0 +push local 4 +not +not +if-goto WHILE_END0 +push constant 32 +call String.new 1 +push constant 80 +call String.appendChar 2 +push constant 108 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 80 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 103 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 68 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 119 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 107 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +label WHILE_EXP1 +push local 1 +push constant 0 +eq +not +if-goto WHILE_END1 +call Keyboard.keyPressed 0 +pop local 1 +goto WHILE_EXP1 +label WHILE_END1 +push local 1 +pop local 0 +label WHILE_EXP2 +push local 1 +push constant 0 +eq +not +not +if-goto WHILE_END2 +call Keyboard.keyPressed 0 +pop local 1 +goto WHILE_EXP2 +label WHILE_END2 +call Output.println 0 +pop temp 0 +push local 0 +push constant 137 +eq +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push constant 2 +call String.new 1 +push constant 111 +call String.appendChar 2 +push constant 107 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +push constant 0 +not +pop local 4 +label IF_FALSE0 +goto WHILE_EXP0 +label WHILE_END0 +push constant 0 +pop local 4 +push constant 14 +call String.new 1 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 67 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 58 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +push constant 59 +call String.new 1 +push constant 40 +call String.appendChar 2 +push constant 86 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 105 +call String.appendChar 2 +push constant 102 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 105 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 41 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +label WHILE_EXP3 +push local 4 +not +not +if-goto WHILE_END3 +push constant 29 +call String.new 1 +push constant 80 +call String.appendChar 2 +push constant 108 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 109 +call String.appendChar 2 +push constant 98 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 51 +call String.appendChar 2 +push constant 39 +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 +call Keyboard.readChar 0 +pop local 0 +call Output.println 0 +pop temp 0 +push local 0 +push constant 51 +eq +if-goto IF_TRUE1 +goto IF_FALSE1 +label IF_TRUE1 +push constant 2 +call String.new 1 +push constant 111 +call String.appendChar 2 +push constant 107 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +push constant 0 +not +pop local 4 +label IF_FALSE1 +goto WHILE_EXP3 +label WHILE_END3 +push constant 0 +pop local 4 +push constant 14 +call String.new 1 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 76 +call String.appendChar 2 +push constant 105 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 58 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +push constant 38 +call String.new 1 +push constant 40 +call String.appendChar 2 +push constant 86 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 105 +call String.appendChar 2 +push constant 102 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 103 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 102 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 98 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 107 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 41 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +label WHILE_EXP4 +push local 4 +not +not +if-goto WHILE_END4 +push constant 36 +call String.new 1 +push constant 80 +call String.appendChar 2 +push constant 108 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 74 +call String.appendChar 2 +push constant 65 +call String.appendChar 2 +push constant 67 +call String.appendChar 2 +push constant 75 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 58 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +call Keyboard.readLine 1 +pop local 2 +push local 2 +call String.length 1 +push constant 4 +eq +if-goto IF_TRUE2 +goto IF_FALSE2 +label IF_TRUE2 +push local 2 +push constant 0 +call String.charAt 2 +push constant 74 +eq +push local 2 +push constant 1 +call String.charAt 2 +push constant 65 +eq +and +push local 2 +push constant 2 +call String.charAt 2 +push constant 67 +eq +and +push local 2 +push constant 3 +call String.charAt 2 +push constant 75 +eq +and +if-goto IF_TRUE3 +goto IF_FALSE3 +label IF_TRUE3 +push constant 2 +call String.new 1 +push constant 111 +call String.appendChar 2 +push constant 107 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +push constant 0 +not +pop local 4 +label IF_FALSE3 +label IF_FALSE2 +goto WHILE_EXP4 +label WHILE_END4 +push constant 0 +pop local 4 +push constant 13 +call String.new 1 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 73 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 58 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +push constant 38 +call String.new 1 +push constant 40 +call String.appendChar 2 +push constant 86 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 105 +call String.appendChar 2 +push constant 102 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 104 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 103 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 102 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 98 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 107 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 41 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +label WHILE_EXP5 +push local 4 +not +not +if-goto WHILE_END5 +push constant 38 +call String.new 1 +push constant 80 +call String.appendChar 2 +push constant 108 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 45 +call String.appendChar 2 +push constant 51 +call String.appendChar 2 +push constant 50 +call String.appendChar 2 +push constant 49 +call String.appendChar 2 +push constant 50 +call String.appendChar 2 +push constant 51 +call String.appendChar 2 +push constant 39 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 97 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 110 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 114 +call String.appendChar 2 +push constant 58 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +call Keyboard.readInt 1 +pop local 3 +push local 3 +push constant 32123 +neg +eq +if-goto IF_TRUE4 +goto IF_FALSE4 +label IF_TRUE4 +push constant 2 +call String.new 1 +push constant 111 +call String.appendChar 2 +push constant 107 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +call Output.println 0 +pop temp 0 +push constant 0 +not +pop local 4 +label IF_FALSE4 +goto WHILE_EXP5 +label WHILE_END5 +call Output.println 0 +pop temp 0 +push constant 27 +call String.new 1 +push constant 84 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 111 +call String.appendChar 2 +push constant 109 +call String.appendChar 2 +push constant 112 +call String.appendChar 2 +push constant 108 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 116 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 100 +call String.appendChar 2 +push constant 32 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 99 +call String.appendChar 2 +push constant 101 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 115 +call String.appendChar 2 +push constant 102 +call String.appendChar 2 +push constant 117 +call String.appendChar 2 +push constant 108 +call String.appendChar 2 +push constant 108 +call String.appendChar 2 +push constant 121 +call String.appendChar 2 +call Output.printString 1 +pop temp 0 +push constant 0 +return diff --git a/projects/12/OutputTest/Output.jack b/projects/12/OutputTest/Output.jack index 369c0d3..05d59de 100644 --- a/projects/12/OutputTest/Output.jack +++ b/projects/12/OutputTest/Output.jack @@ -199,20 +199,21 @@ class Output { function void printChar(char c) { var int k, x, y, addr; var Array cm; - if (c = 8) { + if (c = 129) { do Output.backSpace(); return; } if (cursorI = 22) { - if (c = 10) { + if (c = 128) { return; } if (cursorJ = 63) { return; } } - if (c = 10) { + if (c = 128) { do Output.println(); + return; } let k = 0; let x = cursorJ / 2; @@ -282,7 +283,9 @@ class Output { /** Advances the cursor to the beginning of the next line. */ function void println() { - do Output.moveCursor(cursorI + 1, 0); + if (cursorI < 22) { + do Output.moveCursor(cursorI + 1, 0); + } return; } diff --git a/projects/12/OutputTest/Output.vm b/projects/12/OutputTest/Output.vm index 8d6e74c..e291708 100644 --- a/projects/12/OutputTest/Output.vm +++ b/projects/12/OutputTest/Output.vm @@ -1531,7 +1531,7 @@ push constant 0 return function Output.printChar 5 push argument 0 -push constant 8 +push constant 129 eq if-goto IF_TRUE0 goto IF_FALSE0 @@ -1548,7 +1548,7 @@ if-goto IF_TRUE1 goto IF_FALSE1 label IF_TRUE1 push argument 0 -push constant 10 +push constant 128 eq if-goto IF_TRUE2 goto IF_FALSE2 @@ -1567,13 +1567,15 @@ return label IF_FALSE3 label IF_FALSE1 push argument 0 -push constant 10 +push constant 128 eq if-goto IF_TRUE4 goto IF_FALSE4 label IF_TRUE4 call Output.println 0 pop temp 0 +push constant 0 +return label IF_FALSE4 push constant 0 pop local 0 @@ -1788,11 +1790,18 @@ push constant 0 return function Output.println 0 push static 1 +push constant 22 +lt +if-goto IF_TRUE0 +goto IF_FALSE0 +label IF_TRUE0 +push static 1 push constant 1 add push constant 0 call Output.moveCursor 2 pop temp 0 +label IF_FALSE0 push constant 0 return function Output.backSpace 0 -- cgit v1.2.3