Unit 7 Assignment Solution
Unit 7 Assignment Solution
In the first problem we needed to implement a ‘for’ loop using the assembler available in the Hack
system. The example in Assembler has a lot of comments designed to help you understand the
program. First, everything to the right of the // is actually a comment. The only code is actually to the
left of the //. Immediately to the right of the //, there is a number. This represents the location in
ROM of each line in the program. I have the heading “program counter” above this to indicate that if
you watch the current state of the program counter in the CPU simulator you will see the code being
executed for any program counter number matches the number in the comments.
One important thing to keep in mind is that these examples have been put together using a ‘brute force’
approach. What this means is that the most straight forward approach to solve the problem was used.
It is possible (and likely) that there is a more elegant or efficient way to implement any of these
problems. This was not done for these examples to make the code easier to read and follows. If you
have implemented your solutions to these problems using a more elegant approach your solution is just
as valid as these examples!
Problem 3 implements a while loop and within the while loop an if expression showing the use of
complex nested code implemented using the Hack assembler.
Problem 4: demonstrates the ability to create an array and then manipulate the array both by traversing
it (moving through the array to be able to extract items in the array) and also demonstrating the ability
to update values or use values within the array.
One important feature that you will want to understand in this example solution appears in red in the
following code. In this case we are maintaining the array index in memory location 10. What we want
to do is use the current value of the index to get access to an item in the array. The way that we do this
is by loading the value in memory location 10 into the A register. Any value in the A register when using
an M command is assumed to be a memory address so these two lines of code in effect implement the
ability to access items in the array via an index.