Sunteți pe pagina 1din 17

(MCA) Semester 3 MC0073 System Programming 4 Credits (Book ID: B0811) Assignment Set 1 (60 Marks)

Q1) Describe the following with respect to Language Specification: A) Programming Language Grammars B) Classification of Grammars C) Binding and Binding Times Ans:1 Programming Language Grammars: The lexical and syntactic features of a programming language are specified by its grammar. This section discusses key concepts and notions from formal language grammars. A language L can be considered to be a collection of valid sentences. Each sentence can be looked upon as a sequence of words and each word as a sequence of letters or graphic symbols acceptable in L. A language specified in this manner is known as& formal language. A formal language grammar is a set of rules which precisely specify the sentences of L. It is clear those natural languages arenot formal languages due to their rich vocabulary. However, PLs are formal languages. Terminal symbols, alphabet and strings: The alphabet of L, denoted by the Greek symbol, is the collection of symbols in its character set. We will use lower case letters a, b, c, etc. to denote symbols in. A symbol in the alphabetise known as terminal symbol (T) of L. The alphabet can be represented using the mathematical notation of a set, e.g. = {a, b, z, 0, l, 9}Here the symbols {, , and} are part of the notation. We call them met symbols to differentiate them from terminal symbols. Throughout this discussion we assume that met symbols are distinct from the terminal symbols. If this is not the case, i.e. if a terminal symbol and a met symbol are identical, we enclose the terminal symbol in quotes to differentiate it from the met symbol. For example, the set of punctuation symbols of English can be defined as where , denotes the terminal symbol comma. A string is a finite axy is a string over The length of a string is the number of symbols in it. Note that the . absence of any symbol is also a string, the Null string The concatenation operation combines two strings into a single string. It is used hich is formed by before the sequence of symbols forming . For string can also particip Non terminal symbols A nonterminal symbol (NT) is the name of a syntax category of a language, e.g. noun, verb, etc.An NT is written as a single capital letter, or as a name enclosed between <>, e.g. A or <Noun>. During grammatical analysis, a nonterminal symbol represents an instance of the category. Thus, < Noun >represents a noun. Productions A production, also called a rewriting rule,is a rule of the grammar. A production has the form An non terminal symbol :: = String of Ts and NTs and defines the fact that the NT on the LHS of the production can be rewritten as the string of Ts and NTs appearing on the RHS. When an NT can be written as one of many different strings, the symbol (standing for or) is used to separate the strings on the RHS, e.g.<

Article > ::- a | an |theThe string on the RHS of a production can be a concatenation of component strings, e.g. the production <Noun Phrase > ::= < Article><Noun > expresses the fact that the noun phrase consists of an article followed by a noun.Each grammar G defines a language lg. G contains an NT called the distinguished symbol or the start NT of G. Unless otherwise specified, we use the symbol S as the distinguished symbol of G. which can be applied independent of its context. These grammars are therefore knownas context free grammars (CFG). CFGs are ideally suited for programming language specification. Type 3 grammars Type-3 grammars are characterized by productions of the formA::= tB | t or A ::= Bt | t Note that these productions also satisfy the requirements of Type-2 grammars. The specific formof the RHS alternativesnamely a single T or a string containing a single T and a single NT gives some practical advantages in scanning.Type-3 grammars are also known as linear grammars or regular grammars. These are further categorized into left-linear and right-linear grammars depending on whether the NT in the RHSalternative appears at the extreme left or extreme right. Binding and BindingTimes A binding is the association of an attribute of a program entity with avalue. Binding time is the time at which a binding is performed. Thus the type attribute of variable var is bound to type, when its declaration is processed. The size attribute of type is bound to a values time prior to this binding. We are interested in the following binding times:1. Language definition time of L2. Language implementation time of L3. Compilation time of P4. Execution init time of proc5. Execution time of proc.Where L is a programming language, P is a program written in L and proc is a procedure in P. Note that language implementation time is the time when a language translator is designed. The preceding list of binding times is not exhaustive; other binding times can be defined, viz. binding gat the linking time of P. The language definition of L specifies binding times for the attributes of various entities of programs written in L

Q 2. What are data formats? Explain ASCII data formats Data format in information technology can refer to either one of:

Data type, constraint placed upon the interpretation of data in a type system Signal (electrical engineering), a format for signal data used in signal processing Recording format, a format for encoding data for storage on a storage medium File format, a format for encoding data for storage in a computer file o Container format (digital), a format for encoding data for storage by means of a standardized audio/video codecs file format Content format, a format for converting data to information o Audio format, a format for processing audio data o Video format, a format for processing video data

ASCII data format:Acronym for the American Standard Code for Information Interchange. Pronounced ask-ee, ASCII is a code for representing English characters as numbers, with each letter assigned a number from 0 to 127. For example, the ASCII code for uppercase M is 77. Most computers use ASCII codes to represent text, which makes it possible to transfer data from one computer to another.

Text files stored in ASCII format are sometimes called ASCII files. Text editors and word processors are usually capable of storing data in ASCII format, although ASCII format is not always the default storage format. Most data files, particularly if they contain numeric data, are not stored in ASCII format. Executable programs are never stored in ASCII format. The standard ASCII character set uses just 7 bits for each character. There are several larger character sets that use 8 bits, which gives them 128 additional characters. The extra characters are used to represent non-English characters, graphics symbols, and mathematical symbols. Several companies and organizations have proposed extensions for these 128 characters. The DOS operating system uses a superset of ASCII called extended ASCII or high ASCII. A more universal standard is the ISO Latin 1 set of characters, which is used by many operating systems, as well as Web browsers.

3. Explain the following with respect to the design specifications of an Assembler: A) Data Structures :The second step in our design procedure is to establish the databases that we have to work with. Pass 1 Data Structures 1. Input source program 2. A Location Counter (LC), used to keep track of each instructions location. 3. A table, the Machine-operation Table (MOT) that indicates the symbolic mnemonic, for each instruction and its length (two, four, or six bytes) 4. A table, the Pseudo-Operation Table (POT) that indicates the symbolic Mnemonic and action to be taken for each pseudo-op in pass 1. 5. A table, the Symbol Table (ST) that is used to store each label and its corresponding value. 6. A table, the literal table (LT) that is used to store each literal encountered and its corresponding assignment location. 7. A copy of the input to be used by pass 2 Pass 2 Data Structures 1. Copy of source program input to pass1. 2. Location Counter (LC)

3. A table, the Machine-operation Table (MOT), that indicates for each instruction, symbolic mnemonic, length (two, four, or six bytes), binary machine opcode and format of instruction. 4. A table, the Pseudo-Operation Table (POT), that indicates the symbolic mnemonic and action to be taken for each pseudo-op in pass 2. 5. A table, the Symbol Table (ST), prepared by pass1, containing each label and corresponding value. 6. A Table, the base table (BT), that indicates which registers are currently specified as base registers by USING pseudo-ops and what the specified contents of these registers are. 7. A work space INST that is used to hold each instruction as its various parts are being assembled together. 8. A work space, PRINT LINE, used to produce a printed listing. 9. A work space, PUNCH CARD, used prior to actual outputting for converting the assembled instructions into the format needed by the loader. 10. An output deck of assembled instructions in the format needed by loader

B) pass1 & pass2 Assembler flow chart Pass Structure of Assemblers Here we discuss two pass and single pass assembly schemes in this section: Two pass translation Two pass translation of an assembly language program can handle forward references easily. LC processing is performed in the first pass and symbols defined in the program are entered into the symbol table. The second pass synthesizes the target form using the address information found in the symbol table. In effect, the first pass performs analysis of the source program while the second pass performs synthesis of the target program. The first pass constructs an intermediate representation (IR) of the source program for use by the second pass. This representation consists of two main componentsdata structures, e.g. the symbol table, and a processed form of the source program. The latter component is called intermediate code (IC). Single pass translation LC processing and construction of the symbol table proceed as in two pass translation. The problem of forward references is tackled using a process called back patching. The operand

field of an instruction containing a forward reference is left blank initially. The address of the forward referenced symbol is put into this field when its definition is encountered. Look at the following instructions: START 101 READ N 101) + 09 0 113 MOVER BREG, ONE 102) + 04 2 115 MOVEM BREG, TERM 103) + 05 2 116 AGAIN MULT BREG, TERM 104) + 03 2 116 MOVER CREG, TERM 105) + 04 3 116 ADD CREG, ONE 106) + 01 3 115 MOVEM CREG, TERM 107) + 05 3 116 COMP CREG, N 108) + 06 3 113 BC LE, AGAIN 109) + 07 2 104 MOVEM BREG, RESULT 110) + 05 2 114 PRINT RESULT 111) + 10 0 114 STOP 112) + 00 0 000 N DS 1 113) RESULT DS

1 114) ONE DC 1 115) + 00 0 001 TERM PS 1 116) END In the above program, the instruction corresponding to the statement MOVER BREG, ONE can be only partially synthesized since ONE is a forward reference. Hence the instruction opcode and address of BREG will be assembled to reside in location 101. The need for inserting the second operands address at a later stage can be indicated by adding an entry to the Table of Incomplete Instructions (TII). This entry is a pair (instruction address>, <symbol>), e.g. (101, ONE) in this case. By the time the END statement is processed, the symbol table would contain the addresses of all symbols defined in the source program and TII would contain information describing all forward references. The assembler can now process each entry in TII to complete the concerned instruction. For example, the entry (101, ONE) would be processed by obtaining the address of ONE from symbol table and inserting it in the operand address field of the instruction with assembled address 101. Alternatively, entries in TII can be processed in an incremental manner. Thus, when definition of some symbolsymb is encountered, all forward references tosymb can be processed. Design of A Two Pass Assembler

Tasks performed by the passes of a two pass assembler are as follows: Pass I: 1. Separate the symbol, mnemonic opcode and operand fields. 2. Build the symbol table. 3. Perform LC processing. 4. Construct intermediate representation. Pass II: Synthesize the target program. Pass I performs analysis of the source program and synthesis of the intermediate representation while Pass II processes the intermediate representation to synthesize the target program. The design details of assembler passes are discussed after introducing advanced assembler directives and their influence on LC processing.

4) Explain the following, a) Lexical Analsis b) Syntax Analysis. Lexical rule which govern the formation of valid lexical units in the source language. Syntax rule which govern the formation of valid statements in the source language. Semantic rule which associate meaning with valid statements of the language. Thus analysis of a source statement consists of lexical, syntax and semantic analysis.

Lexical analysis (Scanning) it identifies the lexical unit in a source statement. It then classifies the units into different lexical classes e.g.: ids, constants etc. and enter them into different tables. Lexical analysis build a descriptor called a token, for each lexical unit.

Syntax analysis (Parsing) It process the strings of token build by lexical analysis by determine the statement class, e.g. Assignment statement, if statement etc. It then builds an IC which represents the structure of the statement. The IC is passed to semantic analysis to determine the meaning of the statement

Q5). Describe the process of Bootstrapping in the context of Linkers

Boot straping: In computing, bootstrapping refers to a process where a simple system activates another more complicated system that serves the same purpose. It is a solution to the Chicken-andegg problem of starting a certain system without the system already functioning. The term is most often applied to the process of starting up a computer, in which a mechanism is needed to execute the software program that is responsible for executing software programs (the operating system). Bootstrap loading: The discussions of loading up to this point have all presumed that theres already an operating system or at least a program loader resident in the computer to load the program of interest. The chain of programs being loaded by other programs has to start somewhere, so the obvious question is how is the first program loaded into the computer .In modern computers, the first program the computer runs after a hardware reset invariably is stored in a ROM known as bootstrap ROM. as in "pulling ones self up by the bootstraps." When he CPU is powered on or reset, it sets its registers to a known state. On x86 systems, for example, the reset sequence jumps to the address 16 bytes below the top of the systems address space. The bootstrap ROM occupies the top 64K of the address space and ROM code then starts up the computer. On IBM-compatible x86 systems, the boot ROM code reads the first block of the floppy disk into memory, or if that fails the first block of the

first hard disk, into memory location zero and jumps to location zero. The program in block zero in turn loads a slightly larger operating system boot program from a known place on the disk into memory, and jumps to that program which in turn loads in the operating system and starts it. (There can be even more steps, e.g., a boot manager that decides from which disk partition to read the operating system boot program, but the sequence of increasingly capable loaders remains. Why not just load the operating system directly.Because you cant fit an operating system loader into 512 bytes. The first level loader typically is only able to load a single-segment program from a file with a fixed name in the top-level directory of the boot disk. The operating system loader contains more sophisticated code that can read and interpret a configuration file, uncompress a compressed operating system executable, address large amounts of memory (on anx86 the loader usually runs in real mode which means that its tricky to address more than 1MBof memory.) The full operating system can turn on the virtual memory system, loads the drivers it needs, and then proceed to run userlevel programs. Many Unix systems use a similar bootstrap process to get user-mode programs running. The kernel creates a process, then stuffs a tiny little program, only a few dozen bytes long, into that process. The tiny program executes a system call that runs /etc/init, the user mode initialization program that in turn runs configuration files and starts the daemons and login programs that a running system needs

6. Describe the procedure for design of a Linker. Design of a linker Relocation and linking requirements in segmented addressing

The relocation requirements of a program are influenced by the addressing structure of the computer system on which it is to execute. Use of the segmented addressing structure reduces therelocation requirements of program.

A Linker for M S-DOS Example: Consider the program of written in the assembly language of Intel 8088. The ASSUME statement declares the segment registers CS and DS to the available for memory addressing. Hence all memory addressing is performed by using suitable displacements from their contents. Translation time address o A is 0196. In statement 16, a reference to A is assembled as a displacement of 196 from the contents of the CS register. This avoids the use of an absolute address, hence the instruction is not address sensitive. Now no relocation is needed Now no relocation is needed if segment SAMPLE is to be loaded with address 2000 by a calling program (or by the OS). The effective operand address would be calculated as <CS>+0196, which is the correct address 2196.A similar situation exists with the reference to B in statement 17. The reference to B is assembled as a displacement of 0002 from the contents of the DS register. Since the DS register would be loaded with the execution time address of DATA_HERE, the reference to B would be

Though use of segment register reduces the relocation requirements, it does not completely eliminate the need for relocation. Consider statement 14 .MOV AX, DATA_HER

Which loads the segment base of DATA_HERE into the AX register preparatory to its transfer into the DS register? Since the assembler knows DATA_HERE to be a segment, it makes provision to load the higher order 16 bits of the address of DATA_HERE into the AX register. However it does not know the link time address of DATA_HERE, hence it assembles the MOV instruction in the immediate operand format and puts zeroes in the operand field. It also makes an entry for this instruction in RELOCTAB so that the linker would put the appropriate addressing the operand field. Inter-segment calls and jumps are handled in a similar way. Relocation is somewhat more involved in the case of intra-segment jumps assembled in the FAR format. For example, consider the following program: FAR_LAB EQU THIS FAR; FAR_LAB is a FAR label FAR_LAB ; A FAR jump

JMP FAR_LAB; A FAR jump Here the displacement and the segment base of FAR_LAB are to be put in the JMP instruction itself. The assembler puts the displacement of FAR_LAB in the first two operand bytes of the instruction, and makes a RELOCTAB entry for the third and fourth operand bytes which are to hold the segment base address. A segment like ADDR_A DW OFFSET A(which is an address constant) does not need any relocation since the assemble can itself put the required offset in the bytes. In summary, the only RELOCATAB entries that must exist for a program using segmented memory addressing are for the bytes that contain a segment base address. For linking, however both segment base address and offset of the external symbol must be computed by the linker. Hence there is no reduction in the linking requirements

Master of Computer Application (MCA) Semester 3 MC0073 System Programming 4 Credits (Book ID: B0811) Assignment Set 2 (60 Marks)

1.Write short notes on video controller ? Ans: Video controller is used to control the operation of the display d e v i c e . A f i x e d a r e a o f t h e system is reserved for the frame buffer, and the video controller is given direct access to the frame buffer memory.

2. Write about Deterministic and Non-Deterministic Finite Automata with suitable numerical examples?

Ans: Deterministic finite automata Definition: Deterministic finite automata (DFA)A deterministic finite automaton (DFA) is a 5-tuple: (S , ,T , s, A) an alphabet () a set of states (S ) a transition function (T :S S ). a start state ( sS ) a set of accept states ( A S )The machine starts in the start state and reads in a string of symbols from its alphabet. It use st he transition function T to determine the next state using the current state and the symbol just read. If, when it has finished reading, it is in an accept The machine starts in the start state and reads in a string of symbols from its alphabet. It uses the transition function to determine the next state using the current state and the symbol just read. If, when it has finished reading, it is in an accepting state, it is said to accept the string, otherwise it is said t o r e j e c t t h e string. The set of strings it accepts form a language, which is the language the DFA recognize. Non-Deterministic Finite Automaton (N-DFA) A Non-Deterministic Finite Automaton (NFA) is a 5-tuple: (S , ,T , s, A) an alphabet () a set of states (S ) a transition function (T :S S ). a start state ( S ) a set of accept states ( A S ))Where P (S ) is the power set of S and is the empty string. The machine starts in the start state and reads in a string of symbols from its alphabet. It uses the transition relation T to determine the next state(s) using the current state and the symbol just read or the empty string. If, when it has finished reading, it is in an accepting state, it is said to accept the string, otherwise it is said to reject the string. The set of strings it accepts form a language, which is the language the NFA recognizes.A D F A or NFA can easily be converted into a GNFA and t hen the GNFA c a n b e e a s i l y converted into a regular expression by reducing the number of states until S = { s,a}.

Deterministic Finite State Machine The following example explains a deterministic finite state machine (M ) with a binary alphabet, which determines if the input contains an even number of 0s.See the following figure M = (S , ,T , s,A) = {0, 1} S = {S 1,S 2} s=S 1 A= {S 1}The transition function T is visualized by the directed graph shown on the right, and defined as follows (S 1, 0)=S 2oT

S 1, 1) =S 1o T (S 2, 0) =S 1oT (S 2, 1) =S 2 Simply put, the state S 1 represents that there has been an even number of 0s in the input so far, while S 2 signifies an odd number. A 1 in the input does not change the state of the automaton. When the input ends, the state will show whether the input contained an even number of 0s or not. T h e r e a r e t w o m a i n methods for handling where to generate the outputs for a f inite state machine. They are called a Moore Machine and a Merely Machine, n a m e d a f t e r t h e i r r e s p e c t i v e authors.

3. Write a short note on: A) C Pre-processor for GCC version B) Conditional Assembly

Ans: The C Pre-processor for GCC version 2The C pre-processor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs. The C pre-processor provides four separate facilities that you can use as you see fit: Inclusion of header files. These are files of declarations that can be substituted into your program. Macro expansion. You can define macros, which are abbreviations for arbitrary fragments of C code, and then the C pre-processor will replace the macros with their definitions throughout the program. Conditional compilation. Using special pre-processing directives, you can include or exclude parts of the program according to various conditions. Line control. If you use a program to combine or rearrange source files into an intermediate file which is then compiled, you can use line control to inform the compiler of where each source line originally came from. ANSI Standard C requires the rejection of many harmless constructs commonly used by todays C p r o g r a m s . S u c h i n c o m p a t i b i l i t y w o u l d b e i n c o n v e n i e n t f o r u s e r s , s o t h e G N U C p r e - p r o c e s s o r i s configured to accept these constructs by default. Strictly speaking, to get ANSI Standard C, you must use the options`-trigraphs `undefand`pedantic, but in practice the consequences of having strict ANSI Standard C make it undesirable to do this. Conditional Assembly :Means that some sections of the program may be optional, either included or not in the final program, dependent upon specified conditions. A reasonable use of conditional assembly would be to combine t w o versions of a program, one that prints debugging information during t e s t e x e c u t i o n s f o r t h e developer, another version for production operation that displays only results of interest for the average user. A program fragment that assembles the instructions to print the Ax register only if Debug is true is given below. Note that true is any non-zero value.

Here is a conditional statements in C programming, the following statements tests the expression`BUFSIZE == 1020, where `BUFSIZE must be a macro.#if BUFSIZE == 1020 printf ("Large buffers!n");#endif /* BUFSIZE is large */

4. Write about different Phases of Compilation. ? Ans: Phases of Compiler A compiler takes as input a source program and produces as output an equivalent sequence of machine instructions. This process is so complex that it is not reasonable, either from a logical point of view or from an implementation point of view, to consider the compilation process as occurring in one single step. For this reason, it is customary to partition the compilation process into a series of sub processes called phases. A phase is a logically cohesive operation that takes as input one representation of the source program and produces as output another representation. The first phase, called the lexical analyser, or scanner, separates c h a r a c t e r s o f t h e s o u r c e language into groups that logically belong together; these groups are called tokens. The usual tokens are keywords, such as DO or IF identifiers, such as X or NUM, operator symbols such as < = or +, and punctuation symbols such as parentheses or commas. The output of the lexical analyzer is a stream of tokens, which is passes to the next phase, the syntax analyzer, or parser. The tokens in this stream can be represented by codes which we may regard as integers. Thus, DO might be represented by 1, + by 2,and identifier by 3. In the case of a token like identifier, a second quantity, telling which of those identifiers used by the program is represented by this instance of token identifier, is passed along with the integer code for identifier.The syntax analyzer groups tokens together in to syntactic structures. For example, the three tokens representing A + B might be grouped into a syntactic structure called an expression. Expressions might further be combined to form statements. Often the syntactic structure can be regarded as a tree wh ose leaves are the tokens. The interior nodes of the tree represent strings of tokens

that logically belong together. The intermediate code generator uses the structure produced by the syntax analyzer to create a stream of simple instructions. Many styles of intermediate code are possible. One common style uses instructions with one operator and a small number of operands. These instructions can be viewed as simple macros like the macro ADD2. The primary difference between intermediate code and assembly code is that the intermediate code need not specify the registers to be used for each operation. Code Optimization is an optional phase designed to improve the intermediate code so that the ultimate object program runs faster and / or takes less space. Its output is another intermediate code program that does the same job as the original, but perhaps in a way that saves time and / or space .The final phase, code generation, produces the object code by deciding on the memory locations for data, selecting code to access each datum, and selecting the registers in which each computation isto be done. Designing a code generator that produces truly efficient object programs is one of the most difficult parts of compiler design, both practically and theoretically .The TableManagement, or bookkeeping, portion of the compiler keeps track of the names used by the program and records essential information about each, such as its type (integer, real, etc). The data structure used to record this information is called a Symbol table. The Error Handler is invoked when a flaw in the source program is detected. It must warn the programmer by issuing a diagnostic, and adjust the information being passed from phase to phase so that each phase can proceed. It is desirable that compilation be completed on flawed programs, at least t h r o u g h t h e syntax-analysis phase, so that as many errors as possible can b e d e t e c t e d i n o n e compilation. Both the table management and error handling routines interact with all phases of the compiler.

Lexical Analysis The lexical analyser is the interface between the source program and the compiler. The lexical analyser reads the source program one character at a time, carving the source program into a sequence of atomic units called tokens. Each token represents a sequence of characters that can be treated as a singl e logical entity. Identifiers, keywords, constants, operators, and punctuation symbols such as commas commas and parentheses are typical tokens. There are two kinds of token: specific strings such as IF or a semicolon, and classes of strings such as identifiers, constants, or labels. Syntax Analysis The parser has two functions. It checks that the tokens appearing in its input, which is the output of the lexical analyser, occur in patterns that are permitted by the specification for the source language .It also imposes on the tokens a tree-like structure that is used by the subsequent phases of the compiler. T h e s e c o n d a s p e c t o f s y n t a x a n a l y s i s i s t o m a k e e x p l i c i t t h e h i e r a r c h i c a l s t r u c t u r e o f t h e incoming token stream by identifying which parts of the token stream should be grouped together Intermediate Code Generation On a logical level the output of the syntax analyser is some representation of a parse tree. The i n t e r m e d i a t e c o d e g e n e r a t i o n p h a s e t r a n s f o r m s t h i s p a r s e t r e e i n t o a n i n t e r m e d i a t e l a n g u a g e representation of the source program called Three-Address Code. Three-Address Code One popular type of intermediate language is what is called three -address code. A typical threeaddress code statement is A: = B op Where A, B and C are operands and op is a binary operator. Code Optimization Object programs that are frequently executed should be fast and small. Certain compilers have within them a phase that tries to apply transformations to the output of the intermediate code generator, in an attempt to produce an intermediatelanguage version of the source program from which a faster or s m a l l e r o b j e c t language program can ultimately be produced. This phase is popularity

called the optimization phase. A good optimizing compiler can improve the target program by perhaps a factor of two in o v e r a l l s p e e d , i n comparison with a compiler that generates code carefully but w i t h o u t u s i n g specialized techniques generally referred to as code optimization. There are two types of optimizations used: Local Optimization Loop Optimization Code Generation T h e c o d e g e n e r a t i o n p h a s e c o n v e r t s t h e i n t e r m e d i a t e c o d e i n t o a s e q u e n c e o f m a c h i n e instructions. A simple-minded code generator might map the statement A: = B+C into the machine code sequence LOAD BADD CSTORE However, such a straightforward macro like expansion of intermediate code into machine code usually produces a target program that contains many redundant loads and stores and that utilizes the resources of the target machine inefficiently. To avoid these redundant loads and stores, a code generator might keep track of the run time contents of registers. Knowing what quantities reside in registers, the code generator can generate loads and stores only when necessary. Many computers have only a few high speed registers in which computations can be performed p a r t i c u l a r l y q u i c k l y . A good code generator would therefore attempt to utilize these registers a s efficiently as possible. This aspect of code generation, called register allocation, is particularly difficult to do optimally. 5. What is MACRO? Discuss its Expansion in detail with the suitable example. Macro definition and Expansion Definition: macro A macro name is an abbreviation, which stands for some related lines of code. Macros are useful for the following purposes: To simplify and reduce the amount of repetitive coding To reduce errors caused by repetitive coding To make an assembly program more readable. A macro consists

of name, set of formal parameters and body of code. The use of macro name with set of actual parameters is replaced by some code generated by its body. This is called macro expansion. M a c r o s a l l o w a p r o g r a m m e r t o d e f i n e p s e u d o o p e r a t i o n s , typically operations that are generally desirable, are not implemented as part of the processor instruction, and can be implemented as s equence of instructions. Each use of a macro generates new program instructions, the macro has the effect of automatic writing of program

Macros can be defined used in many programming languages, like C, C++ etc. Example macro in C p r o g r a m m i n g . M a c r o s a r e c o m m o n l y u s e d i n C t o d e f i n e s m a l l s n i p p e t s o f c o d e . I f t h e m a c r o h a s parameters, they are substituted into the macro body during expansion; thus, a C macro can mimic a Function. The usual reason for doing this is to avoid the overhead of a fun ction call in simple cases, w h e r e t h e c o d e i s l i g h t w e i g h t e n o u g h t h a t f u n c t i o n c a l l o v e r h e a d h a s a s i g n i f i c a n t i m p a c t o n performance. For instance, define max (a, b) a>b? A: b For instance ,#define max (a, b) a>b? A: b Defines the macro max, taking two arguments a and b. This macro may be called like any C function,using identical syntax. Therefore, after preprocessingz = max(x, y);Becomes z = x>y? X:y; While this use of macros is very important for C, for instance to define type-safe generic data-types or debugging tools, it is also slow, rather inefficient, and may lead to a number of pitfalls macros are capable of mimicking functions, creating new syntax within some limitations, as well as expanding into arbitrary text (although the C compiler will require that text to be valid C source code,or else comments), but they have some limitations as a programming construct. Macros which mimic f u n c t i o n s , f o r i n s t a n c e , c a n b e called like real functions, but a macro cannot be passed to a n o t h e r function using a function pointer, since the macro itself has no address. In programming languages, such as C or assembly language, a name that defines a set of commands that are substituted for the macro name wherever the name appears in a program (a process called macro expansion) when the program is compiled or assembled. Macros are similar to functions in that they can take arguments and in that they are calls to lengthier sets of instructions. Unlike functions ,m a c r o s a r e r e p l a c e d b y t h e a c t u a l c o m m a n d s t h e y r e p r e s e n t w h e n t h e p r o g r a m i s p r e p a r e d f o r execution. function instructions are copied into a program only once .Macro Expansion. A macro call leads to macro expan sion. During macro expansion, the macro statement is replaced by sequence of assembly statements.

6 .What is linking? Explain dynamic linking in detail.? Ans: Linking: Linking, is the process where object modules are connected together (linked) to for more large program. That is, if you have three source code files called my programs, search function .c,and print reports, you would compile them separately into my program. obj, search function. obj, and print report. obj. The linker with take the object modules, along with any library code that needs to be included, and create one .exe file, resolving external variables and function calls Dynamic linking Dynamic linking defers much of the linking process until a program starts running. It provides a variety of benefits that are hard to get otherwise: Dynamically linked shared libraries are easier to create than static linked shared libraries. Dynamically linked shared libraries are easier to update than static linked shared libraries. The semantics of dynamically linked shared libraries can be much closer to those of unshared libraries. Dynamic linking permits a program to load and unload routines at runtime, a facility that can otherwise be very difficult to provide There are a few disadvantages, of course. The runtime performance costs o f d y n a m i c l i n k i n g a r e substantial compared to those of static linking, since a large part of the linking process has to be redone every time a program runs. Every dynamically linked symbol used in a program has to be looked up in symbol table and resolved. (Windows DLLs mitigate this cost somewhat, as we describe below.)Dynam ic libraries are also larger than static libraries, since the dynamic ones have to include symbol tables. Beyond issues of call compatibility, a chronic source of problems is changes in library semantics. Since dynamic shared

libraries are so easy to update compared to unshared or static shared libraries, it's easy t o c h a n g e libraries that are in use by existing programs, which means that the behaviour of those programs changes even though "nothing has changed". T h i s i s a f r e q u e n t s o u r c e o f p r o b l e m s o n Microsoft Windows, where programs use a lot of shared libraries, libraries go through a lot of versions, and library version control is not very sophisticated. Most programs ship with copies of all of the libraries they use, and installers often will inadvertently install an older version of a shared library on t o p o f a n e w e r o n e , breaking programs that are expecting features found in the newer one. W e l l - behaved applications pop up a warning before installing an older library over a newer one, but even so, programs that depend on semantics of older libraries have been known to break when newer versions replace the older ones.

S-ar putea să vă placă și