Assembler Tutorial This program is part of the
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 Assembler Tutorial Index Slide 1/25
The book’s software suite: Simulators: Translators: § used to build hardware platforms and execute programs; § Written by the students; § Executable solutions available. § supplied by us. Translators: § Assembler: translates programs written in the Hack assembly language to executable code written in the Hack machine language; § Jc. VM (modeled after Java’s JVM): translates programs written in the stackbased 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. Assembler Tutorial Index Slide 2/25
The book’s software suite: Assembler Tutorial § This tutorial describes the Assembler § The Assembler generates binary code that can be tested either in the hardware simulator or in the CPU emulator. Tutorial Index Slide 3/25
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 Tutorial Index Slide 4/25
Assembler Tutorial Part I: Before we start talking about the Assembler, let’s take a look at a typical assembly program Assembler Tutorial Index Slide 5/25
Example sum. bin 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) Assembler Tutorial Index 00000010000 1110111111001000 00000010001 111010001000 00000010000 111111000001000001100100 11100100110100000010010 111000000010000 11111100000100000010001 111100001000 00000010000 111111001000 0000000100 111010000111 Slide 6/25
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) Assembler Tutorial 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 § 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. Tutorial Index Comments and white space are ignored Slide 7/25
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 Tutorial Index Slide 8/25
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) Assembler Tutorial Index Slide 9/25
The command-level assembler The OS “type” command can be used to inspect the assembly source (. asm file) Assembler Tutorial (in your computer the path will probably be different) Tutorial Index Slide 10/25
Inspecting the source file Source code is shown Assembler Tutorial Index Slide 11/25
Invoking the Assembler Invoke the Assembler program Assembler Tutorial Name of the source assembly file (full path). This file name is an argument of the Assembler program. Tutorial Index Slide 12/25
Invoking the Assembler The OS “type” command can be used to inspect the translated code (. bin file) Assembler Tutorial Index Slide 13/25
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 Index Slide 14/25
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 Tutorial Index Slide 15/25
Hardware Simulation Tutorial Part III: Learn how to use the interactive assembler Assembler Tutorial Index Slide 16/25
Loading an assembly program 1. To load an assembly program, click here. 2. Navigate to a directory and select an. asm file. Assembler Tutorial Index Slide 17/25
Loading an assembly program § Read-only view of the assembly source code § To edit it, use an external text editor. Assembler Tutorial Index Slide 18/25
Translating a program Immediate translation (no animation) Re-start the translation Stop the translation Translate the entire program Translate line-by-line Assembler Tutorial Index Slide 19/25
Inspecting the translation 1. Click an assembly command Assembler Tutorial 2. The translated binary code is highlighted Tutorial Index Slide 20/25
Saving the translated code Saves the translated binary code in a. bin file Assembler Tutorial § 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. Tutorial Index Slide 21/25
Comparing the translated code to a compare-file 1. Load a comparison file 2. Select a comparison (. bin) file Assembler Tutorial Index Slide 22/25
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 Assembler Tutorial Index Slide 23/25
Comparing the translated code to a compare-file The translation of the highlighted line does not match the corresponding line in the compare file. Assembler Tutorial Index Slide 24/25
Mistakes are the portals of discovery James Joyce (1882 -1941) Assembler Tutorial Index Slide 25/25
- Slides: 25