Download - Assemblers
![Page 1: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/1.jpg)
Assemblers
Dr. Monther Aldwairi
10/21/2007 1Dr. Monther Aldwairi
![Page 2: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/2.jpg)
Fundamental Functions
• Generate machine language– Translate mnemonic operation codes to machine
code
• Assign addresses to symbolic labels used by the programmer
10/21/2007 2Dr. Monther Aldwairi
![Page 3: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/3.jpg)
Additional Functions
• Generate an image of what memory must look like for the program to be executed.
• Interpret assembler directives (Pseudo-Instructions)– They provide instructions to the assembler– They do not translate into machine code– They might affect the object code
10/21/2007 3Dr. Monther Aldwairi
![Page 4: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/4.jpg)
Input / Output• Input Assembly Code• Output
– Assembly Listing– Object Code
• Intermediate files– Assembly Listing– LOCCTR– Instruction Length– Error Flags
10/21/2007 4Dr. Monther Aldwairi
![Page 5: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/5.jpg)
Input
10/21/2007 Dr. Monther Aldwairi 6
![Page 6: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/6.jpg)
Output/ Assembly Listing
10/21/2007 7Dr. Monther Aldwairi
![Page 7: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/7.jpg)
Output
Record Type Column Content
1
1 Record Type = 1
2-3 Byte Count
4-7 The execution start address (Hex)
8-9 Check Sum (CS)
2
1 Record Type = 2
2-3 Byte Count
4-7 The load address for the instruction/data (Hex)
8-57 The code to be loaded in Hex
58-59 Check Sum (CS)
10/21/2007 8Dr. Monther Aldwairi
![Page 8: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/8.jpg)
Object Code
10/21/2007 Dr. Monther Aldwairi 9
![Page 9: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/9.jpg)
Design Approach
• One Pass: Line by Line– forward reference?
• Two Passes:– Pass 1– Pass2
• Intermediate files
10/21/2007 10Dr. Monther Aldwairi
![Page 10: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/10.jpg)
One-Pass Assembler
• The main problem is forward reference.• Eliminating forward reference
– Simply ask the programmer to define variables before using them.
• However, ?!– Backward jumps is too restrictive.– Forward jumps (Subroutine calls, Loops)
10/21/2007 Dr. Monther Aldwairi 11
![Page 11: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/11.jpg)
Input
10/21/2007 Dr. Monther Aldwairi 12
![Page 12: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/12.jpg)
Output/ Assembly Listing
10/21/2007 13Dr. Monther Aldwairi
![Page 13: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/13.jpg)
Output Object Code
Record Type Column Content
1
1 Record Type = 1
2-3 Byte Count
4-7 The execution start address (Hex)
8-9 Check Sum (CS)
2
1 Record Type = 2
2-3 Byte Count
4-7 The load address for the instruction/data (Hex)
8-57 The code to be loaded in Hex
58-59 Check Sum (CS)
10/21/2007 14Dr. Monther Aldwairi
![Page 14: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/14.jpg)
Example
10/21/2007 Dr. Monther Aldwairi 15
![Page 15: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/15.jpg)
Example
10/21/2007 Dr. Monther Aldwairi 16
![Page 16: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/16.jpg)
Forward Reference• For any symbol that has not yet been defined
1. omit the address translation2. insert the symbol into SYMTAB, and mark this symbol
undefined3. the address that refers to the undefined symbol is
added to a list of forward references associated with the symbol table entry
4. when the definition for a symbol is encountered, the proper address for the symbol is then inserted into any instructions previous generated according to the forward reference list
10/21/2007 Dr. Monther Aldwairi 17
![Page 17: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/17.jpg)
Load-and-go Assembler (Cont.)
• At the end of the program– any SYMTAB entries that are still marked with *
indicate undefined symbols– search SYMTAB for the symbol named in the END
statement and jump to this location to begin execution
• The actual starting address must be specified at assembly time
10/21/2007 Dr. Monther Aldwairi 18
![Page 18: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/18.jpg)
Processing Example
10/21/2007 Dr. Monther Aldwairi 19
After scanning line 40
![Page 19: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/19.jpg)
Processing Example
10/21/2007 Dr. Monther Aldwairi 20
After scanning line 160
![Page 20: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/20.jpg)
Two Passes• Pass 1
– Validate Opcodes– Assign addresses to all statements in the program– Scan the source for labels and save their values– Perform some processing of assembler directives
• Determine the length of areas defined by DC, DS
• Pass 2– Translate/assemble the instructions– Generate Data Values defined by DC– Process the rest of the assembler directives– Write the Object Code and Assembly Listing
10/21/2007 21Dr. Monther Aldwairi
![Page 21: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/21.jpg)
Data Structures• Operation Code Table (OPTAB)
– Opcode, Instruction format, and length– Pass 1: Validate opcodes– Pass2: Assemble instructions
• Symbol Table (SYMTAB)– Label name and value, error flags– Pass 1: Created!– Lookup symbols to insert in assembled instr.
• Location Counter– Initialed to the Org or End
10/21/2007 22Dr. Monther Aldwairi
![Page 22: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/22.jpg)
A Simple Two Pass Assembler Implementation
10/21/2007 Dr. Monther Aldwairi 23
Pass 1 Pass 2 Object codes
Sourceprogram
OPTAB SYMTAB SYMTAB
Label and addressmappings created
Label and addressmappings are referenced
Mnemonic andopcode mappingsare referenced
READ (Label, opcode, operand)
![Page 23: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/23.jpg)
Hash Tables
• OPTAB is static (access)– Retrieval efficiency– Key : Mnemonic operation
• SYMTAB (add, access)– Insertion and Retrieval efficiency– Key: Label Name
• LOOP1, LOOP2, LOOP3…, A, X, Y, Z…
10/21/2007 24Dr. Monther Aldwairi
![Page 24: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/24.jpg)
OPTAB (operation code table)• Content
– The mapping between mnemonic and machine code. Also include the instruction format, available addressing modes, and length information.
• Characteristic– Static table. The content will never change.
• Implementation– Array or hash table. Because the content will never change, we can
optimize its search speed.• In pass 1, OPTAB is used to look up and validate mnemonics in
the source program.• In pass 2, OPTAB is used to translate mnemonics to machine
instructions.
10/21/2007 Dr. Monther Aldwairi 25
![Page 25: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/25.jpg)
Symbol Table (SYMTAB)• Content
– Include the label name and value (address) for each label in the source program.
– Include type and length information (e.g., int64)– With flag to indicate errors (e.g., a symbol defined in two places)
• Characteristic– Dynamic table (I.e., symbols may be inserted, deleted, or
searched in the table)• Implementation
– Hash table can be used to speed up search– Because variable names may be very similar (e.g., LOOP1,
LOOP2), the selected hash function must perform well with such non-random keys.
10/21/2007 Dr. Monther Aldwairi 26
![Page 26: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/26.jpg)
Pass 1 Pseudo Code
10/21/2007 Dr. Monther Aldwairi 27
![Page 27: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/27.jpg)
Pass 1
10/21/2007 Dr. Monther Aldwairi 28
![Page 28: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/28.jpg)
Pass 1
10/21/2007 Dr. Monther Aldwairi 29
![Page 29: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/29.jpg)
Pass 2 Pseudo Code
10/21/2007 Dr. Monther Aldwairi 30
![Page 30: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/30.jpg)
Pass 2
10/21/2007 Dr. Monther Aldwairi 31
![Page 31: Assemblers](https://reader035.vdocuments.net/reader035/viewer/2022062500/568151df550346895dc017be/html5/thumbnails/31.jpg)
Pass 2
10/21/2007 Dr. Monther Aldwairi 32