["294 Appendix A Figure A.5 GUI-empowered chips. Since the loaded HDL program uses GUI-empowered chips as internal parts (step 1), the simulator draws their respective GUI images (step 2). When the user changes the values of the chip input pins (step 3), the simulator re\ufb02ects these changes in the respective GUIs (step 4). The circled horizontal line is the visual side effect of storing \u00c01 in memory location 5012. Since the 16-bit 2\u2019s complement binary code of \u00c01 is 1111111111111111, the computer draws 16 pixels starting at the 320th column of row 156, which happen to be the screen coordinates associated with address 5012 of the screen memory map (the exact memory-to-screen mapping is given in chapter 4).","295 Hardware Description Language (HDL) Chip name Speci\ufb01ed Has Comment in chapter GUI Nand Not 1 Foundation of all combinational chips And 1 Or 1 Xor 1 Mux 1 DMux 1 Not16 1 And16 1 Or16 1 Mux16 1 Or8way 1 Mux4way16 1 Mux8way16 1 DMux4way 1 DMux8way 1 HalfAdder 1 FullAdder 2 Add16 2 ALU 2 Inc16 2 rZ DFF 2 Bit 3 Foundation of all sequential chips Register 3 ARegister 3 DRegister 3 rZ Identical operation to Register, with GUI RAM8 3 rZ Identical operation to Register, with GUI RAM64 3 rZ RAM512 3 rZ RAM4K 3 rZ RAM16K 3 rZ PC 3 rZ ROM32K 3 rZ Program counter Screen 5 rZ GUI allows loading a program from a text \ufb01le Keyboard 5 rZ GUI connects to a simulated screen 5 rZ GUI connects to the actual keyboard Figure A.6 All the built-in chips supplied with the present version of the hardware simulator. A built-in chip has an HDL interface but is implemented as an executable Java class.","296 Appendix A other than Hack would probably bene\ufb01t from the simulator\u2019s ability to accommo- date new built-in chip de\ufb01nitions. Developing New Built-In Chips The hardware simulator can execute any desired chip logic written in HDL; the ability to execute new built-in chips (in addition to those listed in \ufb01gure A.6) written in Java is also possible, using a chip-extension API. Built-in chip implementations can be designed by users in Java to add new hardware components, introduce GUI effects, speed-up execution, and facilitate behavioral simulation of chips that are not yet developed in HDL (an important capability when designing new hardware platforms and related hardware construction projects). For more information about developing new built-in chips, see chapter 13.","Appendix B: Test Scripting Language Mistakes are the portals of discovery. \u2014James Joyce (1882\u20131941) Testing is a critically important element of systems development, and one that typi- cally gets little attention in computer science education. In this book we take testing very seriously. In fact, we believe that before one sets out to develop a new hardware or software module P, one should \ufb01rst develop a module T designed to test it. Fur- ther, T should then become part of P\u2019s of\ufb01cial development\u2019s contract. As a matter of best practice, the ultimate test of a newly designed module should be formulated not by the module\u2019s developer, but rather by the architect who speci- \ufb01ed the module\u2019s interface. Therefore, for every chip or software system speci\ufb01ed in the book, we supply an of\ufb01cial test program, written by us. Although you are wel- come to test your work in any way you see \ufb01t, the contract is such that eventually, your implementation must pass our tests. In order to streamline the de\ufb01nition and execution of the numerous tests scattered all over the book projects, we designed a uniform test scripting language. This lan- guage works almost the same across all the simulators supplied with the book: m Hardware simulator: used to simulate and test chips written in HDL m CPU emulator: used to simulate and test machine language programs m VM emulator: used to simulate and test programs written in the VM language Every one of these simulators features a rich GUI that enables the user to test the loaded chip or program interactively, using graphical icons, or batch-style, using a test script. A test script is a series of commands that (a) load a hardware or soft- ware module into the relevant simulator, and (b) subject the module to a series of preplanned (rather than ad hoc) testing scenarios. In addition, the test scripts feature commands for printing the test results and comparing them to desired results, as","298 Appendix B de\ufb01ned in supplied compare \ufb01les. In sum, a test script enables a systematic, repli- cable, and documented testing of the underlying code\u2014an invaluable requirement in any hardware or software development project. Important We don\u2019t expect students to write test scripts. The test scripts necessary to test all the hardware and software modules mentioned in the book are supplied by us and available on the book\u2019s Web site. Therefore, the chief purpose of this appendix is to explain the syntax and logic of the supplied test scripts, as needed. B.1 File Format and Usage The act of testing a hardware or software module using any one of the supplied simulators involves four types of \ufb01les: Xxx.yyy: where Xxx is the module name and yyy is either hdl, hack, asm, or vm, standing respectively for a chip de\ufb01nition written in HDL, a program written in the Hack machine language, a program written in the Hack assembly language, or a program written in the VM virtual machine language; Xxx.tst: this test script walks the simulator through a series of steps designed to test the code stored in Xxx.yyy; Xxx.out: this optional output \ufb01le keeps a printed record of the actual simulation results; Xxx.cmp: this optional compare \ufb01le contains a presupplied record of the desired simulation results. All these \ufb01les should be kept in the same directory, which can be conveniently named xxx. In all simulators, the \u2018\u2018current directory\u2019\u2019 refers to the directory from which the last \ufb01le has been opened in the simulator environment. White space: Space characters, newline characters, and comments in test scripts (Xxx.tst \ufb01les) are ignored. Test scripts are not case sensitive, except for \ufb01le and di- rectory names. Comments: The following comment formats can appear in test scripts: \/\/ Comment to end of line \/* Comment until closing *\/ \/** API documentation comment *\/","299 Test Scripting Language Usage: In all the projects that appear in the book, the \ufb01les Xxx.tst, Xxx.out, and Xxx.cmp are supplied by us. These \ufb01les are designed to test Xxx.yyy, whose devel- opment is the essence of the project. In some cases, we also supply a skeletal version of Xxx.yyy, for example, an HDL interface with a missing implementation part. All the \ufb01les in all the projects are plain text \ufb01les that can be viewed and edited using plain text editors. Typically, one starts a simulation session by loading the supplied Xxx.tst script \ufb01le into the relevant simulator. Typically, the \ufb01rst commands in the script instruct the simulator to load the code stored in Xxx.yyy and then, optionally, initialize an output \ufb01le and a compare \ufb01le. The remaining commands in the script run the actual tests, as we elaborate below. B.2 Testing Chips on the Hardware Simulator The hardware simulator supplied with the book is designed for testing and simulating chip de\ufb01nitions written in the Hardware Description Language (HDL) described in appendix A. Chapter 1 provides essential background on chip development and testing, and thus it is recommended to read it \ufb01rst. B.2.1 Example The script shown in \ufb01gure B.1 is designed to test the EQ3 chip de\ufb01ned in \ufb01gure A.1. A test script normally starts with some initialization commands, followed by a series of simulation steps, each ending with a semicolon. A simulation step typically instructs the simulator to bind the chip\u2019s input pins to some test values, evaluate the chip logic, and write selected variable values into a designated output \ufb01le. Figure B.2 illustrates the EQ3.tst script in action. B.2.2 Data Types and Variables Data Types Test scripts support two data types: integers and strings. Integer con- stants can be expressed in hexadecimal (%X pre\ufb01x), binary (%B pre\ufb01x), or decimal (%D pre\ufb01x) format, which is the default. These values are always translated into their equivalent 2\u2019s complement binary values. For example, the commands set al %B1111111111111111, set a2 %XFFFF, set a3 %D-1, set a4 -1 will set the four variables to the same value: a series of sixteen 1\u2019s, representing \u2018\u2018minus one\u2019\u2019 in","300 Appendix B \/* EQ3.tst: tests the EQ3.hdl program. The EQ3 chip should return true if its two 3-bit inputs are equal and false otherwise. *\/ load EQ3.hdl, \/\/ Load the HDL program into the simulator output-file EQ3.out, \/\/ Write script outputs to this file compare-to EQ3.cmp, \/\/ Compare script outputs to this file output-list a b out; \/\/ Each subsequent output command should \/\/ print the values of the variables \/\/ a, b, and out set a %B000, set b %B000, eval, output; set a %B111, set b %B111, eval, output; set a %B111, set b %B000, eval, output; set a %B000, set b %B111, eval, output; set a %B001, set b %B000, eval, output; \/\/ Since the chip has two 3-bit inputs, \/\/ an exhaustive test requires 2^3*2^3=64 such scenarios. Figure B.1 Testing a chip on the hardware simulator. decimal. String values (%S pre\ufb01x) are used strictly for printing purposes and cannot be assigned to variables. String constants must be enclosed by \u2018\u2018 \u2019\u2019. The simulator clock (used in testing sequential chips only) emits a series of values denoted 0, 0\u00fe, 1, 1\u00fe, 2, 2\u00fe, 3, 3\u00fe, and so forth. The progression of these clock cycles (also called time units) is controlled by two script commands called tick and tock. A tick moves the clock value from t to t\u00fe, and a tock from t\u00fe to t \u00fe 1, bringing upon the next time unit. The current time unit is stored in a system variable called time. Script commands can access three types of variables: pins, variables of built-in chips, and the system variable time. Pins: Input, output, and internal pins of the simulated chip. For example, the command set in 0 sets the value of the pin whose name is in to 0. Variables of built-in chips: Exposed by the chip\u2019s external implementation. See sec- tion B.2.4 for more details. Time: The number of time units that elapsed since the simulation started running (read-only).","301 Test Scripting Language Figure B.2 Typical hardware simulation session, shown at the script\u2019s end. The loaded script is identical to EQ3.tst from \ufb01gure B.1, except that some white space was added to improve readability. B.2.3 Script Commands Command Syntax A script is a sequence of commands. Each command is termi- nated by a comma, a semicolon, or an exclamation mark. These terminators have the following semantics: m Comma (,): terminates a script command. m Semicolon (;): terminates a script command and a simulation step. A simulation step consists of one or more script commands. When the user instructs the simulator","302 Appendix B to \u2018\u2018single-step\u2019\u2019 via the simulator\u2019s GUI, the simulator executes the script from the current command until a semicolon is reached, at which point the simulation is paused. m Exclamation mark (!): terminates a script command and stops the script execu- tion. The user can later resume the script execution from that point onward. This option is typically used to facilitate interactive debugging. It is convenient to organize the script commands in two conceptual sections. \u2018\u2018Set up commands\u2019\u2019 are used to load \ufb01les and initialize global settings. \u2018\u2018Simulation com- mands\u2019\u2019 walk the simulator through a series of tests. Setup Commands load Xxx.hdl: Loads the HDL program stored in Xxx.hdl into the simulator. The \ufb01le name must include the .hdl extension and must not include a path speci\ufb01- cation. The simulator will try to load the \ufb01le from the current directory, and, failing that, from the simulator\u2019s builtIn directory, as described in section A.3. output-file Xxx.out: Instructs the simulator to write further output to the named \ufb01le, which must include an .out extension. The output \ufb01le will be created in the current directory. output-list v1; v2; . . . : Instructs the simulator what to write to the output \ufb01le in every subsequent output command in this script (until the next output-list command, if any). Each value in the list is a variable name followed by a format- ting speci\ufb01cation. The command also produces a single header line consisting of the variable names. Each item v in the output-list has the syntax variable format padL.len.padR. This directive instructs the simulator to write padL spaces, then the current variable value in the speci\ufb01ed format using len columns, then padR spaces, then the divider symbol \u2018\u2018|\u2019\u2019. Format can be either %B (binary), %X (hexa), %D (deci- mal) or %S (string). The default format speci\ufb01cation is %B1.1.1. For example, the CPU.hdl chip of the Hack platform has an input pin named reset, an output pin named pc (among others), and a chip part named DRegister (among others). If we want to track the values of these variables during the chip\u2019s execution, we can use something like the following command: Output-list time%S1.5.1 \/\/ System variable reset%B2.1.2 \/\/ Input pin of the chip pc%D2.3.1 \/\/ Output pin of the chip DRegister[] %X3.4.4 \/\/ State of this built-in part","303 Test Scripting Language (Sate variables of built-in chips are explained here.) This command may produce the following output (after two subsequent output commands): | time |reset| pc |DRegister[]| | 20+ | 0 | 21 | FFFF | | 21 | 0 | 22 | FFFF | compare-to Xxx.cmp: Instructs the simulator that each subsequent output line should be compared to its corresponding line in the speci\ufb01ed comparison \ufb01le (which must include the .cmp extension). If any two lines are not the same, the simulator displays an error message and halts the script execution. The compare \ufb01le is assumed to be present in the current directory. Simulation Commands set variable value: Assigns the value to the variable. The variable is either a pin or an internal variable of the simulated chip or one of its chip parts. The widths of the value and the variable must be compatible. For example, if x is a 16-bit pin and y is a 1-bit pin, then set x 153 is valid whereas set y 153 will yield an error and halt the simulation. eval: Instructs the simulator to apply the chip logic to the current values of the input pins and compute the resulting output values. output: This command causes the simulator to go through the following logic: 1. Get the current values of all the variables listed in the last output-list command. 2. Create an output line using the format speci\ufb01ed in the last output-list command. 3. Write the output line to the output \ufb01le. 4. (if a compare \ufb01le has been previously declared via the compare-to command): If the output line differs from the current line of the compare \ufb01le, display an error message and stop the script\u2019s execution. 5. Advance the line cursors of the output \ufb01le and the compare \ufb01le. tick: Ends the \ufb01rst phase of the current time unit (clock cycle). tock: Ends the second phase of the current time unit and embarks on the \ufb01rst phase of the next time unit. repeat num {commands}: Instructs the simulator to repeat the commands enclosed by the curly brackets num times. If num is omitted, the simulator repeats the com- mands until the simulation has been stopped for some reason.","304 Appendix B while Boolean-condition {commands}: Instructs the simulator to repeat the com- mands enclosed in the curly brackets as long as the Boolean-condition is true. The condition is of the form x op y where x and y are either constants or variable names and op is one of the following: =, >, <, >=, <=, <>. If x and y are strings, op can be either = or <>. echo text: Instructs the simulator to display the text string in the status line (which is part of the simulator GUI). The text must be enclosed by \u2018\u2018 \u2019\u2019. clear-echo: Instructs the simulator to clear the status line. breakpoint variable value: Instructs the simulator to compare the value of the speci\ufb01ed variable to the speci\ufb01ed value. The comparison is performed after the exe- cution of each script command. If the variable contains the speci\ufb01ed value, the exe- cution halts and a message is displayed. Otherwise, the execution continues normally. clear-breakpoints: Clears all the previously de\ufb01ned breakpoints. built-in-chip method argument(s): External implementations of built-in chips can expose methods that perform chip-speci\ufb01c operations. The syntax of the allowable method calls varies from one built-in chip to another and is documented next. B.2.4 Variables and Methods of Built-In Chips The logic of a chip can be implemented by either an HDL program or by a high-level programming language, in which case the chip is said to be \u2018\u2018built-in\u2019\u2019 and \u2018\u2018exter- nally implemented.\u2019\u2019 External implementations of built-in chips can facilitate access to the chip\u2019s state via the syntax chipName[varName], where varName is an imple- mentation-speci\ufb01c variable that should be documented in the chip API. The APIs of all the built-in chips supplied with the book (as part of the Hack computer platform) are shown in \ufb01gure B.3. For example, consider the command set RAM16K[1017] 15. If RAM16K is the currently simulated chip or an internal part of the currently simulated chip, this command will set its memory location number 1017 to the 2\u2019s complement binary value of 15. Further, since the built-in RAM16K chip happens to have GUI side effects, the new value will also be displayed in the chip\u2019s visual image. If a built-in chip maintains a single-valued internal state, the current value of the state can be accessed through the notation chipName[]. If the internal state is a vector, the notation chipName[i] is used. For example, when simulating the built-in Register chip, one can write script commands like set Register[] 135. This com- mand sets the internal state of the chip to the 2\u2019s complement binary value of 135; in","305 Test Scripting Language Chip name Exposed variables Data type\/range Methods load Xxx.hack\/Xxx.asm Register Register[] 16-bit (-32768. . .32767) ARegister ARegister[] 16-bit DRegister DRegister[] 16-bit PC PC[] 15-bit (0. .32767) RAM8 RAM8[0..7] Each entry is 16-bit RAM64 RAM64[0..63] RAM512 RAM512[0..511] \\\" RAM4K RAM4K[0..4095] \\\" RAM16K RAM16K[0..16383] \\\" ROM32K ROM32K[0..32767] \\\" Screen Screen[0..16383] \\\" Keyboard Keyboard[] \\\" 16-bit, read-only Figure B.3 API of all the built-in chips supplied with the book. the next time unit, the Register chip will commit to this value and its output will start emitting it. Built-in chips can also expose implementation-speci\ufb01c methods that extend the simulator\u2019s commands repertoire. For example, in the Hack computer, programs re- side in an instruction memory unit implemented by a chip named ROM32K. Before one runs a machine language program on this computer, one must \ufb01rst load a pro- gram into this chip. In order to facilitate this service, our built-in implementation of ROM32K features a load \ufb01le name method, referring to a text \ufb01le that, hopefully, contains machine language instructions. This chip-speci\ufb01c method can be accessed by a test script via commands like ROM32K load Myprog.hack. In the chip set supplied with the book, this is the only method supported by any of the built-in chips. B.2.5 Ending Example We end this section with a relatively complex test script, designed to test the topmost Computer chip of the Hack platform. One way to test the Computer chip is to load a machine language program into it and monitor selected values as the computer exe- cutes the program, one instruction at a time. For example, we wrote a program that (hopefully) computes the maximum of RAM[0] and RAM[1] and writes the result to RAM[2]. The machine language version of this program is stored in the text \ufb01le Max.hack. Note that at the very low level in which we operate, if such a program","306 Appendix B does not run properly it may be either because the program is buggy, or the hard- ware is buggy (and, for completeness, it may also be that the test script or the hard- ware simulator are buggy). For simplicity, let us assume that everything is error-free, except, possibly, for the tested Computer chip. To test the Computer chip using the Max.hack program, we wrote a test script called ComputerMax.tst. This script loads Computer.hdl into the hardware simu- lator and then loads the Max.hack program into its ROM32K chip part. A reason- able way to check if the chip works properly is as follows: put some values in RAM[0] and RAM[1], reset the computer, run the clock, and inspect RAM[2]. This, in a nutshell, is what the script in \ufb01gure B.4 is designed to do. How can we tell that fourteen clock cycles are suf\ufb01cient for executing this pro- gram? This can be found by trial and error, starting with a large value and watching the computer\u2019s outputs stabilizing after a while, or by analyzing the run-time behav- ior of the currently loaded program. B.2.6 Default Script The simulator\u2019s GUI buttons (single step, run, stop, reset) don\u2019t control the loaded chip. Rather, they control the progression of the loaded script, which controls the loaded chip\u2019s operation. Thus, there is a question of what to do if the user has loaded a chip directly into the simulator without loading a script \ufb01rst. In such cases, the simulator uses the following default script: \/\/ Default script of the hardware simulator repeat { tick, tock; } B.3 Testing Machine Language Programs on the CPU Emulator The CPU emulator supplied with the book is designed for testing and simulating the execution of binary programs on the Hack computer platform described in chapter 5. The tested programs can be written in either the native Hack code or the assembly language described in chapter 4. In the latter case, the simulator translates the loaded code into binary on the \ufb02y, as part of the \u2018\u2018load program\u2019\u2019 operation.","307 Test Scripting Language \/* ComputerMax.tst script. The max.hack program should compute the maximum of RAM[0] and RAM[1] and write the result in RAM[2]. *\/ \/\/ Load the Computer chip and set up for the simulation load Computer.hdl, output-file Computer.out, compare-to ComputerMax.cmp, output-list RAM16K[0] RAM16K[1] RAM16K[2]; \/\/ Load the Max.hack program into the ROM32K chip part ROM32K load Max.hack, \/\/ Set the first 2 cells of the RAM16K chip part to some test values set RAM16K[0] 3, set RAM16K[1] 5, output; \/\/ Run the clock enough cycles to complete the program\u2019s execution repeat 14 { tick, tock, output; } \/\/ Reset the Computer set reset 1, tick, \/\/ Run the clock in order to commit the Program tock, \/\/ Counter (PC, a sequential chip) to the new reset value output; \/\/ Now re-run the program with different test values. set reset 0, \/\/ \\\"De-reset\\\" the computer (committed in next tick-tock) set RAM16K[0] 23456, set RAM16K[1] 12345, output; repeat 14 { tick, tock, output; } Figure B.4 Testing the topmost Computer chip.","308 Appendix B As a convention, a script that tests a machine language program Xxx.hack or Xxx.asm is called Xxx.tst. As usual, the simulation involves four \ufb01les: the test script itself (Xxx.tst), the tested program (Xxx.hack or Xxx.asm), an optional output \ufb01le (Xxx.out) and an optional compare \ufb01le (Xxx.cmp). All these \ufb01les must reside in the same directory. This directory can be conveniently named xxx. For more information about \ufb01le structure and recommended usage, see section B.1. B.3.1 Example Consider the multiplication program Mult.hack, designed to effect RAM[2] \u00bc RAM[0]*RAM[1]. A reasonable way to test this program is to put some values in RAM[0] and RAM[1], run the program, and inspect RAM[2]. This logic is carried out in \ufb01gure B.5. \/\/ Load the program and set up for the simulation load Mult.hack, output-file Mult.out, compare-to Mult.cmp, output-list RAM[2]%D2.6.2; \/\/ Set the first 2 cells of the RAM to some test values set RAM[0] 2, set RAM[1] 5; \/\/ Run the clock enough cycles to complete the program's execution repeat 20 { ticktock; } output; \/\/ Re-run the same program with different test values set PC 0, set RAM[0] 8, set RAM[1] 7; repeat 50 { \/\/ Mult.hack is based on repetitive addition, so ticktock; \/\/ greater multiplicands require more clock cycles } output; Figure B.5 Testing a machine language program on the CPU emulator.","309 Test Scripting Language B.3.2 Variables The CPU emulator, which is hardware-speci\ufb01c, recognizes a set of variables related to internal components of the Hack platform. In particular, scripting commands running on the CPU emulator can access the following elements: A: value of the address register (unsigned 15-bit); D: value of the data register (16-bit); PC: value of the Program Counter register (unsigned 15-bit); RAM[i]: value of RAM location i (16-bit); time: Number of time units (also called clock cycles, or ticktocks) that elapsed since the simulation started (read-only). B.3.3 Commands The CPU emulator supports all the commands described in section B.2.3, except for the following changes: load program: Here program is either Xxx.hack or Xxx.asm. This command loads a machine language program (to be tested) into the simulated instruction memory. If the program is written in assembly, it is translated into binary on the \ufb02y. eval: Not applicable; built-in-chip method argument(s): Not applicable; ticktock: This command is used instead of tick and tock. Each ticktock advances the clock one time unit (cycle). B.3.4 Default Script The CPU emulator\u2019s GUI buttons (single step, run, stop, reset) don\u2019t control the loaded program. Rather, they control the progression of the loaded script, which controls the program\u2019s operation. Thus, there is a question of what to do if the user has loaded a program directly into the CPU emulator without loading a script \ufb01rst. In such cases, the emulator uses the following default script: \/\/ Default script of the CPU emulator repeat { ticktock; }","310 Appendix B B.4 Testing VM Programs on the VM Emulator Chapters 7\u20138 describe a virtual machine model and specify a VM implementation on the Hack platform. The VM emulator supplied with the book is an alternative VM implementation that uses Java to run VM programs, visualize their operations, and display the states of the effected virtual memory segments. Recall that a VM program consists of one or more .vm \ufb01les. Thus, the simulation of a VM program involves four elements: the test script (Xxx.tst), the tested pro- gram (a single Xxx.vm \ufb01le or an Xxx directory containing one or more .vm \ufb01les), an optional output \ufb01le (Xxx.out) and an optional compare \ufb01le (Xxx.cmp). All these \ufb01les must reside in the same directory, which can be conveniently named xxx. For more information about \ufb01le structure and recommended usage, see section B.1. Chapter 7 provides essential information about the virtual machine architecture, without which the discussion below will not make much sense. Startup Code A VM program is normally assumed to contain at least two func- tions: Main.main and Sys.init. When the VM translator translates a VM pro- gram, it generates machine language code that sets the stack pointer to 256 and then calls the Sys.init function, which then calls Main.main. In a similar fashion, when the VM emulator is instructed to execute a VM program (collection of one or more VM functions), it is programmed to start running the Sys.init function, which is assumed to exist somewhere in the loaded VM code. If a Sys.init function is not found, the emulator is programmed to start executing the \ufb01rst command in the loaded VM code. The latter convention was added to the emulator in order to assist the gradual development of the VM implementation, which spans two chapters in the book. In chapter 7, we build only the part of the VM implementation that deals with pop, push, and arithmetic commands, without getting into subroutine calling commands. Thus, the test programs associated with Project 7 consist of \u2018\u2018raw\u2019\u2019 VM commands without the typical function\/return wrapping. Since we wish to allow informal experimentation with such commands, we gave the VM emulator the ability to exe- cute \u2018\u2018raw\u2019\u2019 VM code which is neither properly initialized nor properly packaged in a function structure. Virtual Memory Segments In the process of simulating the virtual machine\u2019s oper- ations, the VM emulator manages the virtual memory segments of the Hack VM (argument, local, etc.). These segments must be allocated to the host RAM\u2014a","311 Test Scripting Language task that the emulator normally carries out as a side effect of simulating the execu- tion of call, function, and return commands. This means that when simulating \u2018\u2018raw\u2019\u2019 VM code that contains no subroutine calling commands, we must force the VM emulator to explicitly anchor the virtual segments in the RAM\u2014at least those segments mentioned in the current code. Conveniently, this initialization can be ac- complished by script commands that manipulate the pointers controlling the base RAM addresses of the virtual segments. Using these script commands, we can effec- tively put the virtual segments in selected areas in the host RAM. B.4.1 Example The FibonacciSeries.vm \ufb01le contains a series of VM commands that compute the \ufb01rst n elements of the Fibonacci series. The code is designed to operate on two arguments: the value of n and the starting memory address in which the computed elements should be stored. The script in \ufb01gure B.6 is designed to test this program using the actual arguments 6 and 4000. B.4.2 Variables Scripting commands running on the VM emulator can access the following ele- ments: Contents of Virtual Memory Segments local[i]: value of the i-th element of the local segment; argument[i]: value of the i-th element of the argument segment; this[i]: value of the i-th element of the this segment; that[i]: value of the i-th element of the that segment; temp[i]: value of the i-th element of the temp segment. Pointers to Virtual Memory Segments local: base address of the local segment in the RAM; argument: base address of the argument segment in the RAM; this: base address of the this segment in the RAM; that: base address of the that segment in the RAM.","312 Appendix B \/* The FibonacciSeries.vm file contains a series of VM commands that compute the first n Fibonacci numbers. The program's code contains no function\/call\/return commands, and thus the VM emulator must be forced to initialize the virtual memory segments used by the code explicitly. *\/ \/\/ Load the program and set up for the simulation load FibonacciSeries.vm, output-file FibonacciSeries.out, compare-to FibonacciSeries.cmp, output-list RAM[4000]%D1.6.2 RAM[4001]%D1.6.2 RAM[4002]%D1.6.2 RAM[4003]%D1.6.2 RAM[4004]%D1.6.2 RAM[4005]%D1.6.2; \/\/ Initialize the stack and the argument and local segments. set SP 256, \/\/ Stack pointer (stack begins in RAM[256]) set local 300, \/\/ Base the local segment in some RAM location set argument 400; \/\/ Base the argument segment in some RAM loc. \/\/ Set the arguments to two test values set argument[0] 6, \/\/ n=6 set argument[1] 4000; \/\/ Put the series at RAM[4000] and onward \/\/ Execute enough VM steps to complete the program's execution repeat 140 { vmstep; } output; Figure B.6 Testing a VM program on the VM emulator. Implementation-Speci\ufb01c Variables RAM[i]: value of the i-th RAM location; SP: value of the stack pointer; currentFunction: name of the currently executing function (read only). line: contains a string of the form: current-function-name.line-index-in-function (read only). For example, when execution reaches the third line of the function Sys.init, the line variable contains \u2018\u2018Sys.init.3\u2019\u2019. This is a useful means for setting breakpoints in selected locations in the loaded VM program.","313 Test Scripting Language B.4.3 Commands The VM emulator supports all the commands described in section B.2.3, except for the following changes: load source: Here source is either Xxx.vm, the name of a \ufb01le containing one or more VM functions, or a series of \u2018\u2018raw\u2019\u2019 VM commands, or Xxx, the name of a directory containing one or more .vm \ufb01les (in which case all of them are loaded). If the .vm \ufb01les are located in the current directory, the source argument can be omitted. tick\/tock: Not applicable. vmstep: Simulates the execution of a single VM command from the VM program, and advances to the next command in the code. B.4.4 Default Script The VM emulator\u2019s GUI buttons (single step, run, stop, reset) don\u2019t control the loaded VM code. Rather, they control the progression of the loaded script, which controls the code\u2019s operation. Thus, there is a question of what to do if the user has loaded a program directly into the VM emulator without loading a script \ufb01rst. In such cases, the emulator uses the following default script: \/\/ Default script of the VM emulator repeat { vmstep; }","Index Abstraction, 6, 263 Application Program Interface (API) implementation paradigm, xi\u2013xii notation, 19 modules and, 2\u20133 Adder gates, 29\u201339 Architecture, x, 79, 99\u2013101 Addresses, 45, 104 bottom-up, 3\u20134 direct addressing, 60\u201361 chip set, 2 indirect addressing, 61 CPU and, 82\u201383 machine language and, 60\u201361, 63 Hack, 5\u20136, 85\u201398 mapping and, 84\u201391, 137\u2013141 hardware, 2 memory and, 81\u201382 (see also Memory) I\/O and, 84\u201385 program size limits and, 106 Jack, 175\u2013176 (see also Jack) registers and, 45, 83\u201386 machine language and, 106\u2013107 subroutines and, 153\u2013159 memory and, 81\u201382 symbol table and, 105 modi\ufb01cations and, 277\u2013279 VM-Hack mapping and, 139\u2013143, 161\u2013168 modules and, 2\u20133 Addressing instruction (A-instruction), 64\u2013 optimization and, 80 65, 108\u2013110, 115 registers and, 83\u201384 Algorithms sequential chip hierarchy and, 47\u201350 ef\ufb01ciency and, 249, 272\u2013273 standards and, 84 graphics and, 257\u2013263 stored program concept and, 80 mathematics and, 248\u2013252 top-down, 3 memory management and, 252\u2013256 VM and, 121\u2013151 (see also Virtual operating systems and, 272\u2013273 (see also Machine) Operating systems) von Neumann, 62, 79\u201381, 85 runtime and, 249 Aristotle, 6 syntax and, 250 Arithmetic addition, 37 ALU. See Arithmetic Logic Unit Arithmetic Logic Unit (ALU), 2, 6, 39 Analysis-synthesis paradigm, 223 Boolean arithmetic and, 29, 32, 35\u201338 And function, 8\u20139, 20 combinational chips and, 46\u201347 implementation of, 26 CPU and, x, 82\u201383, 94 multi-bit versions of, 21\u201323 description of, 35\u201338 Hack and, 86","316 Index Arithmetic Logic Unit (ALU) (cont.) radix complement method, 31 operating systems and, 248\u2013249 signed binary numbers, 31\u201332 visualized chip operations and, 292 stack processing and, 126\u2013127 Boolean logic Arrays, 81 abstraction of, 11 data translation and, 224\u2013231 algebra and, 8\u201310 Jack and, 175, 184\u2013185, 191, 265, 269 canonical representation, 9 operating systems and, 256, 265, 269 conditional execution, 62 stack processing and, 124\u2013127 gates and, 8, 11\u201313 variable-length, 256 hardware construction and, 13\u201314 Virtual Machine (VM) and, 137 HDL and, 14\u201317 machine language and, 57\u201377 ASCII code, 71, 89, 252 repetition, 61\u201362 Assembler, x, 5, 71\u201372, 75\u201376, 118\u2013120, 277 subroutine calling, 62 truth tables, 8 hash table, 115 two-input functions, 9\u201310 implementation of, 112\u2013116 Bootstrap code, 165 labels and, 105 Buses, 21\u201322, 286\u2013287 machine language speci\ufb01cation and, 107 macros and, 117 C#, 4\u20135, 112, 121, 147, 169 mnemonics and, 108, 114 Jack and, 174, 196 program size limits and, 106 symbols and, 60, 104\u2013106, 110\u2013111, 114\u2013 C++, 112, 147, 253 116, 143, 164 Canonical representation, 9 syntax and, 104, 107\u2013110 Case conventions, 108 test scripts and, 103\u2013104 Central Processing Unit (CPU), 6, 29, 59 as translator program, 104\u2013107, 163\u2013164 variables and, 105 ALU and, 82\u201383, 94 architecture and, 82\u201383 Best-\ufb01t, 254 control unit and, 82\u201383 Big-Oh notation (O(n)), 249 description of, 82\u201383 Binary code, 5, 108. See also Boolean logic emulators and, 306\u2013309 Hack and, 62\u201363, 85\u201396 code generation and, 223\u2013246 instruction memory and, 82 graphics and, 257\u2013263 program counter and, 84 Jack and, 174 registers and, 82 Binary search, 251 testing and, 306\u2013309 Bitmaps, 259\u2013263, 269 von Neumann architecture and, 81 Bit shifting, 60 Character output, 259\u2013263, 269 Bit-wise negation, 60 Chips, 2. See also Gates Boolean arithmetic, x adder, 29\u201339 addition, 30 API speci\ufb01cation and, 19 algebra and, 8\u201310 Boolean logic and, 7\u201328 ALU and, 29, 32, 35\u201338 built-in, 287\u2013288, 293, 296, 304\u2013305 binary numbers and, 30\u201332 buses and, 286\u2013287 CPU and, 29 clocks and, 289\u2013291 least signi\ufb01cant bits (LSB), 30 combinational, 41, 46\u201347 memory and, 42\u201347 most signi\ufb01cant bits (MSB), 30","317 Index connections and, 285\u2013286 mapping and, 137\u2013141 cost and, 14\u201315 memory allocation and, 234 description of, 11 nested subroutine calling and, 153 ef\ufb01ciency and, 288 parsing and, 203\u2013207 feedback loops and, 291\u2013292 p-code and, 123, 146 Hack platform and, 85\u201391 semantics and, 199 hardware simulator and, 283\u2013284 syntax analysis and, 199\u2013221, 237\u2013241 HDL and, 14\u201317, 281\u2013296 VM and, 122\u2013127, 161\u2013168, 233\u2013235 (see incrementer, 33\u201339 maintaining state and, 41\u201342 also Virtual Machine) pins and, 284\u2013286 XML and, 199\u2013201, 211\u2013218, 221 RAM, 86 Complex Instruction Set Computing (CISC), ROM, 85 98 sequential, 41\u201355, 289\u2013292 Composite gates, 11\u201313 simulators and, 299\u2013306 Compute instruction (C-instruction), 66\u201369, testing and, 297\u2013313 86, 108\u2013110, 115 visualized operations for, 288, 292\u2013296 Computers. See also Architecture Clocks, 41, 48, 289\u2013290 ALU and, 29 (see also Arithmetic Logic feedback loops and, 291\u2013292 Unit) memory and, 42, 52\u201354 Boolean abstraction and, 11 Code generation bootstrap code and, 165 commands translation and, 231\u2013232 CPU and, 29 (see also Central Processing data translation and, 224\u2013231 Unit) operating systems and, 272 (see also dedicated, 97 Operating systems) emulators and, 121\u2013122 registers and, 223\u2013224 general-purpose, 97\u201398 syntax analysis and, 237\u2013241 HDL and, 14\u201317 (see also Hardware virtual machines and, 224 Description Language) Combinational logic. See Boolean arithmetic machine language and, 57\u201377 Commands translation, 231\u2013232 memory and, 81\u201382 Common Language Runtime (CLR), 123, program \ufb02ow and, 153\u2013159 146\u2013147 stored program concept and, 79\u201380 Communications, 279 Conditional execution, 62 Compare \ufb01le, 18 Conditional jump, 62 Compilers, ix\u2013x, 2, 5\u20136, 17, 103, 112 Constants, 181\u2013182 abstraction and, 175\u2013179 Control logic, 94\u201395 analysis-synthesis paradigm and, 223 Control unit, 82\u201383 code generation and, 223\u2013246 Converters. See Not function description of, 199\u2013201 Counters, x, 45, 47\u201348, 50, 52, 84, 95 grammars and, 203, 206\u2013207 CPU. See Central Processing Unit Hack and, 133\u2013134 (see also Hack) Cycles, 42 high-level language and, 146\u2013147 Jack and, 133\u2013134, 174, 193\u2013195 (see also Data \ufb02ip-\ufb02op (DFF) Jack) clocked chips and, 290\u2013291 lexical analysis and, 202, 208 implementation of, 50\u201351 sequential logic and, 42\u201348","318 Index Data races, 46 Functions. See also Boolean logic Debugging, 75 And, 8\u20139, 20\u201323, 26 Decoding, 94\u201396 assembly language symbols and, 164 Defragmantation, 254\u2013256 bootstrap code and, 165 Demultiplexors, 21, 24\u201326 calling commands and, 159\u2013164 Design. See also Architecture compilers and, 233\u2013235 (see also Compilers) Jack and, 174\u2013175, 190\u2013193 (see also Jack) alternative elements for, 277 Nand, 2, 7, 10, 19, 27 Boolean logic and, 7\u201328 Nor, 2, 10 bottom-up, 3\u20134 Not, 8\u20139, 26 cost and, 14\u201315 Or, 8\u20139, 20\u201326 digital, 27 subroutines, 62, 112, 153\u2013161, 181\u2013190, gate logic and, 11\u201313 195, 209, 234\u2013235 HDL and, 14\u201317 (see also Hardware symbolic names and, 160 Description Language) testing and, 297\u2013313 modi\ufb01cations and, 277\u2013279 VM-Hack mapping and, 139\u2013143, 161\u2013168 standards and, 84 Xor, 10, 20\u201323, 26 testing and, 297\u2013313 top-down, 3 Gates, ix\u2013x, xvi, 4, 6 Device driver, 256\u2013257 adder, 29\u201339 Direct addressing, 60\u201361 And, 8\u20139, 20\u201323, 26 Division, 250\u2013251 API speci\ufb01cation and, 19 DOS, 272 Boolean arithmetic and, 29\u201340 Boolean logic and, 7\u201328 Emulators, 121\u2013122 built-in chips and, 287\u2013288 Hack and, 76\u201377 buses and, 21\u201322, 286\u2013287 testing and, 297, 306\u2013313 composite, 11\u201313 construction of, 13\u201314 Equivalence function, 10 demultiplexors and, 21 Execute cycle, 86, 98 \ufb02ip-\ufb02ops and, 41\u201354, 287\u2013291 Expression evaluation, 187\u2013188, 231\u2013232 HDL and, 14\u201317 (see also Hardware Feedback loops, 46, 52\u201353, 291\u2013292 Description Language) Fetching, 86, 95\u201396, 98 interfaces and, 12\u201313 File formats, 107\u2013110 memory and, 42\u201347 First-\ufb01t, 254 multi-bit versions of, 21\u201325 Flip-\ufb02ops, x, 41, 52\u201354, 287\u2013288 Nand, 2, 7, 10, 19, 27 Nor, 2, 10 clocked chips and, 290\u2013291 Not, 8\u20139, 26 data, 42\u201351, 290\u2013291 Or, 8\u20139, 20\u201326 implementation of, 50\u201351 primitive, 11\u201313, 25\u201326 memory and, 42 sequential, 41\u201355 Flow control, 231\u2013232 speci\ufb01cation, 17\u201325 Formal languages, 201\u2013202 switching devices and, 2 Fragmentation, 254, 256 Xor, 10, 20\u201323, 26 FreeList, 254, 256 Full-adder chip, 32\u201333, 38","319 Index Goto operation, 95, 155 Boolean logic and, 8\u201328 Grammars chips and, 85, 293\u2013296 (see also Chips; Jack and, 203, 207\u2013215 Gates) parsing and, 203\u2013207 keyboard, 71 syntax analyzer and, 207\u2013213 machine language and, 57\u201377 Graphical User Interface (GUI), 247, 283, memory and, 81\u201382 modi\ufb01cations and, 278\u2013279 288\u2013290 operating systems and, 247\u2013276 testing and, 297\u2013313 RAM, 42\u201347 visualized chip operations and, 292\u2013296 screen, 70 Graphics, 98 sequential chips and, 41\u201355 character output, 259\u2013261 simulators and, 299\u2013306 circle drawing, 259 stored program concept and, 79\u201380 keyboard handling and, 261\u2013263 Hardware Description Language (HDL), line drawing, 257\u2013258 x\u2013xiii, 5\u20136, 93, 278 multiplication and, 258\u2013259 API notation and, 282 pixel drawing, 257 case sensitivity and, 283 GUI. See Graphical User Interface chip logic and, 17\u201325, 281\u2013296 compare \ufb01le, 18 Hack, 5, 35, 79 description of, 281 address instruction format and, 64\u201365, 85\u2013 ef\ufb01ciency and, 288 86 hardware simulator and, 14, 17\u201325, 283\u2013 assembler, 75\u201376, 103\u2013120 284 built-in chips and, 293, 296 header section, 15 case conventions and, 108 identi\ufb01er naming and, 283 case sensitivity, 75 interfaces and, 15\u201316 C-instruction, 66\u201369 logic building and, 39 CPU and, 62\u201363, 76\u201377, 85\u201396 parts section, 15 destination speci\ufb01cation and, 66\u201368 statement representation, 15 \ufb01le formats and, 71\u201372, 107\u2013110 technical references for, 281\u2013296 graphics card and, 98 testing and, 16\u201317 input\/output (I\/O) handling and, 70\u201371, visualized chip operations and, 292\u2013296 98 Hardware simulator, 14, 283\u2013284 instructions and, 108\u2013110 chip speci\ufb01cations and, 17\u201325 Internet and, 279 Hash tables, 115, 226 jump speci\ufb01cation, 68\u201369 HDL. See Hardware Description Language memory and, 63, 87\u201391, 96 Heap, 132\u2013133 modi\ufb01cations and, 278\u2013279 High-level language, 4\u20136 platform description, 62\u201364, 85\u201398 Jack, 173 (see also Jack) symbols, 69\u201370 operating systems and, 248 syntax, 71\u201373, 107\u2013110 program \ufb02ow and, 153\u2013159 VM mapping and, 139\u2013143, 161\u2013168 subroutines and, 62, 112, 153\u2013161, 181\u2013190, Half-adder chip, 32\u201333, 38 195, 209, 234\u2013235 Hardware, ix\u2013x, 4\u20136. See also Input\/output VM-Hack mapping and, 139\u2013143, 161\u2013168 architecture of, 2, 79\u2013101","320 Index If-goto destination, 155 array handling, 175, 184\u2013185, 191, 265, 269 If-x-then-y function, 10 binary code and, 174 Immediate addressing, 61 classes and, 175\u2013183, 187\u2013193, 208, 248, Incrementer chip, 33\u201339 Indirect addressing, 61 263\u2013273 Inheritance, 195\u2013196, 241\u2013242 code generation and, 223\u2013246 Input\/output (I\/O), x constants, 181\u2013182 constructor for, 234\u2013235 characters and, 259\u2013263 data types and, 183\u2013185 device driver, 256\u2013257 evaluation order, 188 graphics, 257\u2013263 expression evaluation and, 187\u2013188, 231\u2013 Hack and, 62\u201377, 70\u201371, 98 keyboards, 261\u2013263, 266 232 operating systems and, 256\u2013270 \ufb02ow control and, 231\u2013232 screens, 265\u2013266 generic statements, 187 standards and, 84 grammar and, 203, 207\u2013215 Instructions, 116 identi\ufb01ers, 181\u2013182 addresses, 64\u201365, 108\u2013110 (see also inheritance and, 195\u2013196, 241\u2013242 Addresses) I\/O and, 191\u2013193, 209\u2013215, 265\u2013266, 269\u2013 assembler and, 103\u2013120 CISC, 98 270 compilers and, 122\u2013127 (see also Compilers) Java and, 174, 183, 196 compute, 66\u201369, 108\u2013110 keyboards and, 192\u2013193, 266, 270 decoding, 94\u201396 lexical analysis and, 202, 208 execution, 94\u201396 linked list implementation, 179\u2013180 fetching, 86, 95\u201396, 98 Main.main function, 174\u2013175 labels and, 105 memory and, 193, 266\u2013267, 270\u2013271 macros and, 117 modi\ufb01cations and, 277\u2013278 memory and, 63, 82 as object-based language, 173, 195\u2013196, RISC, 98 stack processing and, 130 (see also Stack 199 processing) object handling and, 189\u2013190, 228\u2013231 subroutines and, 62, 112, 153\u2013161, 181\u2013190, operating system, 195, 197, 235, 253, 257\u2013 195, 209, 234\u2013235 symbolic vs. binary, 104 273 variables and, 105 operator priority, 188 Interfaces, 282, 284 parsing and, 200, 217, 221 HDL and, 15\u201316 program elements in, 133\u2013134 logic gates and, 12 rational numbers and, 175\u2013179 Intermediate language (IL), 123 reserved words, 181\u2013182 Internal pins, 15\u201316 screens and, 192, 265\u2013266, 269 simplicity of, 174 Jack, 1, 4\u20135, 147, 165, 169, 197 standard library of, 174, 190\u2013193, 196, 263 abstract data types and, 175\u2013179 strings and, 191, 264\u2013265, 268\u2013269 API notation and, 175\u2013176, 200, 215, 224 subroutines and, 181\u2013190, 195, 209, 234\u2013235 applications writing, 193\u2013195 symbols and, 181\u2013182, 238\u2013239 syntax and, 181\u2013182, 187, 207\u2013221, 237\u2013 241 tokenizing and, 181, 202, 205, 208, 214\u2013215, 219\u2013221","321 Index type conversions, 183, 241 jumps, 61\u201362, 68\u201369, 96 variables and, 181\u2013187 stack processing and, 130 (see also Stack VM code and, 174, 233\u2013235, 240 processing) void methods and, 235 stored program concept and, 79\u201380 white space, 181\u2013182 XML and, 199\u2013201, 211\u2013218, 221 Machine language, x Java, 17, 247, 253, 277 abstraction and, 81 assembler and, 112 addressing and, 60\u201361, 63 built-in chips and, 293, 296 assembler and, 103\u2013120 Jack and, 174, 183, 196 binary codes and, 59\u201360 stack arithmetic and, 122, 134 commands and, 60\u201362 standard libraries, 147 compilers and, 122\u2013127 (see also Compilers) VM and, 122, 134, 169 conditional execution, 62 Java Runtime Environment, 123, 146 Hack, 62\u201377 Java Virtual Machine (JVM), 121, 123, instruction memory and, 82 146 labels and, 105 Jump, 109\u2013110, 114 memory and, 58\u201362 nested subroutine calling and, 153\u2013159 mnemonic symbols, 59 speci\ufb01cation, 61\u201362, 68\u201369, 96 processor and, 59 program size limits and, 106 Keyboard input, 71, 84, 86, 89, 96 registers and, 59 Jack and, 192\u2013193, 266, 270 repetition and, 61\u201362 operating systems and, 266, 270 subroutine calling, 62 string reading and, 262\u2013263 symbolic vs. binary, 104 text handling and, 261\u2013263 syntax and, 60\u201362, 71\u201373, 104 visualized chip operations and, 292\u2013293 testing and, 306\u2013309 unconditional jump, 62 Labels, 70, 105, 110, 116, 155, 159 variables and, 105 Last-in-\ufb01rst-out (LIFO) storage model, 124, VM and, 122\u2013127 (see also Virtual Machine) 157 Macro commands, 117 Least signi\ufb01cant bits (LSB), 30 Mapping Lexical analysis, 202, 208 I\/O operations and, 84\u201391 XML and, 199\u2013201, 211\u2013218, 221 keyboard handling and, 262\u2013263 Lexical analysis (LEX) tool, 217 memory segments and, 142\u2013143 Line drawing, 257\u2013259 VM-to-Hack, 139\u2013143, 161\u2013168 Linked list, 179\u2013180 VM-to-Jack, 233\u2013235 Linux, xiii, 277 Memory, 2 Load command, 60 addresses and, 45, 91 (see also Addresses) Logic allocation and, 253\u2013254 Boolean, 7\u201328 (see also Boolean logic) arrays and, 227\u2013228 control logic and, 94\u201395 clocks and, 42, 52\u201354 decoding, 94\u201396 compilers and, 234 fetching, 95\u201396 dynamic allocation and, 252\u2013253 HDL and, 281\u2013296 \ufb02ip-\ufb02ops and, 42\u201354 instruction execution, 94\u201396","322 Index Memory (cont.) arrays and, 256, 265, 269 fragmentation and, 254, 256 classes and, 264\u2013271 graphics and, 257\u2013263 description of, 247 Hack and, 63, 87\u201391, 96 graphics and, 257\u2013263 implementation and, 50\u201352 hardware\/software gaps and, 247 improved allocation and, 254\u2013256 initialization and, 267 instruction, 63, 82 input\/output (I\/O) management, 256\u2013266, Jack and, 193, 266\u2013267, 270\u2013271 269\u2013270 machine language and, 58\u201362 Jack and, 195, 235, 263\u2013273 (see also Jack) mapped input\/output (I\/O) and, 84\u201391 mathematical operations and, 248\u2013252, 264, object handling and, 228\u2013231 268 operating systems and, 247, 252\u2013256, 266\u2013 memory and, 247, 252\u2013256, 266\u2013267, 270\u2013 267, 270\u2013271 271 RAM, 42\u201345, 49\u201350 (see also Random program size limits and, 106 access memory) screens and, 265\u2013266, 269\u2013270 registers and, 42\u201349 strings and, 252, 256, 264\u2013265, 268\u2013269 stored program concept and, 79\u201380 Sys and, 267, 271 subroutines and, 62, 112, 153\u2013161, 181\u2013190, Operator priority, 188 195, 209, 234\u2013235 Or function, 8\u20139, 20 testing and, 310\u2013311 implementation of, 26 variable locations and, 106 multi-bit versions of, 21\u201323 virtual segment mapping and, 142\u2013143 multi-way versions and, 23\u201325 visualized chip operations and, 292 Over\ufb02ow, 30 VM and, 127\u2013133 von Neumann architecture and, 81 Parsing, 2, 17, 60, 116 assembler and, 112\u2013114 Mnemonics, 59, 108, 114 compilers and, 217 (see also Compilers) Multi-bit bus, 286\u2013287 expression evaluation and, 187\u2013188, 231\u2013 Multiplexors, x, 20\u201326 232 Multiplication, 249\u2013250, 258\u2013259 grammar and, 203\u2013207 Multitasking, 247 Jack and, 200, 203\u2013207, 217, 221 programming and, 107 Nand function, 2, 7, 10, 19, 27 recursive descent, 204\u2013206 Negative numbers, 31\u201332 symbol-less, 114\u2013115 Nested subroutine calling, 153\u2013159 VM and, 144\u2013146, 168 .NET infrastructure, 122, 123, 146\u2013147 Network interface cards, 84 Pascal, 123, 146 Newton-Raphson method, 251 P-code, 123, 146 Non-terminals, 203, 211 Pins, 11, 15\u201316, 284\u2013286, 290, 300 Nor function, 2, 10 Pixel drawing, 257 Not function, 8\u20139, 26 Pointers, 69\u201370, 124, 131, 142, 161 Number base, 30 Pop operation, 124, 130\u2013132 Positive numbers, 31\u201332 Object types, 183\u2013184 Post\ufb01x notation, 231\u2013232 Operating systems, ix\u2013x, 4 Primitive gates, 11\u201313, 25\u201326 Program counter, 45, 84, 95 API notation and, 263, 267","323 Index Program \ufb02ow virtual, 69 assembly language symbols and, 164 visualized chip operations and, 292 bootstrap code and, 165 Reserved words, 181\u2013182 calling protocol and, 160\u2013161 Return address, 158 LIFO model and, 157 Right Polish Notation (RPN), 231\u2013232 nested subroutine calling and, 153\u2013159 Rogers, Carl, 1 VM, 129\u2013130, 133\u2013134, 153\u2013168 RPN. See Right Polish Notation Push operation, 124, 130\u2013132 Screen output, 70, 84, 86, 89, 96 Radix complement method, 31 characters and, 259\u2013263 RAM. See Random access memory graphics and, 257\u2013263 Random access memory (RAM), x, 6, 278\u2013 Jack and, 192, 265\u2013266, 269 operating systems and, 265\u2013266, 269\u2013270 279 resolution and, 257\u2013258 clocked chips and, 290\u2013291 visualized chip operations and, 292\u2013296 Hack platform and, 86, 96, 139\u2013143, 161\u2013 Segment index, 131\u2013132, 135 168 Selectors, 20 implementation of, 52 Semantics, 199. See also Symbols; Syntax memory management and, 253 data translation and, 224\u2013231 operating systems and, 270\u2013271 Sequential logic, 6 registers and, 49\u201350 chip hierarchy and, 47\u201350 sequential logic and, 42\u201347 clocks and, 289\u2013291 testing and, 304\u2013308, 311\u2013312 feedback loops and, 291\u2013292 VM and, 137\u2013143, 161\u2013168 \ufb02ip-\ufb02ops and, 41\u201354 Rational numbers, 175\u2013179 memory and, 42\u201347 Read-only memory (ROM) chips, 6, 85\u201386, time and, 45\u201347 91, 278\u2013279 Signed binary numbers, 31\u201332 Read\/write operations Simulators, 101 memory and, 42\u201347 testing and, 297, 299\u2013306 registers and, 48\u201349 Square root function, 251 Recursive descent parsing, 204\u2013206 Stack pointer, 124, 131, 142, 161 Reduced Instruction Set Computing (RISC), Stack processing, 122 98 arithmetic and, 126\u2013130 Registers, x, 2 bootstrap code and, 165 addresses and, 45, 83\u201386 heap structure and, 132\u2013133 API speci\ufb01cation and, 48\u201349 LIFO model and, 124, 157 architecture of, 83\u201384 memory and, 130\u2013133 CPU and, 82 model of, 124\u2013127 Hack and, 63\u201364, 69 nested subroutine calling and, 153\u2013159 implementation of, 52 pop operation, 124, 130\u2013132 machine language and, 59 push operation, 124, 130\u2013132 memory and, 42\u201349 subroutines and, 153\u2013159 RAM and, 49\u201350 VM-Hack mapping and, 139\u2013143, 161\u2013168 read-write operations and, 48\u201349 Standard language library, 4 testing and, 304\u2013305","324 Index Standard mapping, 141 GUI and, 297\u2013298 Store command, 60 machine language and, 306\u2013309 Stored program concept, 79\u201380 script commands and, 301\u2013304 Strings, 184 simulators and, 297, 299\u2013306 test scripts, 16\u201317, 103\u2013104 Jack and, 191, 264\u2013265, 268\u2013269 VM and, 310\u2013313 keyboard handling and, 262\u2013263 Text \ufb01les, 2 operating systems and, 252, 256, 264\u2013265, Time 268\u2013269 clocks, 41\u201354, 289\u2013292 Subroutines, 62, 112 counters, x, 45, 47\u201348, 50, 52, 84 calling protocol and, 160\u2013161 sequential logic, 6, 42\u201354, 289\u2013292 code generation and, 234\u2013235 testing and, 297\u2013313 functional commands and, 153\u2013159 Tokens, 181 Jack and, 181\u2013190, 195, 209, 234\u2013235 Jack tokenizing, 202, 205, 208, 214\u2013215, LIFO model and, 157 void, 235 219\u2013221, 237\u2013241 Switching technology, 2, 11 syntax analyzer and, 207\u2013213 Symbols Transistors, 2, 11 assembler and, 60, 104\u2013106, 110\u2013111, 114\u2013 Translator program, 163\u2013164 Truth tables, 8\u20139 116, 143, 164 Turing, Alan, 122 function calling and, 160 Turing machine, 80\u201381 Jack and, 181\u2013182 Two-input Boolean functions, 9\u201310 labels, 70, 105, 110, 116, 155, 159 2\u2019s complement method, 30 machine language and, 59\u201360, 69\u201370, 104 mnemomic, 59 Unconditional jump, 62 resolution and, 105\u2013106 Unix, 272 variables and, 105 Symbol tables, 103, 105, 115\u2013116, 243 Variables, 105, 116 data translation and, 225\u2013226 argument, 234 Jack and, 238\u2013239 \ufb01elds, 183, 185, 226, 227 Syntax, 5, 104 Jack and, 181\u2013187 expression evaluation and, 187\u2013188, 231\u2013 local, 183, 185\u2013186, 227, 234, 253 232 parameter, 183, 185\u2013186 formal languages and, 201\u2013202 scope and, 1, 225\u2013226 non-terminals and, 203, 211 static, 183, 185, 226\u2013227, 234, 253 RPN, 231\u2013232 semantics and, 199 Virtual Hardware Description Language terminals and, 203, 211 (VHDL), 14 testing and, 301\u2013304 XML and, 199\u2013201, 211\u2013218, 221 Virtual Machine (VM) advantages of, 121, 123\u2013124 Taylor series, 251 arithmetic and, 126\u2013130, 135 Terminals, 203, 211 array handling and, 137 Testing bootstrap code and, 165 class and, 129 chips, 299\u2013306 compilers and, 122\u2013127 emulators and, 297, 306\u2013313 design suggestions for, 143","325 Index emulators and, 121\u2013122, 150\u2013151 examples of, 135\u2013139 functions and, 127, 129\u2013130, 133, 135\u2013139 Hack mapping and, 139\u2013143, 161\u2013168 heap structure and, 132\u2013133 high-level language and, 146\u2013147, 153\u2013 154 implementation, 55, 103, 112 Jack and, 174, 233\u2013235, 240 language for, 122 memory and, 127, 129\u2013133 modi\ufb01cations and, 277\u2013279 modularity and, 123\u2013124 nested subroutine calling and, 153\u2013159 object handling and, 137\u2013139 program \ufb02ow and, 129\u2013130, 133\u2013134, 153\u2013 168 stack processing and, 124\u2013127 subroutines and, 154\u2013159 symbols and, 143 syntax and, 123 testing and, 310\u2013313 translator, 121 Virtual memory segments, 131 Visual Basic, 147 VM. See Virtual Machine Void methods, 235 von Neumann architecture, 62, 79\u201381, 85 White space, 108, 113, 181\u2013182 Windows, xiii, 277 Working stack, 161 XML, 199\u2013201, 211\u2013218, 221 Xor function, 10, 20 implementation of, 26 multi-bit versions of, 21\u201323 Yet Another Compiler Compiler (YACC), 217"]
Search
Read the Text Version
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331