NAND2GO Chapter 4. Machine Language
1. Context |
We have learned how to use HDL to build logic gates and to describe Boolean algebra. | ||
2. Goal |
Learn to use assembly language to realize simple functions. Understand two of logical thinking strategies- abstraction and staging. | ||
3. Measurable Effects |
Writing assembly language program which can be tested by test files. Knowing that assembly language shows the staging in computer structure, staging can simplify the translation process. Knowing that assembly language is an abstraction of logical gates to make program more readable and writable. | ||
4. Outputs | 5. Process | 6. Inputs | |
Assembly language programming ability and logical thinking. Two projects mult and fill. |
Learn the function and stage of assembly language in a computer then understand the necessity of it. Learn the basic instructions and symbols of assembly language in order to write a assembly language program. Learn how assembly instructions operate the computer memory and register. |
memory and register chip built in chapter 3. nand2tetris course | |
7. External Factors |
Introduction
We have learned how to use HDL to describe logic flow in the first three chapters and also built the register and memory ready to be used. At this chapter we will learn a new language, assembly language. It is in fact an abstract representation of what we realized in the last three chapters. Learned from the introduction of the book, we know that modern computer recognizes only Boolean value, namely 1 and 0. So the code used for computer to read is all written by 1 and 0, that's machine language. Programmers use high-level computer language, such as C, Python and Java, to realize complex functions. So, there must be a way to translate the high-level language to machine language. But it is very hard because there's a huge difference between them. Therefore, assembly language was born as a transition of these two languages. Assembly language contains symbols and numbers, which is comfortable for people to read and understand, so it’s easier to translate high-level languages into assembly language. Besides, assembly language also has a strong correspondence to the machine language. Therefore, thanks to the assembly language, the translation process is simplified a lot. This exactly shows an important strategy of logical thinking——staging. When we find a work is difficult to do, we can find a transition, divide the problem into several parts and solve them respectively by focusing on only one part at a time. So, let's take out journey of assembly language.
What is assembly language?
Assembly language is a low-level programming language which can be translated into the machine language directly, and the machine language can be recognized and executed by the hack computer. Assembly language tells memory, CPU, register directly what to do in every step, such as saving a value in a RAM or adding two values up. It is simple logically but it's hard to tell what the whole program aims to do. Often, a simple high-level language program can be translated into hundreds and thousands of lines in assembly language, so it is not common to write a assembly language program by human today, instead we use tools like compiler to help us translating the high-level language to assembly language. Therefore, we learn assembly language not just to write an assembly program, but more importantly to understand the level assembly language occupied in a computer system and to establish logical thinking pattern.
Background
Why Assembly Language
Assembly language works as a middle stage language to connect high-level language and machine language, showing an important conception-staging. With staging, we can simplify the translating process and it is easier to understand. This means we divide the whole process into small processes and focus on only one stage in each process.
In the last 3 chapters we have built the basic chips, for the purpose of building a complete computer. The computer will be built in the project 5. Project 2,3,5 are all implemented by HDL, then why we should take a look at assembly language in the middle of these chapters?
In the project 5, when we finally build a hack computer, we need assembly language fi le (.asm) to test whether the computer is functional, which means the computer can execute the .asm fi les and have a correct output. Therefore, we need to master the basic skills to write a simple assembly language program.
Assembly Language and Machine Language
Machine language writes with only "0" and "1", which is the only thing the computer can understand. It indicates what computer should do in every step. Assembly language indicates as much as machine language but replaces the "0","1" code with instructions and symbols. It makes the program more readable. The translation from assembly language to machine language can be implement by assembler (chapter 6).
Assembly Language Specification
Memory & Registers
In the last chapter, we built the memory and register. Now let's take a look what do they do in a hack computer. Memory is used for storing data. In a program, tons of data will be stored in and taken from the memory. RAM (random access memory) is a kind of memory, it has faster access speed but it can lose its data once power is off. In this project we use RAM to store the data produced in the program. RAM is divided into many cells, each of them contains 16 bits in binary. So in the hack computer, each data and instruction is translated into a 16-bit binary code and stored in one cell of RAM. These cells are tagged with addresses and computer locates them by their addresses.
Register is classified into two kinds, A-register and D-register. A-register is for storing an address temporarily and sometimes it can also be used to store a value. D-register is for storing a value(data)
In assembly language, A, D and M are used to refer to the address or value in A-register, the value in D-register and the value in the address stored in A-register.
example
@1 //1 is a value
D=A //copy 1 into D-register
@1 //1 is an address, referring to RAM [1]
D=M //copy the value in RAM [1] into D-register
Instructions
The concrete functions in the hack computer are accomplished by instructions of assembly language. The instructions are classifi ed into two kinds, A-instruction and C-instruction.
A-instruction is to indicate an address or a value. Normally,an A-instruction is followed by a C-instruction. The address or value will be used or calculated in the following C-instruction.
C-instruction answers three questions: (a) what to compute, (b) where to store the computed value, and (c) what to do next? These are indicated respectively by the three parts of C-instruction: comp, dest and jump.
A-instruction
@value
value can be either a non-negative decimal number or a symbol.
This instruction occupies a 16-bit RAM in computer, the fi rst bit is "0", and the rest is the binary code of the non-negative decimal number or the number that the symbol refers to.
example:
"@9"
can be translated into "0000000000001001"
C-instruction
dest=comp;jump
The first three bits are "1", the 4th-10th bits encode the comp; the 11th-13th bits encode the dest; the 14th-16th bit encode the jump.
If this is a compare and goto operation, then dest is empty(the three bits of dest are all "0") and the "=" is omitted.
example:
"D; JGE” //if D>=0 jump"
can be translated into "1110001100000011"
If this is an assign operation, then jump is empty and ";" is omitted.
example:
"D=M”
can be translated into "1110001100001000"
The correspondence is listed in the fi gure.
Fig 4.1 Jump Code
Fig 4.2 Comp Code
Fig 4.3 Dest Code
example
@3 //3 is a value
D=A //copy the value 3 in the A-register into D-register
@5 //5 is a value
D=D+A //compute 3 add 5 and store the result in the D-register
@1 //1 is an address, refers to RAM [1]
M=D //copy the value in the D-register into RAM [1]
As we can see, A-instructions and C-instructions cooperate with each other and then get things done!
Symbol
A good reason why assembly language is more readable than machine language is that assembly language uses symbols, it can represent a concrete meaning and refer to a non-negative decimal number. Assembly language contains different types of symbols.
Predefined symbols
Virtual registers
R0……R15 refer to RAM addresses 0……15 respectively
In an A-instruction, @0 and @R0 are exactly the same if 0 here refers to a RAM address. The system only predefined the first 16 RAM addresses as Rx, from RAM [16], only the address can be used.
Pointers
SP, LCL, ARG, THIS, THAT refers to RAM addresses 0 to 4, respectively.
SCREEN and KBD refers to RAM [16384] and RAM [24576], respectively.
RAM address 16384(0x4000 in hexadecimal code) is the beginning of the screen RAM part which ends at RAM [24575], these 8192 addresses stores 131056 bits of data. Each bit encodes the black and white condition of a pixel of screen. The hack computer's screen is 512*256 pixels, exactly 131056 bits in total.
Hack computer uses only one RAM address 24576(0x6000 in hexadecimal code) to store the ASCⅡ code of the key is being pressed.
Virtual symbols
(xxx)
virtual symbols are used to mark the line next to it, it won't be interpreted in machine language. when there's "@xxx" in the program, the computer knows it should go to the line after "(xxx)" and start implementation from this line.
Variable symbols
A variable symbol is defined by programmers themselves and would be replaced with its RAM address in the machine language.
Input/output
screen
Hack computer's screen has 256 rows with 512 pixels each row. Every 16 pixels on one row are controlled by a RAM's 16 bits. Each pixel has two conditions, black and white, white is coded with 0 and black is 1.
Screen's memory section is from RAM [16384] to RAM [24575], 8 k in total.
*keyboard
Different from the computer we use, hack computer uses only one 16-bit RAM to save keyboard input, RAM [24576]. When no key on the keyboard is pressed, the RAM's value is 0. When a key is pressed, its ASCII code is saved in the RAM.
Development and Application
Assembly language have no portability; it varies from how CPU works. So different computers have their own set of assembly language. What we learned in this chapter is very different from the common used assembly language today which contains more symbols and instructions. But no matter what kind of assembly languages, it follows the same idea and logic and plays the same role in a computer system.
There are some instructions are widely used in today's assembly languages, such as ADD, MOV, SHL, BT and so on. If you are interested, search online and learn more about assembly language.
Project
Mult
Assembly language of hack computer does not have the multiplication operation. So we can break it down to finite addition operations. We can use a loop to accomplish that.
If you want to optimize your program on time, you can compare the two addends and use the smaller one as cycle times to do the loop.
Fill
To solve this problem, you need to have a good conception of the I/O of hack computer. You can use the jump operation to judge whether a key is being pressed and set the values in every RAM address of screen section -1(1111111111111111) to blacken the screen.
Glossary
Assembly Language
Machine Language
References
Description: References are divided into books, journal articles, audio recordings, videos, web pages, research reports, etc. Please select the appropriate template for reference according to your needs.
- Nand2teris, Temporary link failure, Refer to chapter 04