one-pass assembler with nestedif else

4
System Software Internals Group Activity – 1 procedure GROUP_DETAILS   begin     var groupNo := 7;     ..     ..     groupNo7.append(“hemanth”, “juhi”, “saivaishnavi”, “sowmiya”, “loganathan”);   end Algorithm for a 'One-Pass' Macro Assembler: (With modifications to include IF-ELSE-ENDIF and NESTED-IFs structure) begin  {macro processor} EXPANDING : = FALSE while  OPCODE  ‘END’ do begin           GETLINE               PROCESSLINE      end   {while} end  {macro processor} Fig 1. Algorithm for Macro Processor procedure  PROCESSLINE begin     search NAMTAB for OPCODE          if  found then           EXPAND      else if  OPCODE = ‘MACRO’ then           DEFINE       else  write source line to expanded file end  {PROCESSLINE} Fig 2. Algorithm for Processing a line

Upload: hemanth

Post on 02-Dec-2015

5 views

Category:

Documents


1 download

DESCRIPTION

c cx cx

TRANSCRIPT

Page 1: One-Pass Assembler With NestedIF ELSE

System Software InternalsGroup Activity – 1

procedure GROUP_DETAILS  begin    var groupNo := 7;    ..    ..    groupNo7.append(“hemanth”, “juhi”, “saivaishnavi”,“sowmiya”, “loganathan”);  end

Algorithm for a 'One-Pass' Macro Assembler:(With modifications to include IF-ELSE-ENDIF and NESTED-IFs structure)

begin {macro processor}EXPANDING : = FALSEwhile OPCODE   ‘END’ do≠

begin           GETLINE               PROCESSLINE      end  {while}end {macro processor}

Fig 1. Algorithm for Macro Processor

procedure PROCESSLINEbegin

     search NAMTAB for OPCODE          if found then           EXPAND      else if OPCODE = ‘MACRO’ then           DEFINE       else write source line to expanded file

end {PROCESSLINE} 

Fig 2. Algorithm for Processing a line

Page 2: One-Pass Assembler With NestedIF ELSE

procedure EXPAND  begin     EXPANDING : = TRUE          get first line of macro definition {prototype} from DEFTAB       set up arguments from macro invocation in ARGTAB          write macro invocation to expanded file as a comment          while not end of macro definition do           begin                GETLINE                   search NAMTAB for OPCODE

if OPCODE='IF' thenPROCESS_NESTED_IF_ELSE

elsePROCESSLINE

end {while}        EXPANDING : = FALSE

end  {EXPAND}

Fig 3. Algorithm for Expansion with Nested­IFs

What actually should PROCESS_NESTED_IF_ELSE do?

1. Consider all statements with 'IF-ELSE-ENDIF' structure.

2. Consider all statements without 'IF-ELSE-ENDIF' structure.

3. Consider all statements with NESTED-IF structure.

How are the NESTED-IFs handled ?

1. Create a variable 'LEVEL' and initialize it to '1'. ( Meaning the first IF encountered)

2. Process the 'IF' part (or) the 'ELSE' part for the first 'IF' section.

3. If the 'IF' part (or) the 'ELSE' part contains another 'IF-ELSE' part, increment LEVEL by 1 an continue step 2 and 3.

4. If 'ENDIF' part is encountered, then decrement 'LEVEL' by 1 and continue processing the lines for remaining 'IF's already encountered.

5. Continue this steps until all 'IF-ELSE' structure is analysed. (ie., 'LEVEL' comes back to 1)

6. Stop the program when 'ENDIF' is encountered and LEVEL=1.

Page 3: One-Pass Assembler With NestedIF ELSE

Appended Algorithm for Processing NESTEDIFs :

procedure PROCESS_NESTED_IF_ELSE   begin      LEVEL := 1   while LEVEL > 0 do      begin

  GETLINE  pass := evaluate the boolean 

expression  if pass = TRUE then     while OPCODE != 'ELSE'

   begin           GETLINE

 if OPCODE = 'IF' then    LEVEL := LEVEL + 1

      elseif OPCODE = 'ENDIF' thenLEVEL := LEVEL – 1

      PROCESSLINE                  end

 elsewhile OPCODE != 'ENDIF' do   begin      GETLINE

 if OPCODE = 'IF' then    LEVEL := LEVEL + 1; elseif OPCODE = 'ENDIF' then    LEVEL := LEVEL – 1

                     PROCESSLINE   end

         end   end

Fig 4. Algorithm that Expands Macros with NESTED­IFs

Page 4: One-Pass Assembler With NestedIF ELSE

procedure GETLINE   begin   if EXPANDING then         begin

    get next line of macro definition from DEFTAB         substitute arguments from ARGTAB for positional notation      end {if}      else         read next line from input file   end {GETLINE}

Fig 5. Algorithm for Getting Next Line from Input or DEFTAB

===