assemblynand2tetris

Example of Clock and DFF for Nand2Tetris Hack .asm Assembly Code


I am a huge learn-by-example person, which means describing it is typical leaves me at a loss, for my learning style, because I need to see first, and then any explanation can be realized. I have been playing with the Nand2Tetris program's Hack Assembly Language .asm files, and creating snippets to study, use and learn from.

My question is if I wanted to clock time, for example, how can I time or tick-tock the time it takes to run a function? I'm looking at the DFF gate as a possible solution, but I'm very confused about how to even go about using it for this task and need to see a solid example code. I am hoping someone can give me a code example of how to code this in ASM format.

For example:

How to translate TimeEnd - TimeStart to something legible?

Is this possible?


Solution

  • There is no way to read a clock / tick time programmatically; the Hack machine has no way to get that info.

    Similarly, the CPU Emulator doesn't have the ability to do this, though having the option to dump an instruction trace would be nice (perhaps something to suggest for the future).

    You can of course manually trace your code and figure out how many instructions are executed. Since each instruction takes a single cycle, this is relatively straightforward.

    If your are absolutely desperate, I wrote a python version of the CPU Emulator that emulates the machine at the functional-unit level as part of a project to implement the Hack CPU in relay logic. It can trace out instructions and could potentially be modified to do this for you. You can find it here: https://github.com/RJWoodhead/Relay2Tetris/blob/master/Simulator.md