Unit 4 Unit 3 Pushdown Assembler Automata Prof
Unit – 4 Unit – 3 Pushdown Assembler Automata Prof. Dixita Kagathara dixita. kagathara@darshan. ac. in System Programming (2150708) Unit – 3 : Assembler Darshan Institute of Engineering & Technology
Topics to be covered • Elements of assembly language programming • Types of assembly language statements • Design of the assembler & assembler design criteria • Advanced assembler directives • Types of assemblers • Two-Pass assemblers • One-Pass assemblers • Algorithm of single pass assembler • Variants of assemblers • Design of two pass assembler Unit – 3 : Assembler System Programming (2150708) 2 Darshan Institute of Engineering & Technology
Assembler • Assembler is a language processor that converts assembly language program to machine language program. MOV id 3, R 1 MUL #2. 0, R 1 MOV id 2, R 2 MUL R 2, R 1 MOV id 1, R 2 ADD R 2, R 1 MOV R 1, id 1 Assembly language program Unit – 3 : Assembler System Programming (2150708) Assembler Error Messages (If any) 3 0000 1100 0010 0111 1000 0001 1111 0101 1110 1100 0000 1011 0010 1001 1000 1001 1100 0000 1000 Machine language program Darshan Institute of Engineering & Technology
Assembly language • An assembly language is machine dependent. • It is low level programming language that is specific to certain computer or to the certain family of computers. • Assembly language provides following three basic facilities: 1. Mnemonic operation codes 2. Symbolic operands 3. Data declarations Unit – 3 : Assembler System Programming (2150708) 4 Darshan Institute of Engineering & Technology
Facilities of assembly language Basic facilities of assembly language Mnemonics Operation Code Symbolic Operand Data Declaration • Consider following assembly statement: MOVE AREG, X Register operand Memory operand Mnemonics in symbolic form Operation Code Unit – 3 : Assembler System Programming (2150708) 5 in symbolic form Darshan Institute of Engineering & Technology
Facilities of assembly language Basic facilities of assembly language Mnemonics Operation Code Symbolic Operand Data Declaration • Consider another assembly statement: X DS, 5 Symbolic Name Declare storage Reserves 5 words of memory for X Unit – 3 : Assembler System Programming (2150708) 6 Darshan Institute of Engineering & Technology
Assembly language statement format • An assembly language statement has the following format: [Label] <Opcode> <operand specification> • Example: AGAIN MULT BREG, A It is a symbolic name. It is an optional field. Unit – 3 : Assembler System Programming (2150708) 7 Darshan Institute of Engineering & Technology
Assembly language statement format • An assembly language statement has the following format: [Label] Instruction opcode <Opcode> <operand specification> Assembly mnemonics Remarks 00 STOP Stop execution 01 ADD Perform addition 02 SUB Perform subtraction 03 MULT Perform multiplication 04 MOVER Memory to Register move 05 MOVEM Register to Memory move 06 COMP Compare & set condition 07 BC Branch on condition 08 DIV Perform division 2. LE Read into Register 3. EQa 5. GE 6. ANY Print content of Register 09 10 Unit – 3 : Assembler System Programming (2150708) READ 1. LT 4. GT PRINT 8 Darshan Institute of Engineering & Technology
Assembly language statement format • An assembly language statement has the following format: [Label] <Opcode> <operand specification> • <operand specification> has following syntax: <symbolic name> [± <displacement>] [(<index register>)] Example: AREA: It returns a memory word with which name AREA is associated. AREA+5: Refers a memory word 5 word away from the word with name AREA. ‘ 5’ is displacement. AREA(4): It implies indexing the operand AREA with index register 4. That is the operand address is obtained by adding the content of index register 4 to the address of AREA+5(4): It is a combination of previous two specifications. Unit – 3 : Assembler System Programming (2150708) 9 Darshan Institute of Engineering & Technology
Types of assembly language statements • Three types of assembly statements are: 1. Imperative statement 2. Declaration statement 3. Assembler directive Unit – 3 : Assembler System Programming (2150708) 11 Darshan Institute of Engineering & Technology
Imperative statement • An imperative statement indicates an action to be performed during the execution of the assembled statement. • Each imperative statement converted into one machine instruction. • These are executable statements. • Examples: MOVER BREG, X ADD AREG, Y PRINT RESULT STOP Unit – 3 : Assembler System Programming (2150708) 12 Darshan Institute of Engineering & Technology
Declarative statement • Declaration statements are used for reserving memory for variables. • The syntax of declaration statement is as follow: [Label] DS <constant> [Label] DC ‘<value>’ Where DS stands for Declare storage and DC stands for Declare constant. Unit – 3 : Assembler System Programming (2150708) 13 Darshan Institute of Engineering & Technology
Declarative statement DS DC The DS statement reserves area of memory and associates name with them. The DC statement constructs memory words containing constants. A DS 10 ONE DC ‘ 1’ Above statement reserves 10 words of memory for variable A. Above statement associates the name ONE with a memory word containing the value ‘ 1’. Unit – 3 : Assembler System Programming (2150708) 14 Darshan Institute of Engineering & Technology
Assembler directives • Assembler directives instruct the assembler to perform certain action during the assembly program. • Assembler directives are not converted into machine code rather they give instruction to assembler itself. 1. START 2. END Unit – 3 : Assembler System Programming (2150708) 15 Darshan Institute of Engineering & Technology
Assembler directives START <constant> • This directive instructs the assembler to place first word of the target program in the memory having the address <constant>. • Example: START 100 • First word of the target program is stored in the memory location 100 onwards. Unit – 3 : Assembler System Programming (2150708) 16 Darshan Institute of Engineering & Technology
Assembler directives END [<operand specification>] • This directive indicates end of the source program. • The operand specification indicates address of the instruction from where the execution of program should begin. • Execution control should transfer to label given in operand field. • By default, execution begins with the first instruction of the assembly program. Unit – 3 : Assembler System Programming (2150708) 17 Darshan Institute of Engineering & Technology
Conversion from Assembly language to Machine language System Programming (2150708) Unit – 3 : Assembler Darshan Institute of Engineering & Technology
Statement formats Assembly statement format: Label Opcode Operand specifications Machine statement format: Opcode Unit – 3 : Assembler System Programming (2150708) Register operand 19 Memory operand Darshan Institute of Engineering & Technology
Memory Opcode Location Assembly Statements Register Operand Memory Operand START 101 READ N 101) 09 0 113 MOVER BREG, ONE 102) 04 2 115 MOVEM BREG, TERM 103) 05 2 116 AGAIN MULT BREG, TERM 104) 03 2 116 MOVER CREG, TERM 105) 04 3 116 ADD CREG, ONE 106) 01 3 115 MOVEM CREG, TERM 107) 05 3 116 COMP CREG, N 108) 06 3 113 BC LE, AGAIN 109) 07 2 104 MOVEM BREG, RESULT 110) 05 2 114 PRINT RESULT 111) 10 0 114 STOP 112) 00 0 000 N DS 1 113) No code generation for DS RESULT DS 1 114) No code generation for DS ONE DC ‘ 1’ 115) TERM DS 1 116) System Programming (2150708) END 20 001 No code generation for DS Darshan Institute of Engineering & Technology
Simple assembly scheme System Programming (2150708) Unit – 3 : Assembler Darshan Institute of Engineering & Technology
Design specification for an assembler • Following steps are used to develop a design specification for an assembler. 1. Identify the information required to perform a task. 2. Design a suitable data structure for recording the information. 3. Determine the processing required for obtaining and maintaining the information. 4. Determine the processing required for performing the task by using the recorded information. Unit – 3 : Assembler System Programming (2150708) 22 Darshan Institute of Engineering & Technology
Design of an assembler mnemonic opcode length ADD 01 1 SUB 02 1 Mnemonics Table Fields of Mnemonics table Data access Control transfer Source Program Analysis Phase Symbol Table Data structures of an Assembler mnemonic opcode AGAIN 104 N 113 Synthesis Phase Target Program Fields of Symbol table Phases of an Assembler Unit – 3 : Assembler System Programming (2150708) 23 Darshan Institute of Engineering & Technology
Analysis phase • The primary function performed by the analysis phase is to build the symbol table. • Addresses of symbolic names are required to build a symbol table. • It is possible to determine some address directly, however others must be inferred. This function is called memory allocation. • Location Counter (LC) is used to implement a memory allocation. • The processing involved in maintaining the location counter is called LC processing. Unit – 3 : Assembler System Programming (2150708) 24 Darshan Institute of Engineering & Technology
Tasks of analysis phase 1. Separate the content of label, mnemonics opcode, and operand fields of a statement. 2. If a label is present, enter the pair (symbol, <LC content>) in a new entry of the symbol table. 3. Check validity of mnemonics opcode. 4. Perform LC processing. Example: ONE MULT BREG, A label ONE Address 100 Symbol table Unit – 3 : Assembler System Programming (2150708) 25 Darshan Institute of Engineering & Technology
Synthesis phase • Consider following assembly statement: MOVER BREG, ONE • We require following information to synthesize the machine instruction corresponding to above statement: 1. Address of variable ONE: depends on the source program; hence it must be available by analysis phase. 2. Machine operation code of MOVER: does not depend on the source program; it depends on the assembly language. Unit – 3 : Assembler System Programming (2150708) 26 Darshan Institute of Engineering & Technology
Tasks of synthesis phase 1. Obtain machine opcode from the mnemonics table. 2. Obtain address of memory operand from the symbol table. 3. Synthesize a machine instruction. (conversion from assembly language to machine language) Example: (Conversion from assembly language to machine language) MOVER BREG, ONE 04 02 100 Opcode Mnemonics 00 STOPCode Register 06 COMP Consider address Code for BREG = 2 Opcode of MOVER = 04 of ONE = 100 Unit – 3 : Assembler System Programming (2150708) 27 01 ADD 01 02 SUB 04 02 MULT 03 MOVER 04 05 MOVEM 03 AREG 07 BREG 08 CREG 09 DREG 10 BC DIV READ PRINT Darshan Institute of Engineering & Technology
Pass structure of assembler System Programming (2150708) Unit – 3 : Assembler Darshan Institute of Engineering & Technology
Pass structure of assembler • A complete scan of the program is called pass. • Types of assembler are: 1. Two pass assembler (Two pass translation) 2. Single pass assembler (Single pass translation) Unit – 3 : Assembler System Programming (2150708) 29 Darshan Institute of Engineering & Technology
Two pass assembler (Two pass translation) l l l Data structures Source Program Pass II Pass I Target Program Intermediate code Data access Control transfer Unit – 3 : Assembler System Programming (2150708) 30 Darshan Institute of Engineering & Technology
Two pass assembler (Two pass translation) Source Program Intermediate representation Pass I Intermediate code Pass II Target Program Data structures • The first pass performs analysis of the source program. • The first pass performs Location Counter processing and records the addresses of symbols in the symbol table. • It constructs intermediate representation of the source program. • Intermediate representation consists of following two components: 1. Intermediate code 2. Data structures Unit – 3 : Assembler System Programming (2150708) 31 Darshan Institute of Engineering & Technology
Two pass assembler (Two pass translation) Source Program Pass I Intermediate representation Intermediate code Pass II Target Program Data structures • The second pass synthesizes the target program by using address information recorded in the symbol table. • Two pass translation handles a forward reference to a symbol naturally because the address of each symbol would be known before program synthesis begins. Use of a Symbol that precedes its definition in a program. Unit – 3 : Assembler System Programming (2150708) 32 Darshan Institute of Engineering & Technology
One pass assembler (One pass translation) • A one pass assembler requires one scan of the source program to generate machine code. • LC processing, symbol table construction and target code generation proceed in single pass. • The issue of forward references can be solved using a process called back patching. Unit – 3 : Assembler System Programming (2150708) 33 Darshan Institute of Engineering & Technology
Example: Back patching Memory Opcode Location Assembly Statements Register Operand Memory Operand START 101 READ N 101) 09 0 115 MOVER BREG, ONE 102) 04 2 116 00 0 001 . . . N DC ‘ 1’ 115) ONE DS 1 116) END No code generation for DS Instruction address Symbol 101 N 102 ONE Table of incomplete instructions (TII) Unit – 3 : Assembler System Programming (2150708) 34 Darshan Institute of Engineering & Technology
Resolving forward references using back patching • The operand field of an instruction containing forward references is left blank initially. • It builds a Table of Incomplete Instructions (TII) to record information about instructions whose operand fields were left blank. • Each entry in TII contains a pair in the form of (instruction address, symbol) • When END statement is processed, The symbol table would contain addresses of all symbols defined in the source program. • TII would contain information describing all forward references. • The assembler can now process each entry in TII to complete the concerned instruction. Unit – 3 : Assembler System Programming (2150708) 35 Darshan Institute of Engineering & Technology
Advanced assembler directives System Programming (2150708) Unit – 3 : Assembler Darshan Institute of Engineering & Technology
Advanced assembler directives • Advanced assembler directives are: 1. ORIGIN 2. EQU 3. LTORG Unit – 3 : Assembler System Programming (2150708) 37 Darshan Institute of Engineering & Technology
ORIGIN • Syntax: ORIGIN <address specification> • Where <address specification> is an <operand specification> or <constant>. • This directive indicates that LC should be set to the address given by <address specification>. • The ORIGIN statement is useful when the target program does not consist of consecutive memory words. Unit – 3 : Assembler System Programming (2150708) 38 Darshan Institute of Engineering & Technology
Example: ORIGIN Memory Opcode Location Assembly Statements START 200 …. LOOP MOVER AREG, A 210) …. ORIGIN LOOP + 2 …. MULT Unit – 3 : Assembler System Programming (2150708) CREG, B 39 212) Register Operand Memory Operand 04 1 217 03 3 218 Darshan Institute of Engineering & Technology
EQU • Syntax <symbol> EQU <address specification> • Where <address specification> is either a <constant> or <symbolic name> ± <displacement>. • The EQU statement simply associates the name <symbol> with the address specified by <address specification>. • However, the address in the location counter is not affected. Unit – 3 : Assembler System Programming (2150708) 40 Darshan Institute of Engineering & Technology
Example: EQU Memory Opcode Location Assembly Statements START 100 …. LOOP MOVER AREG, A 102) Register Operand Memory Operand 04 1 117 07 1 102 . BACK BC LT, BACK EQU LOOP Unit – 3 : Assembler System Programming (2150708) 115) No LC counter is processed 41 Darshan Institute of Engineering & Technology
LTORG • The LTORG directive, stands for 'origin for literals'. • LTORG allows a programmer to specify where literals should be placed. • The assembler uses the following scheme for placement of literals: 1. When the use of a literal is seen in a statement, the assembler enters it into a literal pool unless a matching literal already exists in the pool. 2. At every LTORG statement also at the END statement, the assembler allocates memory to the literals of the literal pool then clears the literal pool. Unit – 3 : Assembler System Programming (2150708) 42 Darshan Institute of Engineering & Technology
Example: LTORG Memory Opcode Location Assembly Statements START 200 LOOP Register Operand Memory Operand MOVER AREG, =’ 5’ 200) 04 1 211 ADD CREG, =’ 1’ 201) 01 3 212 =’ 5’ 211) 00 0 005 =’ 1’ 212) 00 0 001 AREG, =’ 1’ 214) 02 1 219 =’ 1’ 219) 00 0 001 …. LTORG SUB END Literal pool ‘ 5’ ‘ 1’ Unit – 3 : Assembler System Programming (2150708) 43 Darshan Institute of Engineering & Technology
Intermediate code forms System Programming (2150708) Unit – 3 : Assembler Darshan Institute of Engineering & Technology
Intermediate code forms • The Intermediate code consists of a sequence of intermediate code units. (IC units) • Each unit consists of the following three fields: 1. Address 2. Representation of mnemonics opcode 3. Representation of operands • There are two variants of intermediate code: 1. Variant I 2. Variant II • Address and mnemonics opcode fields are assumed to contain identical information in both variants. • But, both variants differ in information contained in operand field. Unit – 3 : Assembler System Programming (2150708) 45 Darshan Institute of Engineering & Technology
Format of intermediate code Address Mnemonics Opcode Operands Code Statement class Imperative statement (IS) Declaration statement (DL) Assembler directive (AD) Examples: START: (AD, 01) Code for AD & DL Code for AD START DS: (DL, 02) END ORIGIN READ: (IS, 09) Unit – 3 : Assembler System Programming (2150708) Instruction opcode for IS 46 01 EQU Code for DL 04 DC 01 02 LTORG 05 DS 02 03 Darshan Institute of Engineering & Technology
Format of intermediate code (Variant I) Address Mnemonics Opcode Second operand First operand Code for register Code for condition Constant(C) Examples: GT, LOOP AREG, A (1) (S, 01) (4) (S, 02) Operands Symbol(S) Operand class Literal (L) AREG, =‘ 1’ START 200 (1) (L, 01) (C, 200) Unit – 3 : Assembler System Programming (2150708) 47 Code Constant itself for constant Code for Condition LT LE EQ GT GE ANY 01 02 03 04 05 06 For symbol and literal entry no. in SYMTAB & LITTAB Darshan Institute of Engineering & Technology
Format of intermediate code (Variant II) • In variant II symbols, condition codes and CPU registers are not processed. Unit – 3 : Assembler System Programming (2150708) 48 Darshan Institute of Engineering & Technology
Variant I & variant II Variant I Assembly statements Variant II START 200 (AD, 01) (C, 200) READ A (IS, 09) (S, 01) (IS, 09) A LOOP MOVER AREG, A (IS, 04) (1)(S, 01) (IS, 04) AREG, A . . . SUB AREG, =’ 1’ (IS, 02) (1)(L, 01) (IS, 02) AREG, (L, 01) BC GT, LOOP (IS, 07) (4)(S, 02) (IS, 07) GT, LOOP STOP (IS, 00) A DS 1 (DL, 02) (C, 1) LTORG (AD, 05) Symbols, condition codes and CPU register are not processed in Variant II. Unit – 3 : Assembler System Programming (2150708) 49 Darshan Institute of Engineering & Technology
Exercise: Write variant I & variant II for given program. 1 2 3 4 5 6 7 12 13 14 15 16 17 18 19 20 21 22 AGAIN N RESULT ONE TERM Assembly statements START 101 READ N MOVER BREG, ONE MOVEM BREG, TERM MULT BREG, TERM MOVER CREG, TERM ADD CREG, ONE MOVEM CREG, TERM COMP CREG, N BC LE, AGAIN MOVEM BREG, RESULT PRINT RESULT STOP DS 1 DC ‘ 1’ DS 1 END Unit – 3 : Assembler System Programming (2150708) Variant I (AD, 01) (C, 101) (IS, 09) (S, 02) (IS, 04) (2)(S, 04) (IS, 05) (2)(S, 05) (IS, 03) (2)(S, 05) (IS, 04) (3)(S, 05) (IS, 01) (3)(S, 04) (IS, 05) (3)(S, 05) (IS, 06) (3)(S, 02) (IS, 07) (2)(S, 01) (IS, 05) (2)(S, 03) (IS, 10) (S, 03) (IS, 00) (DL, 02) (C, 1) (DL, 01) (C, 1) (DL, 02) (C, 1) (AD, 02) 50 Variant II (AD, 01) (C, 101) (IS, 09) N (IS, 04) BREG, ONE (IS, 05) BREG, TERM (IS, 03) BREG, TERM (IS, 04) CREG, TERM (IS, 01) CREG, ONE (IS, 05) CREG, TERM (IS, 06) CREG, N (IS, 07) LE, AGAIN (IS, 05) BREG, RESULT (IS, 10) RESULT (IS, 00) (DL, 02) (C, 1) (DL, 01) (C, 1) (DL, 02) (C, 1) (AD, 02) Darshan Institute of Engineering & Technology
Design of a two pass assembler System Programming (2150708) Unit – 3 : Assembler Darshan Institute of Engineering & Technology
Design of a two pass assembler • Tasks performed by the passes of a two pass assembler are as follows: • Pass I (Analysis phase) 1. Separate the symbol, mnemonics opcode and operand fields. 2. Build the symbol table. 3. Perform Location Counter (LC) processing. 4. Construct intermediate representation. • Pass II (Synthesis phase) 1. Synthesize the target program. Unit – 3 : Assembler System Programming (2150708) 52 Darshan Institute of Engineering & Technology
Data structures of assembler - Pass I • Pass I uses the following data structures: 1. OPTAB - A table of mnemonics opcode and related information 2. SYMTAB - A table of symbols used in the program. 3. LITTAB - A table of literals used in the program 4. POOLTAB – A table of information concerning literal pools Mnemonics opcode Mnemonics info Class MOVER (04, 1) IS (Imperative statement) DS R#7 DL (Declaration statement) START R#11 AD (Assembler directive) • If an imperative statement, then mnemonics info field contains the pair (machine code, instruction length). • Else it contains the id of a routine to handle the declaration or assembler directive statement. Unit – 3 : Assembler System Programming (2150708) 53 Darshan Institute of Engineering & Technology
Data structures of assembler - Pass I • Pass I uses the following data structures: 1. OPTAB - A table of mnemonics opcode and related information 2. SYMTAB - A table of symbols used in the program. 3. LITTAB - A table of literals used in the program 4. POOLTAB – A table of information concerning literal pools Symbol Address Length LOOP 202 1 NEXT 214 1 LAST 216 1 • A SYMTAB entry contains the symbol name, field address and length. • Some addresses can be determined directly, e. g. the address of the first instruction in the program, however others must be inferred. Unit – 3 : Assembler System Programming (2150708) 54 Darshan Institute of Engineering & Technology
Data structures of assembler - Pass I • Pass I uses the following data structures: 1. OPTAB - A table of mnemonics opcode and related information 2. SYMTAB - A table of symbols used in the program. 3. LITTAB - A table of literals used in the program 4. POOLTAB – A table of information concerning literal pools literal Address 1 =’ 5’ 2 =’ 1’ 3 =’ 1’ • A LITTAB entry contains the field literal and address. • The first pass uses LITTAB to collect all literals used in a program. Unit – 3 : Assembler System Programming (2150708) 55 Darshan Institute of Engineering & Technology
Data structures of assembler - Pass I • Pass I uses the following data structures: 1. OPTAB - A table of mnemonics opcode and related information 2. SYMTAB - A table of symbols used in the program. 3. LITTAB - A table of literals used in the program 4. POOLTAB – A table of information concerning literal pools Literal no #1 #3 - • This table contains the literal number of the starting literal of each literal pool. Unit – 3 : Assembler System Programming (2150708) 56 Darshan Institute of Engineering & Technology
1 2 3 4 5 6 7 12 13 14 15 16 17 18 19 20 21 22 23 24 25 LOOP NEXT LAST A BACK B START MOVER MOVEM MOVER ADD … BC LTORG … SUB BC STOP ORIGIN MULT ORIGIN DS EQU DS END 200 AREG, =’ 50’ AREG, A CREG, B CREG, =’ 10’ ANY, NEXT =’ 50’ =’ 10’ AREG, =’ 20’ LT, BACK LOOP + 2 CREG, B LAST + 1 1 LOOP 1 =’ 20’ System Programming (2150708) 200) 201) 202) 203) 204) 210) 211) 212) 214) 215) 216) 204) 217) 218) 219) 57 +04 1 211 +05 1 217 +04 3 218 +01 3 212 +07 6 214 +00 0 005 +00 0 001 +02 1 219 +07 1 202 +00 0 000 +03 3 218 +00 0 001 Symbol LOOP NEXT LAST A BACK B No. 1 2 3 Address 202 214 216 217 202 218 SYMTAB Length 1 1 1 literal Address =’ 50’ 211 =’ 10’ 212 =’ 20’ 219 LITTAB Literal no #1 #3 POOLTAB Darshan Institute of Engineering & Technology
Assembler pass I Algorithm Location Counter 1. loc_cntr=0(default value) Points to an entry in LITTAB pooltab_ptr=1; POOLTAB[1]=1; littab_ptr=1; Points to an entry in POOLTAB 2. While next statement is not END statement a) If a label is present then Enter label in symbol table. I. this_label=symbol in label field II. Enter (this_label, loc_cntr) in SYMTAB Allocate memory for literals and increment pooltab_ptr b) If an LTORG statement then I. Process literals LITTAB to allocate memory and put the address field. update loc_cntr accordingly II. pooltab_ptr= pooltab_ptr+1; III. POOLTAB[ pooltab_ptr]= littab_ptr Memory allocation process c) If a START or ORIGIN statement then I. loc_cntr=value specified in operand field; Update the symbol table entry for label d) If an EQU statement then I. this_address=value specified in <address spec>; II. Correct the symtab entry for this_label to (this_label, this_address); Unit – 3 : Assembler System Programming (2150708) 58 Darshan Institute of Engineering & Technology
Assembler pass I Algorithm 2. While next statement is not END statement e) If a declaration statement then I. code= code of the declaration statement II. size= size of memory area required by DC/DS III. loc_cntr=loc_cntr+size; IV. Generate IC ’(DL, code)’. . f) If an imperative statement then I. II. III. Consider size require by DC/DS and according to that • update Take location machine counter, then generate opcode and intermediate code acoording to that update location counter. • If operand is literal make entry Code= machine opcode from OPTAB in literal table and loc_cntr=loc_cntr+instruction length from OPTAB; update literal if operand is a literal then table pointer this_literal=literal in operand field; • If operand is label make entry in LITTAB[littab_ptr]=this_literal; symbol table, then littab_ptr= littab_ptr +1; generate IC code. else this_entry= SYMTAB entry number of operand generate IC ‘(IS, code)(S, this_entry)’; Unit – 3 : Assembler System Programming (2150708) 59 Darshan Institute of Engineering & Technology
Assembler pass I Algorithm 3. Processing END statement a) Perform step 2(b) b) Generate IC ‘(AD, 02)’ Allocate memory for literals IC unit for END c) Go to pass II Unit – 3 : Assembler System Programming (2150708) 60 Darshan Institute of Engineering & Technology
Assembler pass II Algorithm • loc_cntr : Location Counter • littab_ptr : Points to an entry in LITTAB • pooltab_ptr : Points to an entry in POOLTAB • machine_code_buffer : Area for constructing code for one statement • code_area : Area for assembling the target program • code_area_address : Contains address of code_area 1. Code_area_adress= address of code_areas; Pooltab_ptr=1; Loc_cntr=0; 2. While next statement is not an END statement a) Clear machine_code_buffer; b) if an LTORG statement i. Process literals and generate target code also increment pooltab pointer Process literals in LITTAB and assemble the literals in machine_code_buffer. ii. size= size of memory area required for literals iii. pooltab_ptr=pooltab_ptr +1; Unit – 3 : Assembler System Programming (2150708) 61 Darshan Institute of Engineering & Technology
Assembler pass II Algorithm 2. While next statement is not an END statement c) If a START or ORIGIN statement I. Update location counter loc_cntr=value specified in operand field; II. size=0; Generate target code for DC d) If a declaration statement I. If a DC statement then assemble the constant in machine_code_buffer; II. size= size of memory area required by DC/DS; e) If an imperative statement I. Get operand address from SYMTAB or LITTAB II. Assemble instruction in machine_code_buffer; III. size=size of instruction; f) Move the target code to memory location assigned for it and update location counter. If size≠ 0 then I. Get addresses from symbol table and literal table then generate target code Move contents of machine_code_buffer to the address code_area_address+loc_cntr; II. loc_cntr=loc_cntr+size; Unit – 3 : Assembler System Programming (2150708) 62 Darshan Institute of Engineering & Technology
Assembler pass II Algorithm 3. Processing END statement Generate code for LTORG and place at proper memory location a) Perform steps 2(b) and 2(f). b) Write code_area into output file. Unit – 3 : Assembler System Programming (2150708) Write entire target code to output file. 63 Darshan Institute of Engineering & Technology
End of Unit - 3 System Programming (2150708) Unit – 3 : Assembler Darshan Institute of Engineering & Technology
- Slides: 63