Вы находитесь на странице: 1из 21

ASSEMBLER

First Pass

START 200 MOVER AREG,=5 LOOP MOVEM AREG,AN ---------ADD CREG,=1

--------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)

SYMTAB
SYMBOL
LOOP NEXT LAST A

LITTAB

ADDRESS

LENGTH

literal
=5 =1 =10

address

OPTAB

Mnemonic opcode MOVER DS START

class IS DL AD

Mnemonic info (04,1) R#7 R#11

POOLTAB

LITERAL NO. #1 #3

SYMTAB
SYMBOL
LOOP NEXT LAST A

LITTAB

ADDRESS
201 207 209 211

LENGTH

literal
=5 =1 =10

address
205 206 212

OPTAB

Mnemonic opcode MOVER DS START

class IS DL AD

Mnemonic info (04,1) R#7 R#11

POOLTAB

LITERAL NO. #1 #3

Start

Initialize Data Structures D Read Next Statement

Separate Label, Mnemonic and operand Fields Yes

Label Present
No Enter(Label, LC) into Symbol Table

Type of Instruction IS B LC LC + Length DL AD C

Literal used

No

Calculate Storage Size

Type of Directive ORIGIN

Yes Enter Into LITTAB

LTORG END Allocate Literals, Update LITTAB, POOLTAB

Evaluate Operand address

Correct SYMTAB Entry

Yes

END
No

Pass II

Update LC

Generate Intermediate Code

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

(c) if a declaration statement then


code=code of declaration statement Size=size of memory area required by DC/DS

loc_cntr=loc_cntr+size generate IC (Intermediate Code) d) If an EQU statement then this_addr= value of <address spec>

correct the SYMTAB entry for this_label to (this_label,this_addr)


e) if an LTORG statement then (i) Process literals of current pool. (repeat process for all the literals in the pool) LITTAB[POOLTAB[pooltab_ptr]]=1st literal in pool update loc_cntr accordingly.

(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

this_literal = literal in operand field


LITTAB[litttab_ptr] = this_literal littab_ptr=littab_ptr+1

else
this_entry=SYMTAB entry number of operand generate IC 3) Generate IC go to Pass II

Intermediate Code Variant I Method


The intermediate code consists of a set of IC units.

Each IC unit consists of following three fields


1) Address 2) Representation of mnemonic opcode 3) Representation of operands

Address

Mnemonic Opcode
ADD

Operands

210)

AREG, VAR

Mnemonic opcode field:-

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

Intermediate Code (operand field)


The first operand is represented by a single digit number. It can be (i) code for register (1-4 for AREG-DREG) (ii) condition code (1-6 for LT-ANY) The second operand, which is a memory operand, is represented by a pair ( operand class , code) Where operand class may be (i) C for constant (ii) S for Symbol (iii) L for literal An code may be (i) Internal representation of constant for constant (ii) ordinal number of operands entry in SYMTAB or LITTAB

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

Intermediate Code Variant II Method


LOOP MOVER AREG, A (IS,04) AREG,A So Symbolic references are not processed during pass I in variant II method

PASS2 OF ASSEMBLER

MACHINE CODE BUFFER it is an area for temporary storage. CODE AREA

it is an memory area used for target code area.

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.

Now the condition of F) is true


So code area 500 + size will be executed. And then we loc_cntr + size will be executed