Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views18 pages

May 19 SPCC

Uploaded by

s9322006781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views18 pages

May 19 SPCC

Uploaded by

s9322006781
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Q1) explain forward reference problem and how it is handled in assembler design

 The forward reference problem occurs when an instruction or symbol in an assembly


language program refers to a label or symbol that is defined later in the program. This creates
a challenge for the assembler because it needs to generate machine code or resolve
addresses for these references, but the necessary information may not be available at the
time of the reference.

Here's how the forward reference problem typically arises:

Sequential Processing: Assemblers usually process assembly language programs sequentially,


line by line. When an instruction refers to a label defined later in the program, the assembler
encounters a forward reference.
Unresolved Symbols: At the point of encountering a forward reference, the assembler may
not have processed the label or symbol being referred to. This means it doesn't have the
necessary information about the address or value associated with that label.
Address Resolution: Assemblers need to generate machine code or resolve addresses for
instructions. When a forward reference is encountered, the assembler may not know the
exact address to use in the machine code or how to properly generate the instruction.

To handle the forward reference problem, assemblers typically use a two-pass or multi-pass
approach:

First Pass: In the first pass, the assembler reads the entire source code and builds a symbol
table. It records the addresses or values associated with each label or symbol encountered.
However, it doesn't generate any machine code in this pass.

Second Pass: In the second pass (or subsequent passes), the assembler generates the actual
machine code. Now that it has the symbol table from the first pass, it can resolve any forward
references by looking up the addresses or values of the symbols in the symbol table.
Q2) Macro and Macro Expansion
 Macro
• Formally, macro instructions (often called macro) are single-line abbreviations for groups of
instructions.
• For every occurrence of this one-line macro instruction within a program, the instruction
must be replaced by the entire block.
• The advantages of using macro are as follows:
- Simplify and reduce the amount of repetitive coding.
- Reduce the possibility of errors caused by repetitive coding.
- Make an assembly program more readable.

Macro Expansion

A macro call in a program leads to macro expansion. To expand a macro, the name of the
macro is placed in the operation field, and no special directives are necessary. During macro
expansion, the macro name statement in the program is replaced by the sequence of
assembly statements.
Let us consider the following example:

START 100
A DS 1
B DS 1
+ MOVER REG A
+ ADD REG B

+ MOVEM REG A
PRINT A
STOP
END
The statements marked with a ‘+’ sign in the preceding label field denote the expanded

code and differentiate them from the original statements of the program.
Q3) Find FIRST & FOLLOW for the following grammar
S→Bb | Dd
B→aB | €

D→cD | €
 1. FIRST:
FIRST(a) = {a} (terminal symbol)
FIRST(ε) = {ε} (empty string)

FIRST(B):
FIRST(aB) = {a} (a can appear first)
FIRST(ε) = {ε} (empty string can appear first)
Considering both options for B, FIRST(B) = {a, ε}

FIRST(D):
FIRST(cD) = {c} (c can appear first)
FIRST(ε) = {ε} (empty string can appear first)
Considering both options for D, FIRST(D) = {c, ε}

FIRST(S):
FIRST(Bb) = FIRST(B) = {a, ε} (B can derive empty string)
FIRST(Dd) = FIRST(D) = {c, ε} (D can derive empty string)
FIRST(S) = {a, c, ε} (considering both options for S)
2. FOLLOW:
 FOLLOW(S) = {$} (end of input symbol) **Since S is the start symbol** *
 FOLLOW(B): * FOLLOW(B) appears after B in S → Bb. So, FOLLOW(B) = FOLLOW(S) ={$}

 FOLLOW(D): FOLLOW(D) appears after D in S → Dd. So, FOLLOW(D) = FOLLOW(S) = {$}

Q4) Generate three address code for following code


while(a<b) do
if(c<d) then
x=y+2
else
x=y-2
 L1: # Label for the beginning of the loop
if a < b goto L2 # Compare a and b
goto L5 # Exit loop if a is not less than b

L2: # Label for the beginning of the if block


if c < d goto L3 # Compare c and d
x=y+2 # Assign x = y + 2 if c is less than d
goto L4 # Jump to the end of the if-else block

L3: # Label for the beginning of the else block


x=y-2 # Assign x = y - 2 if c is not less than d

L4: # Label for the end of the if-else block


goto L1 # Jump back to the beginning of the loop

L5: # Label for the exit of the loop


Q5) With reference to assembler explain the following table with suitable example
MOT, POT, ST, BT


Machine Operation Table (MOT):

 Stores information about each machine instruction.


 Includes details like opcode (machine code equivalent), instruction length, and
operand format.
 Used by the assembler to determine instruction size and generate machine code.

Pseudo-Operation Table (POT):

 Contains information on how to handle special instructions called pseudo-ops (don't


translate to machine code directly).
 Common pseudo-ops include START, END, DS (data storage), and DC (define constant).
 The POT guides the assembler's actions for each pseudo-op.
Symbol Table (ST):

 Created during the first assembly pass.


 Stores all symbol names (labels) encountered in the code along with their
corresponding memory addresses.
 Used in the second pass to replace symbolic references with actual memory
addresses.

Base Table (BT) (Optional):

 Not present in all assemblers.


 Used in architectures with base registers for memory addressing.
 Tracks the active base register and its value.
 Helps adjust memory addresses based on the base register for relative addressing
modes.
Q6) Different code optimization techniques

 Code optimization in compiler design refers to the process of improving the efficiency
and performance of a program by transforming the code to produce more optimized
machine code. Here are some common techniques used for code optimization:

1. Dead Code Elimination: Remove code that has no effect on the program’s output, such
as unused variables, unreachable code, or statements that don’t modify any variables.

2. Common Subexpression Elimination: Identify identical subexpressions within a


program and replace them with a single computation, storing the result in a
temporary variable.

3. Loop Optimization: Optimize loops to minimize the number of iterations or eliminate


redundant calculations within the loop. This can include techniques like loop unrolling,
loop fusion, loop interchange, and loop-invariant code motion.
4. Strength Reduction: Replace expensive operations (such as multiplication or division)
with cheaper operations (such as addition or bit shifting) when the results are
equivalent.
5. Register Allocation: Assign variables to CPU registers to reduce memory accesses, as
registers are faster to access than memory.
6. Inline Expansion: Replace function calls with the actual code of the function to reduce
the overhead of function call instructions.

7. Data Flow Analysis: Analyze how data flows through a program to identify
opportunities for optimization, such as identifying variables that can be stored in
registers or identifying variables that are not used after a certain point.
8. Code Reordering: Reorder instructions to optimize instruction pipelining, reduce
dependencies, and exploit parallelism.

9. Compiler-specific optimizations: Different compilers may have their own set of


optimizations specific to their target platform. These optimizations take advantage of
specific hardware features or architectural characteristics.
Q7) Apply dead code elimination technique for following code
int count;
void foo()
{
int i;
i=1 ;
count=1;
count=2;
return
count=3;
}

Explanation:

The line count = 3; is considered dead code because it occurs after the return
statement.
The return statement terminates the function execution, and any code following it
won't be reached.
Code After Dead Code Elimination:
int count;
void foo()
{
int i;
i = 1;
count = 1;
count = 2;
return;
}
Q8) Define leŌ recursion? Eliminate leŌ recursion from the following grammar
S -> (L) | x
L -> L,S |S


EliminaƟon of LeŌ Recursion:
We can apply the following transformaƟon to eliminate leŌ recursion from the grammar:
S -> (L) | x
L -> SL'

L' -> ,SL' | ε


In this transformed grammar:
In the producƟon rule for L, we replace the leŌ recursion L -> L,S with a non-leŌ recursive
version
L -> SL'.
We introduce a new non-terminal L' to handle the recursion. The producƟon rule for L'
represents the ,S part of the original rule L -> L,S, and it can derive ,SL' or ε (epsilon), which
represents the empty string.

Q10) draw flowchart of pass 1 of two pass assembler design and explian in detail
Q11) Different features of macro with example

Optimization Macros:

Macros can be used to optimize code by replacing repetitive or computationally expensive


operations with more efficient alternatives.

Example:
#define SQUARE(x) ((x) * (x))
Usage:
int result = SQUARE(5); // Expands to (5 * 5)
Debugging Macros:

Macros can be utilized for debugging purposes, enabling the inclusion of debugging
statements in the code that are conditionally compiled based on debug flags.
Example:
#ifdef DEBUG
#define DEBUG_PRINT(x) printf("Debug: %s\n", x)
#else

#define DEBUG_PRINT(x)
#endif
Usage:
DEBUG_PRINT("Value of x: 10"); // Only compiled if DEBUG flag is defined
Conditional Compilation Macros:

Macros are used for conditional compilation, allowing different parts of the code to be
compiled based on predefined conditions or configuration options.

Example:
#define USE_FEATURE_A
Usage:
#ifdef USE_FEATURE_A
// Code utilizing feature A
#endif
Code Generation Macros:
Macros are employed for generating code snippets or boilerplate code, reducing redundancy
and enhancing code maintainability.
Example:

#define DECLARE_VARIABLE(type, name) type name


Usage:
DECLARE_VARIABLE(int, x); // Expands to int x;
DECLARE_VARIABLE(float, y); // Expands to float y;
Macro Functions:

Macros can be defined as function-like macros, enabling parameterized code generation and
customization.
Example:
#define MAX(x, y) ((x) > (y) ? (x) : (y))
Usage:
int result = MAX(10, 20); // Expands to ((10) > (20) ? (10) : (20))

Code Inlining:
Macros are utilized for inlining code, allowing small and frequently used code snippets to be
inserted directly into the calling code, thus reducing function call overhead.
Example:
#define MIN(x, y) ((x) < (y) ? (x) : (y))
Usage:
int result = MIN(a, b); // Expands to ((a) < (b) ? (a) : (b))

Q12) Difference between Application Software And System Software


System Software Application Software

System Software maintains the system


Application software is built for
resources and gives the path for
specific tasks.
application software to run.

Low-level languages are used to write the While high-level languages are used
system software. to write the application software.

While it’s a specific purpose


It is general-purpose software.
software.

Without system software, the system stops While Without application software
and can’t run. system always runs.

System software runs when the system is


While application software runs as
turned on and stops when the system is
per the user’s request.
turned off.

Example: System software is an operating Example: Application software is


system, etc. Photoshop, VLC player, etc.

Application software programming is


System Software programming is more
simpler in comparison to system
complex than application software.
software.

The Software that is designed to control, A set of computer programs installed


integrate and manage the individual in the user’s system and designed to
hardware components and application perform a specific task is known as
software is known as system software. application software.

A system software operates the system in


Application software runs in the front
the background until the shutdown of the
end according to the user’s request.
computer.

The system software has no interaction Application software connects an


with users. It serves as an interface intermediary between the user and
between hardware and the end user. the computer.

Application software is dependent on


System software runs independently. system software because they need
a set platform for its functioning.
Q13) Explain Different Function of loader
Q14) Difference Between Synthesized and Inherited Attribute
Q15) Explain different types of loader in detail

You might also like