"Implement the following tasks using Hack commands"

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

"Implement the following tasks using Hack commands"

LionelGoulet
OK, the lecture 04 slides has me showing the students how to write Hack assembler code. Two slides of "Set D to 19" and "arr[3] = -1" examples. Is there a "right answers" page for these examples? I think I have all but the last one correctly coded, but the last one has me stumped. A little help, please? arr[j] = 17 How do I do dat?
Reply | Threaded
Open this post in threaded view
|

Re: "Implement the following tasks using Hack commands"

cadet1620
Administrator
Answered via email.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: "Implement the following tasks using Hack commands"

pimple123
Could you send me the same email? arr[j]=17 is very easy to do with R0-R15, but I can't seem to find a solution that doesn't use them.
Reply | Threaded
Open this post in threaded view
|

Re: "Implement the following tasks using Hack commands"

cadet1620
Administrator
A quick search doesn't turn up that 5 year old email...

The standard solution is to use one of the R registers. When you get to writing your VM Translator, you will find that R13-R15 are reserved for your generated ASM code to use for things like this.

There is a way to do this that does not require a temp register:

Compute the destination RAM address in D.
@17
Swap values in A and D registers.
M=D

It takes 3 instructions to swap A <> D.

Hint: think algebra and subtraction.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: "Implement the following tasks using Hack commands"

pimple123
Thank you very much for the quick response. That hint was very useful indeed, thanks!