From 01da7177bd2f4d83e8916540a69d8b2690b52bbb Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Wed, 17 Jan 2018 18:35:55 +0100 Subject: finished project 12 - revised output - finished string --- projects/12/OutputTest/Output.jack | 28 ++----------- projects/12/OutputTest/Output.vm | 85 +++----------------------------------- 2 files changed, 10 insertions(+), 103 deletions(-) (limited to 'projects/12/OutputTest') diff --git a/projects/12/OutputTest/Output.jack b/projects/12/OutputTest/Output.jack index 05d59de..61b24de 100644 --- a/projects/12/OutputTest/Output.jack +++ b/projects/12/OutputTest/Output.jack @@ -254,30 +254,10 @@ 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, j; - var Array s; - if (i = 0) { - do Output.printChar(48); - return; - } - let n = i; - if (i < 0) { - do Output.printChar(45); - let n = -n; - } - let s = Array.new(5); - let j = 0; - while (n > 0) { - let k = n / 10; - let s[j] = 48 + n - (k * 10); - let n = k; - let j = j + 1; - } - while (j > 0) { - let j = j - 1; - do Output.printChar(s[j]); - } - //do Output.printString(s); + var String s; + let s = String.new(6); + do s.setInt(i); + do Output.printString(s); return; } diff --git a/projects/12/OutputTest/Output.vm b/projects/12/OutputTest/Output.vm index e291708..dd83b63 100644 --- a/projects/12/OutputTest/Output.vm +++ b/projects/12/OutputTest/Output.vm @@ -1702,90 +1702,17 @@ goto WHILE_EXP0 label WHILE_END0 push constant 0 return -function Output.printInt 4 -push argument 0 -push constant 0 -eq -if-goto IF_TRUE0 -goto IF_FALSE0 -label IF_TRUE0 -push constant 48 -call Output.printChar 1 -pop temp 0 -push constant 0 -return -label IF_FALSE0 -push argument 0 +function Output.printInt 1 +push constant 6 +call String.new 1 pop local 0 +push local 0 push argument 0 -push constant 0 -lt -if-goto IF_TRUE1 -goto IF_FALSE1 -label IF_TRUE1 -push constant 45 -call Output.printChar 1 +call String.setInt 2 pop temp 0 push local 0 -neg -pop local 0 -label IF_FALSE1 -push constant 5 -call Array.new 1 -pop local 3 -push constant 0 -pop local 2 -label WHILE_EXP0 -push local 0 -push constant 0 -gt -not -if-goto WHILE_END0 -push local 0 -push constant 10 -call Math.divide 2 -pop local 1 -push local 2 -push local 3 -add -push constant 48 -push local 0 -add -push local 1 -push constant 10 -call Math.multiply 2 -sub -pop temp 0 -pop pointer 1 -push temp 0 -pop that 0 -push local 1 -pop local 0 -push local 2 -push constant 1 -add -pop local 2 -goto WHILE_EXP0 -label WHILE_END0 -label WHILE_EXP1 -push local 2 -push constant 0 -gt -not -if-goto WHILE_END1 -push local 2 -push constant 1 -sub -pop local 2 -push local 2 -push local 3 -add -pop pointer 1 -push that 0 -call Output.printChar 1 +call Output.printString 1 pop temp 0 -goto WHILE_EXP1 -label WHILE_END1 push constant 0 return function Output.println 0 -- cgit v1.2.3