I wish to remind you that next week, we're going to take all the chips and the logic gates we've built in previous weeks of this course, and we're going to assemble them together into a unified hardware platform which we call the, the Hack computer. Now, in order to set up to this very engaging and interesting construction work, we decided that this week we have to give you a good overview of what is it exactly that you're going to build. And that's one reason why we decided to explore all these notions of low-level programming the Hack assembly language, and the Hack hardware, because at this very low-level of operation, everything is tightly coupled. When you build the computer, you have think about its instruction set. When you design the machine language or the instruction set, you have to think about the computer. There's no way to avoid it. So once again, the purpose of of this week was, first of all, to expose you to low-level programming but we also wanted to give you a feeling of a, of the harder platform that you're actually going to implement in the next week. And this is also the purpose of of Project 4. In Project 4 you will write two programs that will let you put your hands on, on the hardware and, and program it in a, in a very low-level, i, in a level which is almost connected to the machine. So we'll do this by writing two relatively simple pro, programs. The first program will affect some algebraic manipulation, and the second program will interact with the user operating on both the screen and the keyboard. All right, so without further ado here is the first program which is called Mult. And the purpose of this program is to compute the product, or the multiplication, of R0 and R1. So here's a screenshot of this program executing in the CPU Emulator. In fact, it's a screenshot of the end of this program's execution. If you look carefully at the top of the RAM, you will see that RAM 0 is 6, RAM 1 is 7, and RAM 2 is 42. And that's exactly what this program is supposed to do, to multiply the first two registers in the memory and compute the, and, and, and place the product of these two numbers in RAM 2. So the assumption is that once the user has loaded this program into the data memory into the instruction memory, the user also places two numbers in RAM 0 and RAM, RAM 1. Clicks the Go button or the Play button starts playing and if everything works nicely the program will compute the product of these two numbers. On the right-hand side, in the screen area we see a script that we supply together with this project. And the script is designed to to test the program using some pairs of numbers that that we made up. And obviously you can explore the script as well in order to see what kind of tests we are going to subject your programmer to. And you know, this is maybe also the place to point out that in the simulator, in, in the CPU Emulator, we use the screen as sort of a multi-purpose device. Sometimes it works as a real physical screen. Sometimes, we use it as a window to display our test scripts, test scripts and, and so on. All right, so once again, in this area you see the input the two inputs and the, the output of the program. And I guess the question that burns right now is how do we write such a program? Well recall, that the, the Hack machine language doesn't have a multiplication operation. All we have is addition and subtraction. So we're faced with a challenge of expressing a multiplication operation using addition and subtraction. I don't think that I have to say much more than this, I can give you perhaps only one more hint. You will have to use a loop. And using this loop, you will somehow compute the the result. By the way those of you who will choose to take part two of this course, I meant to that as part two, will also go through the process of developing an operating system. And one thing that our operating system is going to deliver, to deliver is a, is a math library that features all sorts of mathematical operations, and one of these mathematical operations is going to be multiply. And the multiplication algorithm that you will use in the operating system is, extremely efficient and something that we don't expect you to do in this exercise. In this exercise we simply expect you to somehow multiply the two numbers and, you know, maybe you can try to do it as, as efficiently as you can. But we don't expect you to get out of your way and come up with some very fancy algorithms to, to carry out multiplication. All right, so this the first assignment. And the second assignment is to write a simple interactive program that performs the following operation. This program listens to the keyboard, and as long as the user does nothing with the keyboard, nothing happens. But once the user touches a key on the keyboard, any key, you know, once you touch a key look what happens. The program blackens the screen completely. And once you lift your finger from the keyboard, the screen becomes again clear. You put your finger again on any key on the keyboard, the key the screen becomes black. You take your you take your finger away, the screen becomes clear. That's what this program is doing. It is going through some, some sort of infinite loop that listens to the keyboard all the time and acts accordingly. So actually we have here two separate challenges. One of them is to probe the keyboard and, and understand what the, you know, whether or not any key is pressed. And the other challenge is to be able to blacken the screen or whiten the screen, which is essentially the same operation in one we write a certain value to the memory map and in the other operation we, we write some other value. What makes this exercise somewhat easy is the fact that we operate on the entire memory map. So we don't have to be picky and select certain pixels to turn on and off, we simply sort of a shotgun approach, we either turn on all the pixels or turn off all the pixels. In that respect the program is is not terribly complicated and in general it's not, it's not really a terribly complicated program. But it's kind of fun. You know, it allows you to see how you, you can control peripheral devices using a, a standard Hack machine language called. All right, so here is the general implementation strategy. Listen to the keyboard. To blacken or create a screen, we write code that fills the entire screen memory map with white or black pixels. And in order to do this, we have to address every register in the memory map, and we do it using some sort of a loop. That works with pointers in a very similar way to what we did in in the previous unit when we discussed pointers manipulation. So you really have all the firepower that you need in order to, to implement this particular program. In order to test this program on the CPU Emulator, make sure that the no animation selection is is selected, and also, we cannot provide a test script that tests this program, or, or we can, but we, we thought that it would be too com, too complicated, so you simply have to test it. As I di, demonstrated, you know, put your finger down, put your finger up and hope that the screen will be blackened or whitened and so on. You can also think, if you want, you can think about some fancy ways to blacken or whiten the screen. You know, instead of going line-by-line, you wa, you may want to go column-by-column. You wa, you may want to create some this, this will be quite challenging to create some drawing you know swirls. I don't know. You can, you use your imagination. Do whatever you want in order to blacken or whiten the screen. And and that's actually what is expected from you in in this particular program. All right I want to describe some general things about the overall program development process or cycle when you want to write programs in the Hack machine language. First of all, your programs are going to reside within regular text files that you can write with any text editor. So, you invoke your favorite text editor. You write the program. You save it using the extension asm. And by convention we give our programs names that begin with capital letters so we give it a name, .asm. And then you load this program as is into the CPU Emulator. As I explained before, the CPU Emulator has this very nice service. That when you load the program it automatically translates the program into binary code. So you can next run the program on the CPU Emulator and ask yourself if you're satisfied with the results. If so, you are done. You can submit the program to us and if you are unsatisfied with the program which will be a probably the case in the first few iterations of the debugging, then you will, you fix the program. You look for errors and you fix the errors and you fix them by going back into the text editor, you know, working in the editor, saving the program again. Reloading it into the CPU Emulator, and so on. So it's quite convenient to have two windows open on the, on your screen at the same time, the text editor here, CPU Emulator there, and you can easily, you know, move between the two but don't forget to load your updated program after you you fix it. The error diagnostics of the CPU Emulator is quite primitive. So when you load the program into the CPU Emulator, if there's a syntax error in this program, the CPU Emulator will simply refuse to load it. It will issue some cryptic error message something like oh, it will give you some very important information and to give you the line number with error occurred and the first error that was found. And then you can go into this line number and and fix the problem. And once you, you are done with the, with the syntax e, errors, well, then the real problems begin, you know? These are the run-time errors, which are much harder to detect. And you typically detect them by watching the execution of your program, seeing that something does not work properly. And then once again you have to go through the same cycle. And fix your program until it works to your satisfaction. So this is the general mechanics of developing a program using a CPU Emulator and a text editor. And as you see it, it's quite, it's quite simple and friendly. All right finally I want to say a few words about best practice and advice on do's and do, and don'ts. There's no reason really to think about a machine language programs as some sort of a, a strange animal. It's yet another example of a, a programming art effect. And all the principles that applied to writing programs in a high-level language also apply at the low level. We expect your programs to be short, or not unnecessarily long. They should be short, they should be efficient, they should be elegant, and they should be self-describing. So we expect you to document the programs, but do it by using some judgment. You know, don't over-document the program. The best, I think thing to do is simply to look at our examples and to follow something similar, so, you know, typically we use some high-level operation to describe you know? We start a comment, we write something like, here we're going to say, if i is greater than n, goto, and then, we write the 6 or 7 instructions in machine code that actually implement this this semantics. Now here's some very important technical tips that will make your life easier. First of all, you must use symbolic variables and labels. You know, otherwise your programs will be helplessly and hopelessly complex, and and cryptic. So, if you want to go to somewhere in your program, use a label. If you want to store something repetitively, make up a variable name. So, when you're done writing the program, you know, look at it, and make sure that you don't see any actual educes there, you have to make sure that everything is symbolic, you know, this is one very important ritual, of well-written machine language, language programs. When you invent your variables and your labels, as usual in programming, use sensible names. You know, don't make cryptic names like gu5 3 percent or something like this. Use instead something like loop end, stop, positive, negative. You know, depending on what is it that you want to do, try to use sensible label names and, likewise, variable names. You know, use nice names, like i, end, some, count, and, and so on. Now, when you declare or, or when you use these variable names, use lower-case for variables and upper-case for labels. If you go back to our program examples, you will see that all the labels, you know typically words like END and STOP were uppercase, and all the variables i, n, x, y and so on were lower case. If you follow this convention, which is not required by the way, you know, it's, it's not something that they, that the assembler cares about. But if you use this convention you will have a very easy time for yourself to distinguish between variables and labels. So when you see an @ command when an A command, when you see something about @sum, if the sum is lower-case, you know it's a variable. If it's uppercase, you know that we mean to go to a label called SUM. And finally, use indentation. Once again, follow the examples that we gave in the previous units, and make sure that your programs are good-looking and easy to read. And it's always recommend, recommended, to start with pseudo code. Writing machine level code is always challenging. And your life will be much easier if you first, you know, make up the program or write the program using some, some sort of a high-level language that you can make up for yourself. Or once again, follow our examples. Once the program works nicely in pseudo code, and this something that you have to, to check on, on a piece of paper, you can then translate it into machine language and continue the debugging process. All right where do you find everything you need? Well as usual, you go to the NAND to Tetris website. You look at Project 4. All the files that you need for this exercise are described in for this project. I'm sorry, are described in in this webpage, and there's no need to download anything, because if you downloaded the software suite at the beginning of the course, you now have on your personal computer, you have a directory called project/04. And this directory already contains all the files that you have to use and and manipulate in in Project 4. So this has been the the unit in which we we hopefully give you some tips on how to how to write the, the programs in Project 4. And we now go on to the last unit in this week in which we reflect on what we did during the entire week.