one-pass assembler with nestedif else
DESCRIPTION
c cx cxTRANSCRIPT
![Page 1: One-Pass Assembler With NestedIF ELSE](https://reader035.vdocuments.us/reader035/viewer/2022071705/563db79b550346aa9a8ca1f2/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022071705/563db79b550346aa9a8ca1f2/html5/thumbnails/2.jpg)
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 NestedIFs
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](https://reader035.vdocuments.us/reader035/viewer/2022071705/563db79b550346aa9a8ca1f2/html5/thumbnails/3.jpg)
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 NESTEDIFs
![Page 4: One-Pass Assembler With NestedIF ELSE](https://reader035.vdocuments.us/reader035/viewer/2022071705/563db79b550346aa9a8ca1f2/html5/thumbnails/4.jpg)
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
===