Documente Academic
Documente Profesional
Documente Cultură
First Pass
--------LTORG =5 =1 NEXT SUB AREG,=10 -------LAST STOP ORIGIN LAST+2 AN DS END =10 1
START 200 MOVER AREG, =5 LOOP MOVEM AREG,AN ---------ADD CREG, =1 200) 201) 202) 203) 204)
--------LTORG =5 =1 NEXT SUB AREG,=10 -------LAST STOP ORIGIN LAST+2 AN DS END =10 1
205) 206)
207)
208) 209) (ORIGIN indicates LC should be set to address given in operand)
211)
212)
SYMBOL
LOOP NEXT LAST A
ADDRESS
LENGTH
literal
=5 =1 =10
address
LITTAB
SYMBOL
LOOP NEXT LAST A
ADDRESS
201 207 209 211
LENGTH
literal
=5 =1 =10
address
205 206 212
LITTAB
#3
1. Initialize Loc_cntr=0;
Pooltab_ptr=1;pooltab[1]=1;
Litttabptr=1; 2. While next statement is not END statement (a) if label is present then this_label=symbol in label field ;
Enter(this_label,loc_cntr) in SYMTAB
(b) if START or ORIGIN statement then loc_cntr=value specified in operand field
loc_cntr=loc_cntr+size generate IC (Intermediate Code) d) If an EQU statement then this_addr= value of <address spec>
(ii) pooltab_ptr=pooltab_ptr+1
(iii) POOLTAB[pooltab_ptr]=littab_ptr
LITTAB[POOLTAB[pooltab_ptr]]=LITTAB[POOLTAB[1]]=LITTAB[1]
(f) If an Imperative Statement then (i) code=machine opcode from OPTAB (ii) loc_cntr=loc_cntr+ instruction length from OPTAB (iii) if operand is a literal then
else
this_entry=SYMTAB entry number of operand generate IC 3) Generate IC go to Pass II
Address
Mnemonic Opcode
ADD
Operands
210)
AREG, VAR
it contains a pair of
( statement class, code) where statement class can be one of (i) Imperative Stmt (IS) (ii) Declaration Stmt (DL) (iii) Assembler Directive (AD) For imperative Stmt code=instruction opcode For DL Stmt code = 1 for DC and 2 for DS For AD Stmt code = 1 for START 2 for END 3 for ORIGIN 4 for EQU 5 for LTORG
Example of IC
START 200
LOOP MOVER AREG, A
(AD,01) (C,200)
(IS, 09) (1) (S,01) { HERE (IS,09 ) REPRESENT IMPERATIVE STMT AND ITS OPCODE 09 AND (S,01) REPRESENT SYMBOL A AND ITS ENTRY NUMBER IN SYMTAB)
--------
-------SUB AREG, =1
If we are processing a forward reference , It is necessary to enter Symbol in SYMTAB (entry no). At this point address and length field of symbol can not be filled.
So we can use VARIANT II method
PASS2 OF ASSEMBLER
It has been assumed that the target code is to be assembled in the area named code_area. 1. code_area_address : = address of code_area; 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 in LITTAB [POOLTAB[pooltab_ptr]] assemble the literals in machine_code_buffer. (ii) size : = size of memory area required for literals; (iii) pooltab_ptr : = Pooltab_ptr+1; c) If a START or ORIGIN statement then (i) loc_cntr : = value specified in operand field; (ii) size : = 0; 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) If size != 0 then (i) Move contents of machine_code_buffer to the address code_area_address + size (ii) Loc_cntr : = loc_cntr + size; 3) (Processing of END Statement) Perform steps 2(b) and 2(f) Write code_area into output file.
Explanation of pass 2
Suppose we are using starting address of code area =500 If START stmt is present then it shows that we have to load operand (address) into the loc_cntr. And the size of instruction is =0 If DC/DS is present than size = size of memory area required.