250 likes | 360 Views
Assembler Tutorial. This program is part of the software suite that accompanies the book The Digital Core , by Noam Nisan and Shimon Schocken 2003, www.idc.ac.il/csd , forthcoming by MIT Press The software suite was developed by students at the Efi Arazi School of Computer Science at IDC
E N D
Assembler Tutorial This program is part of the software suite that accompanies the book The Digital Core, by Noam Nisan and Shimon Schocken 2003, www.idc.ac.il/csd, forthcoming by MIT Press The software suite was developed by students at the Efi Arazi School of Computer Science at IDC Chief Software Architect: Yaron Ukrainitz
Simulators: • used to build hardware platforms and execute programs; • supplied by us. • Translators: • Written by the students; • Executable solutions available. The book’s software suite: Translators: • Assembler: translates programs written in the Hack assembly language to executable code written in the Hack machine language; • JcVM (modeled after Java’s JVM): translates programs written in the stack-based Virtual Machine (VM) language to assembly programs; • Jack Compiler: translates programs written in the Java-like Jack language to VM programs that can run on the Virtual Machine.
This tutorial describes the Assembler • The Assembler generates binary code that can be tested either in the hardware simulator or in the CPU emulator. The book’s software suite:
Assembler Tutorial • Purpose: learn how to use the supplied Assembler, designed to translate programs from Hack assembly to Hack machine code • Required knowledge: Chapter 5 of the book • Contents: I. Assembly program example II. Command-level Assembler III. Interactive Assembler
Assembler Tutorial Part I: Before we start talking about the Assembler, let’s take a look at a typical assembly program
sum.bin 0000000000010000 1110111111001000 0000000000010001 1110101010001000 0000000000010000 1111110000010000 0000000001100100 1110010011010000 0000000000010010 1110001100000001 0000000000010000 1111110000010000 0000000000010001 1111000010001000 0000000000010000 1111110111001000 0000000000000100 1110101010000111 Assembler Example sum.asm /* sum=1+2+ … +100 */ @i // i=1 M=1 @sum // sum=0 M=0 (loop) @i // if i-100>0 goto end D=M @100 D=D-A @end D;jgt @i // sum+=i D=M @sum M=D+M @I // i++ M=M+1 @loop // goto loop 0;jmp (end)
Example sum.asm /* sum=1+2+ … +100 */ @i // i=1 M=1 @sum // sum=0 M=0 (loop) @i // if i-100>0 goto end D=M @100 D=D-A @end D;jgt @i // sum+=i D=M @sum M=D+M @I // i++ M=M+1 @loop // goto loop 0;jmp (end) The assembly program: • Stored in a text file named Prog.asm • Written and edited in a text editor The assembly process: • Translates Prog.asm into Prog.bin • Comments and white space are ignored • Variables (e.g. i and sum) are allocated to memory • Labels (e.g. loop and end) are psuedo commands that generate no code • Each assembly command is translated into a 16-bit instruction written in the Hack machine language.
Assembler Tutorial • Purpose: learn how to use the supplied Assembler, designed to translate programs from Hack assembly to Hack machine code • Required knowledge: Chapter 5 of the book • Contents: I. Assembly program example II. Command-level Assembler III. Interactive Assembler
Assembler Tutorial Part II: Learn how to use the Assembler from the operating system’s shell level, batch style. (the Assembler that you have to write should have the same GUI and behavior)
The command-level assembler The OS “type” command can be used to inspect the assembly source (.asm file) (in your computer the path will probably be different)
Inspecting the source file Source code is shown
Invoking the Assembler Invoke the Assembler program Name of the source assembly file (full path). This file name is an argument of the Assembler program.
Invoking the Assembler The OS “type” command can be used to inspect the translated code(.bin file)
Inspecting the translated code Binary code is shown Two ways to test the binary code: 1. Invoke the Hardware Simulator, load the Computer chip, then load the code (.bin file) into the internal ROM chip; 2. Load and run the code in the CPU Emulator (much quicker).
Assembler Tutorial • Purpose: learn how to use the supplied Assembler, designed to translate programs from Hack assembly to Hack machine code • Required knowledge: Chapter 5 of the book • Contents: I. Assembly program example II. Command-level Assembler III. Interactive Assembler
Hardware Simulation Tutorial Part III: Learn how to use the interactive assembler
Loading an assembly program 1. To load an assembly program, click here. 2. Navigate to a directory and select an .asm file.
Loading an assembly program • Read-only view of the assembly source code • To edit it, use an external text editor.
Translating a program Immediate translation(no animation) Re-start the translation Stop the translation Translate the entire program Translate line-by-line
1. Click an assembly command 2. The translated binary code is highlighted Inspecting the translation
Saving the translated code Saves the translated binary code in a .bin file • If an assembly command contains a syntax error, the translation stops with an error message • The “save file” operation is enabled only if the translation was error-free.
Comparing the translated code to a compare-file 1. Load a comparison file 2. Select a comparison (.bin) file
Comparing the translated code to a compare-file 2, Translate the program (any translation mode can be used, as usual) 1. Comparison file is shown
Comparing the translated code to a compare-file The translation of the highlighted line does not match the corresponding line in the compare file.
Mistakes are the portals of discovery James Joyce (1882-1941)