Assembler Tutorial
description
Transcript of Assembler Tutorial
Slide 1/25Assembler Tutorial Tutorial Index
Assembler Tutorial
This program is part of the software suitethat 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 theEfi Arazi School of Computer Science at IDC
Chief Software Architect: Yaron Ukrainitz
Slide 2/25Assembler Tutorial Tutorial Index
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.
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:
Slide 3/25Assembler Tutorial Tutorial Index
The book’s software suite:
The Assembler generates binary code that can be tested either in the hardware simulator or in the CPU emulator.
This tutorial describes the Assembler
Slide 4/25Assembler Tutorial Tutorial Index
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
Slide 5/25Assembler Tutorial Tutorial Index
Assembler Tutorial
Part I:
Before we start talking about the Assembler, let’s take a look at a typical assembly program
Slide 6/25Assembler Tutorial Tutorial Index
Example
/* sum=1+2+ … +100 */@i // i=1M=1@sum // sum=0M=0
(loop)@i // if i-100>0 goto endD=M@100D=D-A@endD;jgt@i // sum+=iD=M@sumM=D+M@I // i++M=M+1@loop // goto loop0;jmp
(end)
/* sum=1+2+ … +100 */@i // i=1M=1@sum // sum=0M=0
(loop)@i // if i-100>0 goto endD=M@100D=D-A@endD;jgt@i // sum+=iD=M@sumM=D+M@I // i++M=M+1@loop // goto loop0;jmp
(end)
sum.asm
000000000001000011101111110010000000000000010001111010101000100000000000000100001111110000010000000000000110010011100100110100000000000000010010111000110000000100000000000100001111110000010000000000000001000111110000100010000000000000010000111111011100100000000000000001001110101010000111
000000000001000011101111110010000000000000010001111010101000100000000000000100001111110000010000000000000110010011100100110100000000000000010010111000110000000100000000000100001111110000010000000000000001000111110000100010000000000000010000111111011100100000000000000001001110101010000111
sum.bin
Assembler
Slide 7/25Assembler Tutorial Tutorial Index
Example
/* sum=1+2+ … +100 */@i // i=1M=1@sum // sum=0M=0
(loop)@i // if i-100>0 goto endD=M@100D=D-A@endD;jgt@i // sum+=iD=M@sumM=D+M@I // i++M=M+1@loop // goto loop0;jmp
(end)
/* sum=1+2+ … +100 */@i // i=1M=1@sum // sum=0M=0
(loop)@i // if i-100>0 goto endD=M@100D=D-A@endD;jgt@i // sum+=iD=M@sumM=D+M@I // i++M=M+1@loop // goto loop0;jmp
(end)
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.
The assembly program:
Stored in a text file named Prog.asm
Written and edited in a text editor
sum.asm
Slide 8/25Assembler Tutorial Tutorial Index
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
Slide 9/25Assembler Tutorial Tutorial Index
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)
Slide 10/25Assembler Tutorial Tutorial Index
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)
Slide 11/25Assembler Tutorial Tutorial Index
Inspecting the source file
Source code is shown
Slide 12/25Assembler Tutorial Tutorial Index
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.
Slide 13/25Assembler Tutorial Tutorial Index
The OS “type” command can be used to inspect the translated code(.bin file)
Invoking the Assembler
Slide 14/25Assembler Tutorial Tutorial Index
Binary code is shown
Inspecting the translated code
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).
Slide 15/25Assembler Tutorial Tutorial Index
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
Slide 16/25Assembler Tutorial Tutorial Index
Hardware Simulation Tutorial
Part III:
Learn how to use the interactive assembler
Slide 17/25Assembler Tutorial Tutorial Index
Loading an assembly program
1. To load an assembly program, click here.
2. Navigate to a directory and select an .asm file.
Slide 18/25Assembler Tutorial Tutorial Index
Loading an assembly program
Read-only view of the assembly source code
To edit it, use an external text editor.
Slide 19/25Assembler Tutorial Tutorial Index
Translating a program
Translate line-by-line
Translate the entire program
Stop the translation
Re-start the translation
Immediate translation(no animation)
Slide 20/25Assembler Tutorial Tutorial Index
1. Click an assembly command
2. The translated binary code is
highlighted
Inspecting the translation
Slide 21/25Assembler Tutorial Tutorial Index
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.
Slide 22/25Assembler Tutorial Tutorial Index
Comparing the translated code to a compare-file
1. Load a comparison file
2. Select a comparison (.bin) file
Slide 23/25Assembler Tutorial Tutorial Index
2, Translate the program (any translation mode can be used, as usual)
Comparing the translated code to a compare-file
1. Comparison file is shown
Slide 24/25Assembler Tutorial Tutorial Index
The translation of the highlighted line does not match the corresponding line in the compare file.
Comparing the translated code to a compare-file
Slide 25/25Assembler Tutorial Tutorial Index
Mistakes are the portals of discovery
James Joyce (1882-1941)