where to start?

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

where to start?

adamjaustin84
this may sound a bit silly, but is project 7 implemented in Hack assembly or in another programming language? i made my assembler in python and plan to use python all the way through the rest of the projects. I have reread the chapter a few times and in parts it talks about an assembly implementation and in end of the chapter it seems to say it is written up in something different. can someone point me in the right direction here??

cheers
Reply | Threaded
Open this post in threaded view
|

Re: where to start?

cadet1620
Administrator
The VM Translator is a tool that runs on your PC/Mac/whatever that translate the VM language into Hack assembly language.  The resulting Hack asm can be run on the Hack CPU Simulator.

Effectively, the VM Translator is the back end for the Jack Compiler that you will write in chapters 10 and 11.

Python's a good language to use for your tools — it's what I used.

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

Re: where to start?

adamjaustin84
cheers for the reply.

so its been at least a week of banging my head against my laptop trying to figure this thing out. i have absolutely NOTHING. i just can't see how this is implemented. the trouble is I'm trying to learn python at the same time as this and it may be throwing me off. I've read the chapter quite a few times and its just not clicking. I'm wondering if there is any supplemental material/videos that may help with the understanding of whats going on here.
also what is the C_ARITHMETIC, C_PUSH, C_POP, etc all about? are these jack commands? what are they?
in figure 7.7 in the book, does each element here have its own stack? share a stack?
so from what I'm understanding of the stack, the initial address is 256, then you push a number in it, then SP is now 257?

@256 |@sp|    push   | [val] |       <= is this location now THIS?? what is THIS and THAT doing??    
@257 |      |      =>   |@sp   |  
@258 |      |              |         |


sorry if these questions should be obvious, it is very frustrating to have had hit such a massive mental wall.

if anyone has any suggestions on any reading material or anything, please let me know

cheers

adam
Reply | Threaded
Open this post in threaded view
|

Re: where to start?

ybakos
All great questions. But, one step at a time. I have a couple suggestions.

You've got the Hack hardware platform, and can write arbitrary assembly language. You can think of the concept of a "virtual machine" as an agreement that we will make about how to use all the memory of the system. For example, we'll agree to use registers 0 - 15 for special purposes; we'll agree to use addresses 16 - 255 for "static" things (you'll discover this later) and addresses 256 - ..., as a "stack," and the rest of memory as a "heap."

To manipulate this virtual machine, we'll rely on an intermediate language: the Hack VM language.

Your goal, for project 7, is to build a program that translates the vm instructions into assembly language. Doing so "realizes" the virtual machine on the hardware platform. It's this "vm translator" program that will enforce the rules like "push 123 means to figure out where the top of the stack is, and put 123 there."

Consider how you built an assembler in the previous project to translate assembly mnemonics into "binary." An asm file was the input, and a hack file was the output. Your vm translator will accept one or more .vm files as input, and generate an .asm file as its output.

As a first step for Project 7, write a Python program, my_translator.py, that allows you to run:
python my_translator.py SimpleAdd/SimpleAdd.vm
And, as a first step, this program should just read the lines in SimpleAdd.vm and print them to the console.

Next, modify your program so that it doesn't print the contents of the file, but rather saves the contents of the file in SimpleAdd.asm. You'll know this is right when the contents of SimpleAdd.asm are the same as SimpleAdd/SimpleAdd.vm.

Next, modify your program so that, for each line in SimpleAdd.vm, you translate it into assembly mnemonics, and write that to the SimpleAdd.asm file. One step at a time: handle push constant N.

See if you can get those steps completed, and post back when you're stuck.

To answer some of your questions, yes, R0, aka SP, is the Stack Pointer, and its contents are a number representing the memory address of the top of the stack. An empty stack has a value of 256 in SP. Pushing new values onto the stack should store the new value at the address specified by SP, and then increment SP, placing the top of the stack "on top of" the newly inserted value.

There is just one stack, but as you'll see later, there's a twist when it comes to functions.

C_ARITHMETIC, etc are just constants representing the type of vm command. For example, push constant 0 is a C_PUSH command, and add is a C_ARITHMETIC command. Your translator will need to handle these kinds of commands differently, and therefore need to differentiate between them.

Reply | Threaded
Open this post in threaded view
|

Re: where to start?

adamjaustin84
cheers for the reply. ill look at what you said and hopefully ill be able to crack it soon