SPCC Answer Bank
SPCC Answer Bank
5 times
4 times
3 times
2 times
1 time
# indicates 5-mark question
SPCC Question Bank
1. Introduction to System Software
1. Di erentiate between System Software and Application software. #
2. Compare Compiler and Interpreter. #
2. Assemblers
1. Explain forward reference problem with suitable example. #
2. Explain with flowchart design of two pass assembler.
3. Draw and explain the flowchart of Pass 1 of two pass assembler with suitable example.
4. State and explain the types of assembly language statements with examples.
5. Consider the following assembly program:
START 501
A DS 1
B DS 1
C DS 1
READ A
READ B
MOVER AREG, A
ADD AREG, B
MOVEM AREG, C
PRINT C
END
- Generate Pass-1 and Pass-2 and also show the content of database table involved in it.
8. Construct SLR parser for the following grammar and parse the input “()()” :
S→ (S)S | ε
9. Compare Bottom-Up and Top-Down parser. #
10. Compare Pattern, Lexeme, and token with example. #
4. What are the di erent ways for Intermediate code representation? Explain with example.
5. Generate 3 address code for the following C program and construct flow graph with the
help of basic blocks(PYQs):
6. Explain DAG and construct DAG for the following expression:
x=m+p/q-t+p/q*y
7. Write a short note on Peephole Optimization. #
8. Explain the concept of basic blocks and flow graph with example of the three-address
code.
1 2 3 4 5 6
2024 Dec 5 15 20 15 25 40
2024 May 5 15 15 15 30 35
2023 Dec 5 25 15 20 25 30
2023 May 5 25 15 15 35 25
2022 Dec 5 10 25 15 25 40
Last 5 Avg 5 15-25 15 15 25 30
*2022 May 5 15 10 20 20 25
Total 30 105 100 100 160 185
*20-marks MCQs asked in 2022 May
SPCC Answer Bank
multiple times asked questions highlighted
question asked once with red font
# indicates 5-mark question
1. Introduction to System Software
1. Di erentiate between System Software and Application software. #
2. Compare Compiler and Interpreter. #
2. Assemblers
3. Explain forward reference problem with suitable example. #
A forward reference occurs when a symbol (like a label or variable) is used in the code before it is
defined. This is a problem in single-pass assemblers, where the assembler reads the code only
once meaning it might encounter an undefined symbol with no prior knowledge of its address.
Example:
JMP LOOP ; Forward reference to label "LOOP"
MOV A, B
LOOP: ADD A, C ; Definition of the label
The assembler encounters JMP LOOP, but at that point, it doesn't know where LOOP is in
memory because it's defined later.
How Forward Reference is Solved using Forward Reference Table:
During Pass 1:
o When a symbol (label) is used before it's defined, its reference (location where it is
needed) is stored in the Forward Reference Table.
o The symbol is added to the symbol table with an undefined address.
During Pass 2:
o When the symbol is finally defined, the assembler updates the symbol table.
o The assembler then revisits the locations stored in the FRT and patches them with the
correct address.
4. Explain with flowchart design of two pass assembler.
Pass 1:
1) Location counter (LC) is initialized.
2) Source statement is read.
3) Operation code is examined to determine if it is a pseudo-opcode.
4) If it is not a pseudo-op then check MOT.
5) The matched MOT entry specifies the length of the instruction.
6) The operand field is examined for a presence of a literal.
7) If the literal is found, it is entered into the literal table (LT) for further processing.
8) The label field is examined for a presence of a symbol.
9) If a symbol is found, it is entered into the symbol table (ST).
10) Finally, the current value of LC is incremented by the length of the instruction and the copy of
the source card / Output file of PASS-1 is send to PASS-2 for further processing.
Pass 2:
1. LC is initialized.
2. Statement is read from the copy file created by PASS-1
3. Operation code is examined to determine if it is a pseudo-op.
4. If it is not a pseudo-op, MOT is examined to find a match from the source statement.
5. There are two types of instruction format:
a. RR: In RR format each of the register specific fields are evaluated.
b. RX: Both register and index fields are evaluated.
6. After the instruction is evaluated, it is put into a necessary format for the processing by the
loader.
7. A listing containing the copy of the source card is used to assign storage locations in
hexadecimal format.
5. Draw and explain the flowchart of Pass 1 of two pass assembler with suitable example.
(Refer previous answer for flowchart and theory of Pass 1)
Goal of Pass 1:
Assign addresses to all statements.
Build the Symbol Table.
Track Literals.
Symbol Table:
Literal Table:
6. State and explain the types of assembly language statements with examples.
1. Imperative Statements:
Imperative statements in assembly language are instructions that explicitly specify the
operations to be performed by the computer's CPU. These statements provide step-by-step
instructions for the processor to execute.
MOV AX, 5 ; Move the value 5 into register AX
ADD AX, 3 ; Add 3 to the value in register AX
Purpose: Directly map to machine instructions, telling the processor what actions to perform
2. Declarative statements:
Declarative statements in assembly language are used to declare data or define structures
without specifying the sequence of operations to be performed. These statements are often
used to reserve memory space for variables, constants, or data structures.
DATA SEGMENT
X DB 5 ; Declare a byte variable X with value 5
Y DW 1000 ; Declare a word (2 bytes) variable Y with value 1000
DATA ENDS
Purpose: Define data, labels, or constants, providing information for the assembler rather than
generating executable code.
a) DC (Define Constant):
Used to create and initialize constants (numbers or text).
NUM DC 10 ; Store number 10
TEXT DC 'Hi' ; Store text "Hi"
b) DS (Define Storage):
Used to reserve memory without giving it a value.
Useful for variables or bu ers that will be filled during the program.
X DS 1 ; Reserve 1 byte for variable X
BUF DS 5 ; Reserve 5 bytes for a small bu er
3. Assembler directives:
Assembler directives are special commands used by the assembler (the program responsible for
translating assembly code into machine code) to control the assembly process. These directives
provide instructions to the assembler regarding how to process and organize the code.
.MODEL SMALL ; Specify the memory model for the program
a) START:
The START directive is used to specify the starting address of the program.
START 1000 ; The program starts at address 1000
b) END:
The END directive marks the end of the assembly language program.
... END ; Marks the end of the program
c) EQU (Equate):
The EQU directive is used to assign a constant value to a symbol or label.
LENGTH EQU 100 ; Define a constant symbol LENGTH with a value of 100
d) ORG (Origin)
Sets the starting memory address.
ORG 2000 ; Start placing code/data at address 2000
e) LTORG (Literal Origin)
Tells the assembler to store any literals (constants) here.
DC X'0A' ; Define a hex value
LTORG ; Store the value here
7. Consider the following assembly program:
START 501
A DS 1
B DS 1
C DS 1
READ A
READ B
MOVER AREG, A
ADD AREG, B
MOVEM AREG, C
PRINT C
END
-Generate Pass-1 and Pass-2 and also show the content of database table involved in it.
(ChatGPT ’ed)
Pass 1: Tasks
Build Symbol Table (SYMTAB)
Generate intermediate code
Maintain location counter (LC)
Location Counter (LC) Tracking:
Symbol Table (SYMTAB):
(Assumed opcodes: MOVER – 04, MOVEM – 05, ADD – 01, READ – 09, PRINT – 10, START – 01,
END – 02)
Pass 2: Tasks
Generate final machine code using symbol table and intermediate code
Machine Code (Pass 2 Output)
Example:
MACRO
PRINTMSG &FLAG
IF &FLAG EQ 1
MVI A, 'Y' ; Load Yes
ELSE
MVI A, 'N' ; Load No
ENDIF
MEND
To construct the necessary data structures after Pass-1 of a two-pass macro processor, we
identify:
1. Macro Name Table (MNT)
2. Macro Definition Table (MDT)
3. Argument List Array (ALA)
15. Write a short note on: Macro facilities. #
A macro facility refers to the set of features or functions that enable the processing and
expansion of macros. A list of common macro facilities within macro processors:
1. Macro Definition
o Allows the user to define a macro, typically with a name and a sequence of
instructions or statements (e.g., MACRO in assembly language).
2. Macro Expansion
o The macro processor replaces each macro call in the code with the defined macro
content, performing text substitution.
3. Parameters
o Macros can accept parameters, allowing for more flexible and reusable macro
definitions (e.g., MACRO X, Y).
4. Conditional Assembly
o Allows macros to be conditionally expanded or not, based on certain conditions
(e.g., using IF, ELSE, ENDIF).
5. Recursive Macros
o Some macro processors allow macros to call other macros or even themselves,
which enables more complex expansions.
16. Explain Macro and Macro expansion with example. #
Macro: A macro is a sequence of instructions grouped under a single name that can be reused
multiple times in an assembly program. It is a way to define code templates that get expanded
wherever they are called, reducing redundancy and improving code readability.
Macro Expansion: When a macro is called in the program, the assembler replaces the macro
call with the actual sequence of instructions defined in the macro. This process is known as
macro expansion.
Example:
ADDNUM MACRO A, B
MOV AX, A ;
ADD AX, B
ENDM
This creates a macro named ADDNUM that takes two values (A and B) and generates two
instructions.
Macro Call: ADDNUM 5, 10
When this line is used, the assembler replaces it with:
MOV AX, 5
ADD AX, 10
This is called macro expansion.
17. Explain di erent features of macros with suitable example.
1. Code Reusability
o Macros help avoid the repetition of code by defining a set of instructions or
statements that can be reused multiple times in a program. Once a macro is defined,
it can be invoked as many times as needed, saving time and reducing errors.
o Example: MACRO MOVE 2, 3 can be called multiple times to move to the same
coordinates.
2. Parameterization
o Macros can accept parameters, which makes them flexible. Instead of writing the
same sequence of instructions multiple times, you can pass di erent values each
time the macro is invoked, making the macro more dynamic and adaptable.
o Example: MACRO ADD a, b expands to a + b when called as ADD 5, 3.
3. No Memory Overhead
o Unlike function calls, macros do not create new memory locations for arguments or
return addresses. They are expanded directly into the program code by the macro
processor at the preprocessing stage, which reduces the memory overhead typically
associated with function calls.
o Example: MACRO PRINT 5 directly inserts the print instruction without using memory
for a function call.
4. Faster Execution
o Since macros are expanded inline by the macro processor, they eliminate the need for
function calls or context switches, which leads to faster execution. The code for the
macro is directly inserted into the program at the point of use.
o Example: MACRO INC X expands to X = X + 1, directly updating the value.
5. Conditional Expansion
o Macros can include conditional logic, allowing for di erent code expansions
depending on certain conditions or flags. This makes macros adaptable to di erent
contexts or environments, such as debugging or platform-specific code.
o Example: MACRO DEBUG_LOG only expands if DEBUG is set, otherwise does nothing.
6. Nested Macros
o Macros can invoke other macros, allowing for more complex, modular, and structured
macro definitions. This capability lets a simple macro rely on the functionality of other
macros, making code easier to maintain and extend.
o Example: MACRO CUBE X can call SQUARE X to compute X * X * X.
4. Loaders and Linkers
18. What are the functions of a Loader. #
A loader is a system program responsible for loading executable programs into memory for
execution. It performs the following key functions:
1. Allocation: Assigns the necessary memory space for the program in the main memory.
2. Linking: Combines two or more object programs or modules and supplies necessary
information.
3. Relocation: It modifies the object program so that it can be loaded at an address di erent
from its original location
4. Loading: It loads the object program into the main memory for execution.
4
19. Explain design of Direct Linking Loader with suitable example. Discuss the databases
used.
Questions asked breakdown:
Explain Direct Linking Loader in detail. X2
Explain design of Direct Linking Loader. X2
Discuss the databases used in Direct Linking Loader.
Explain Direct Linking Loader with suitable example.
1. Purpose:
o It allows programs to use shared libraries at runtime instead of linking them directly
during the compilation process.
o It ensures that external code or resources are loaded into memory only when
needed, saving memory and disk space.
2. Operation:
o When a program starts, the loader checks for the dynamic dependencies (libraries)
defined in the program’s binary (e.g., ELF or PE files).
o It then loads these libraries into memory. If a library is already loaded, it is reused
(shared among processes).
o The loader resolves symbols (function names, variables) from the libraries and links
them with the program.
3. Process Flow:
o Load Libraries: The loader identifies the shared libraries required by the program
and loads them into memory.
o Relocation: If the library code isn't loaded at the exact address expected, the loader
adjusts the program’s address references to point to the correct memory locations.
o Symbol Resolution: The loader resolves any symbols (functions, variables) that the
program or libraries use.
o Linking: The loader binds the resolved symbols to the actual memory locations in
the loaded libraries.
o Execution: Once the libraries are linked, the program starts executing with access to
the dynamically linked functions.
4. Advantages:
o Memory E iciency: Multiple programs can share a single copy of a library.
o Modularity: Programs can be updated without recompiling them, as long as the
interface to the library remains the same.
22. What is relocation and linking concept in Loaders. #
Relocation:
Relocation is the process of modifying address-dependent instructions in a program so it can be
loaded at a di erent memory location than originally specified. Some instructions contain
absolute addresses; the loader updates these using relocation information to fit the current
memory space.
Linking:
Linking is the process of combining multiple object modules into a single executable. It resolves
external references between programs or modules by connecting function calls and variable
references to their correct definitions across modules.
Together, relocation and linking ensure that programs are loaded correctly and external
dependencies are resolved before execution.
5. Compilers: Analysis Phase
23. Explain the di erent phases of compiler with suitable example; or a given statement.
(Previously asked statements)
i. a = a * b – 5 * 3 / c
ii. P = Q + R – S * 3
iii. a=b*c+10
Analysis Phase:
An intermediate representation is created from the given source code. It consists of the
following:
Lexical Analyzer: The lexical analyzer divides the program into “tokens.
Syntax Analyzer: The Syntax analyzer recognizes “sentences” in the program using the
syntax of the language
Semantic Analyzer: Semantic analyzer checks the static semantics of each construct.
Intermediate Code Generator: Intermediate Code Generator generates “abstract” code.
Synthesis Phase:
An equivalent target program is created from the intermediate representation. It has two parts :
Code Optimizer: Enhances intermediate code by eliminating ine iciencies.
Code Generator: Translates abstract intermediate code into specific machine instructions.
iii. a=b*c+10
i.
24. Construct LL(1) parsing table for the given grammar and state whether the given grammar
is LL(1) or not. (Previously asked questions)
25. Write a short note on: Syntax directed translation. #
Syntax-Directed Translation (SDT) combines grammar with semantic rules to assign meaning to
syntactic structures. Each non-terminal in SDT can have attributes, evaluated using rules
associated with production rules. These attributes store values such as numbers, strings, or
memory locations. SDT enables translating programming constructs according to predefined
semantic rules, as seen in compiler design.
SDT (syntax-directed translation) adds 'semantics rules' or actions to CFG products. As a result,
we may refer to the grammar that has been augmented as attributed grammar
1. Constant Folding:
Constant folding evaluates expressions with constant operands at compile time and replaces
them with a single computed value.
Example:
area := (22.0 / 7.0) * r ** 2
is replaced with
area := 3.14286 * r ** 2
2. Constant Propagation:
Constant propagation replaces a variable with its assigned constant value wherever it appears in
an expression.
Example:
pi := 3.14286
area = pi * r ** 2
is replaced with
area = 3.14286 * r ** 2
3. Dead Code Elimination:
Dead code refers to portions of the program that will never be executed. If a computation’s result
is never used, it is considered dead and can be removed from the code.
Example:
i = 0;
if(i == 1) {
a = b + 5;
}
Here, the if statement is dead code because i == 1 will never be true.
2. Machine-Dependent Optimization
1. Instruction Scheduling: Reordering instructions to minimize CPU pipeline stalls and improve
execution speed.
Example:
Before:
LOAD A
MUL A, B
ADD C, A
This may stall because MUL and ADD both depend on A.
After (Reordered to reduce stall):
LOAD A
ADD C, A ; Done while MUL waits for A to be ready
MUL A, B
2. Register Allocation: E iciently assigning frequently used variables to CPU registers instead
of memory for faster access.
Example:
Before:
int a = x + y;
int b = a * 2;
'a' stored in memory, accessed again for 'b'
; After optimization
SHL R1, 1 ; shift left by 1 = multiply by 2
Replaces a costly multiply with a faster shift instruction.
34. Explain di erent issues in code generation phase of a compiler.
The code generation phase translates intermediate code into target machine code. It must
ensure that the generated code is correct, e icient, and optimized for the specific architecture.
Several issues must be handled carefully to produce high-quality code:
1. Input Data for Code Generator
The code generator takes intermediate representations such as:
Three-Address Code (quadruples, triples)
Linear Notations (prefix, postfix)
Graphical Forms (syntax trees, DAGs)
The generator receives such intermediate representations and it must understand and handle
the format accurately.
2. Target Program
Knowledge of the machine architecture and instruction set is essential. It influences whether the
code will be absolute, re-locatable, or assembly-level.
3. Instruction Selection
Choosing the right machine instructions based on the IR level and instruction set a ects
e iciency.
Example: Selecting INC A over ADD A, 1 is faster on some architectures.
4. Register Allocation and Assignment
E icient use of limited CPU registers improves performance. The code generator decides which
variables go into which registers at di erent stages.
Example: Keeping loop counters in registers avoids memory access.
5. Evaluation Order
The order of evaluating expressions a ects temporary variable usage and register pressure.
Example: In a + b * c, evaluating b * c first may save registers.
6. Machine-Specific Constraints
Some architectures have alignment rules or special registers. The code must respect such
constraints to run correctly.
7. Preservation of Correctness
The generated code must preserve the exact semantics of the source program under all
execution paths.
8. Optimization Trade-o s
The compiler must balance between compilation speed and code quality. Over-optimizing can
slow down compilation.
35. Construct three-address code for the following program: (PYQs)
i)
ii) for (i = 0; i < 10; i++)
{
if (i < 5)
a = b + c * 3;
else
x = y + z;
}
Three-Address Code (TAC):
i=0
L1: if i >= 10 goto L4
if i < 5 goto L2
t1 = y + z
x = t1
goto L3
L2: t2 = c * 3
t3 = b + t2
a = t3
L3: i=i+1
goto L1
L4: exit
iii) i = 1;
x = 0;
while (i <= n)
{
x = x + 1;
i = i + 1;
}
Three-Address Code:
i=1
x=0
L1: if i > n goto L2
t1 = x + 1
x = t1
t2 = i + 1
i = t2
goto L1
L2: exit
36. What are the di erent ways for Intermediate code representation? Explain with example.
After syntax and semantic analysis of the source program, many compilers generate an explicit
low-level or machine-like intermediate representation, which we can think of as a program for an
abstract machine.
This intermediate representation should have two important properties: it should be easy to
produce and it should be easy to translate into the target machine.
Three ways of intermediate representation:
Graphical Representation (Syntax tree & DAG)
Postfix notation
Three address code
Graphical Representations:
Syntax Tree & DAG: A Syntax Tree represents the natural hierarchical structure of a source
program. A DAG (Directed Acyclic Graph) gives the same information but in a more compact way
because common subexpressions are identified.
A syntax tree and DAG for the statement a = b * - c + b * - c are as follows:
Postfix notation:
Postfix notation is a linearized representation of a syntax tree; it is a list of the nodes of the tree in
which a node appears immediately after its children. The postfix notation for the syntax tree
given above is:
a b c - * b c - * + assign
Three-Address Code (TAC):
(Also applicable for Write a short note on: Three-address code representation. # [Asked twice])
2. Triples
Uses only three fields: op, arg1, and arg2.
Avoids naming temporary variables by using statement positions.
Another example:
39. Write a short note on Peephole Optimization. #
Peephole optimization is a machine-dependent, local optimization technique used in the final
stages of compilation. It examines a small set of instructions (a "peephole") in the generated
code and looks for patterns that can be replaced with simpler, faster, or shorter equivalents
without changing the program’s behaviour.
Key Characteristics:
Works on small windows of code (typically 1 to 5 instructions).
Focuses on improving performance and reducing code size.
Applied after intermediate code is generated, just before final machine code.
Common Optimizations Include:
Constant folding: e.g., ADD R1, 0 → (remove)
Strength reduction: e.g., MUL R1, 2 → SHL R1, 1
Eliminating unreachable or dead code
Example:
; Before optimization
MUL R1, R1, 2
; After optimization
SHL R1, 1 ; shift left by 1 = multiply by 2
Replaces a costly multiply with a faster shift instruction.
40. Explain the concept of basic blocks and flow graph with example of the three-address
code.
Basic Block: A basic block is a sequence of consecutive instructions in a program that:
Has only one entry point
Has only one exit point
No jumps or labels in the middle of the block.
Example:
a=5
b = 10
c=a+b
This is a basic block. If there were a jump or label between these, it would break the block.
Flow Graph: A flow graph is a directed graph where:
Nodes represent basic blocks.
Edges represent control flow (how control moves from one block to another).
Example with Three-Address Code (TAC):
C program:
if (a < b)
c = a + b;
else
c = a - b;
d = c * 2;
Three-Address Code (TAC):
1. if a < b goto L1
2. else goto L2
3. L1: t1 = a + b
4. c = t1
5. goto L3
6. L2: t2 = a - b
7. c = t2
8. L3: t3 = c * 2
9. d = t3
Flow graph:
~ AJ