From fd8e3bee660f22032b51bc12452353b141e125be Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Wed, 17 Jan 2018 16:44:59 +0100 Subject: checkpoint - finished Array - finished Sys - revised Memory --- projects/12/MemoryTest/Memory.jack | 24 ++------ projects/12/MemoryTest/Memory.vm | 118 +++---------------------------------- 2 files changed, 13 insertions(+), 129 deletions(-) (limited to 'projects/12/MemoryTest') diff --git a/projects/12/MemoryTest/Memory.jack b/projects/12/MemoryTest/Memory.jack index 8583b62..54e04aa 100644 --- a/projects/12/MemoryTest/Memory.jack +++ b/projects/12/MemoryTest/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,7 @@ class Memory { let heap = 2048; let heap[0] = -1; let heap[1] = 14334; + let last = 0; return; } @@ -62,23 +63,10 @@ class Memory { * it available for future allocations. */ function void deAlloc(Array o) { var int oBase; - var int current; - var int prev; - let prev = 0; - let current = heap[prev]; let oBase = o - heap - 2; - while ((current > -1) & (~(current = oBase))){ - let prev = current; - let current = heap[current]; - } - if (current = -1) { - do String.println("The impossible happened"); - do Sys.error(1); - return; - } 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/MemoryTest/Memory.vm b/projects/12/MemoryTest/Memory.vm index c7410fc..0570b46 100644 --- a/projects/12/MemoryTest/Memory.vm +++ b/projects/12/MemoryTest/Memory.vm @@ -21,6 +21,8 @@ pop pointer 1 push temp 0 pop that 0 push constant 0 +pop static 2 +push constant 0 return function Memory.peek 0 push argument 0 @@ -226,137 +228,31 @@ push constant 2 add return label IF_END0 -function Memory.deAlloc 3 -push constant 0 -pop local 2 -push local 2 -push static 1 -add -pop pointer 1 -push that 0 -pop local 1 +function Memory.deAlloc 1 push argument 0 push static 1 sub push constant 2 sub pop local 0 -label WHILE_EXP0 -push local 1 -push constant 1 -neg -gt -push local 1 push local 0 -eq -not -and -not -if-goto WHILE_END0 -push local 1 -pop local 2 -push local 1 push static 1 add -pop pointer 1 -push that 0 -pop local 1 -goto WHILE_EXP0 -label WHILE_END0 -push local 1 push constant 1 neg -eq -if-goto IF_TRUE0 -goto IF_FALSE0 -label IF_TRUE0 -push constant 23 -call String.new 1 -push constant 84 -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 109 -call String.appendChar 2 -push constant 112 -call String.appendChar 2 -push constant 111 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 115 -call String.appendChar 2 -push constant 105 -call String.appendChar 2 -push constant 98 -call String.appendChar 2 -push constant 108 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 32 -call String.appendChar 2 -push constant 104 -call String.appendChar 2 -push constant 97 -call String.appendChar 2 -push constant 112 -call String.appendChar 2 -push constant 112 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 110 -call String.appendChar 2 -push constant 101 -call String.appendChar 2 -push constant 100 -call String.appendChar 2 -call String.println 1 -pop temp 0 -push constant 1 -call Sys.error 1 -pop temp 0 -push constant 0 -return -goto IF_END0 -label IF_FALSE0 -push local 2 -push static 1 -add -push local 1 -push static 1 -add -pop pointer 1 -push that 0 pop temp 0 pop pointer 1 push temp 0 pop that 0 -push local 2 -push constant 1 -add -push static 1 -add -push local 2 +push static 2 push static 1 add -pop pointer 1 -push that 0 -push local 2 -sub -push constant 2 -sub +push local 0 pop temp 0 pop pointer 1 push temp 0 pop that 0 +push local 0 +pop static 2 push constant 0 return -label IF_END0 -- cgit v1.2.3