HACK Assembly Ported to FUZE
-
This is a code example used within the Nand2Tetris course used to introduce the hack assembly language to the student.
// This file is part of www.nand2tetris.org // and the book "The Elements of Computing Systems" // by Nisan and Schocken, MIT Press. // File name: projects/06/rect/Rect.asm // Draws a rectangle at the top-left corner of the screen. // The rectangle is 16 pixels wide and R0 pixels high. @0 D=M @INFINITE_LOOP D;JLE @counter M=D @SCREEN D=A @address M=D (LOOP) @address A=M M=-1 @address D=M @32 D=D+A @address M=D @counter MD=M-1 @LOOP D;JGT (INFINITE_LOOP) @INFINITE_LOOP 0;JMP
All of this code from the example works. However, notice the code @32. This instruction is used mainly to move the pointer which is used to draw to the screen a line down. Thus this draws a rectangle. Adding 32 to the current position would work if the screen was (512x256). But the screen used within the VM is (400x200). So instead you'll need to replace that code with @25. 25 is used in that resolution to move the pointer to the next line directly below the previous location which is above.
-
Is this the type of thing that someone interested in assembly might use to ‘get a feel for it’ before possibly moving on to real hardware?
-
@toxibunny Indeed. In fact you'll learn how to build a computer from nothing but a NAND gate using a HDL language (Hardware description Language) which is used to program hardware in a hardware simulator. Assembly language would be the one abstraction up from that.
But yea, you can get a feel for assembly with this before moving on to the real hardware.
-
@LucasJG25 oh! I remember writing a CPU and assembler for this (as part of the NAND to Tetris course). Great job!
The book is pretty great btw for those who are interested.
@toxibunny Yes but it can give you the impression that assembly is this arcane art that only code wizards can practice properly, when in fact, even in the 80s, assembly was relatively easy in comparison to this thanks to the magic of macros!
-
Here is the same Hack Assembler but attached to a text editor. meaning you don't code in the FUZE editor anymore. But you instead code within the editor running in FUZE. Read the comments as to how to use the editor.
TextEditor:
Code: LVW53MND9K (pending)Make sure to use the delete key to erase characters from a line of text instead of backspace.
To display pixels to the screen give RAM[21451] = 1.
-
Due to memory constraints the ROM had to be reduced. From a whopping 30,000 lines of code to 583 lines. But this reminds me that in every decision when designing software or hardware. There are some sacrifices that have to be made. For example if I had stuck to 30,000 lines of code. Then only 1 file can be used to save and load from. That doesn't really sound to good in terms of saving your code and wanting to make a new project within the text editor without copying the entire text editor code into a new FUZE project.
But now with less lines of code. More files can be used. And amazingly a whole 100 files can be accessed. That speaks volumes!!
Also, I made the decision to reduce the lines of code to provide a challenge. Is it possible to write 583 lines of assembly code and make a game? And since we have 100 files at our disposal. 100 games!!!
Here's the file system working in action
I fixed that little bug which didn't show you the correct maximum of characters when loading and saving.
-
Me: -works hard on my circuit designer and has bugs- LucasJG25:yeah Ima create a full computer that is functional and you can program it... you are really amazing at this stuff!
-
@JMM161437 Thank you! I'm looking forward to your circuit designer. ;)
So, I'm currently working on the language (Not updating it but rather learning to code in it.) and I'll post up a little tutorial concerning how to construct functions, if statements and while loops. The more I learn how to code in this assembly code the more tutorials I'll be able to post up which will give some help to those who don't know about assembly or who find it difficult to code in this language.
The tutorial will take some time but it will be posted up soon.
-
well I look forward to it... as for my circuit designer... yeah I'm keeping it DC Electric and keep this up... by the end of it you might be able to emulate Fuze inside of Fuze lol
-
@JMM161437 hehe. We'll see. High level languages are more complex than assembly. But practice makes perfect.
-
@LucasJG25 yeah I like assembly, its fast when it runs and you can manipulate the CPU directly.
-
Based on what I've tested so far. Functions are done by storing the return address before jumping to complete a task. It's much easier to plan assembly code when you think in line with functions and how they work. The code below goes into a bit of this as well as dealing with IF statements and While loops.
//THIS IS A IF STATEMENT AND WHILE LOOP TEST. //R0 IS THE REGISTER THAT HOLDS THE RETURN ADDRESS //R1 HOLDS THE RETURN VALUE FROM A FUNCTION //R2 AND R3 ARE USED FOR ARGUMENTS. //ENTER VALUE INTO R2 OR R3 AND THE PROGRAM WILL EVALUATE //WHETHER R2 IS GREATER THAN R3 OR VICE VERSA @MAIN 0;JMP (CALL_CHECK) @R2 D=M @R3 D=D-M @IF_TRUE0 D;JGT @50 D=A @R1 M=D @IF_END0 0;JMP (IF_TRUE0) @100 D=A @R1 M=D (IF_END0) @R0 A=M 0;JMP (CALL_DRAW_PIXELS) @16 D=A @COUNT M=D @SCREEN D=A @R2 D=D+A @SCREEN_PTR M=D (WHILE_LOOP0) @SCREEN_PTR A=M M=-1 D=A @32 D=D+A @SCREEN_PTR M=D @COUNT M=M-1 D=M @WHILE_LOOP0 D;JGT @R0 A=M 0;JMP (MAIN) @CC_RET0 D=A @R0 M=D @CALL_CHECK 0;JMP (CC_RET0) @CDP_RET0 D=A @R0 M=D @R1 D=M @R2 M=D @CALL_DRAW_PIXELS 0;JMP (CDP_RET0) ```
-