Vous êtes sur la page 1sur 24

LAKSHMI NARAIN COLLEGE OF TECHNOLOGY Sanwer Road, INDORE Department of Computer Science and Information Technology

SYSTEM PROGRAMMING ASSIGNMENT - 1


Submission Date: 2nd April, 2012

1. What is language processor? Explain with figure.


Ans: Language processors are language translation software like assembler, interpreter and compiler. Allows the development of applications in the language most appropriate to the task, removing the need for developing at the machine level. Programmers can ignore the machine-dependent details of programming. Receives a textual representation of an algorithm in a source language, and produces as output a representation of the same algorithm in the object or target language.

Source languageObject

or

Language processor

object of target language

Fig. Language processor Language processor represented by T-Diagram:The language processor itself is written in what is`s1called the implementation language. These relations are often represented in T-diagrams.

LPs name
Source language Target language Implementation Language

T-Diagram Example:-

Delphi Pascal Machine code

C++

This diagram represents a language processor which translates Pascal to machine code; the processor is called Delphi, and is written in C++.

Conceptual structure:
Source text

Analysis

Semanti c representati

Synthes Executable code is

Front-end

Back-end

Front-end: the part of the language processor that performs the analysis of the
source language

Back-end: the part of the language processor that does the target language
synthesis. In a fully-modular design the front-end is totally unaware of the back-end in practice, this is inefficient, and even the best-structured compilers compromise.

Types of language processing:-

Assemblers: Language processors that map low-level language instructions into


machine code, e.g. the ARM assembler>

Compilers: Language processors that map high-level language instructions into


machine code, e.g. Delphi, GCC, Visual C++ etc.

Pre-processors: Language processors that map a superset of a high-level


language into the original high-level language, or perform simple text substitutions before translation takes place.

Interpreters: Language processors that include an execution component, i.e. they


perform the operations specified in the source text, rather than re-expressing them in another language; e.g. Mat lab.

Dissemblers: language processors that attempt to take object code at a low level
and regenerate source code at a higher level.

Language Processor Examples:


GCC, Delphi, Visual Studio TeX/LaTeX Postscript HTML & Web browsers XML

2. What is Explain with

assembly language example.

programming?

Assembly language is the most basic programming language available for any processor. With assembly language, a programmer works only with operations implemented directly on the physical CPU. Assembly language lacks high-level conveniences such as variables and functions, and it is not portable between various families of processors. Nevertheless, assembly language is the most powerful computer programming language available, and it gives programmers the insight required to write effective code in high-level languages. Learning assembly language is well worth the time and effort of every serious programmer.

The Basics:
Before we can explore the process of writing computer programs, we have to go back to the basics and learn exactly what a computer is and how it works. Every computer, no matter how simple or complex has at its heart exactly two things: a CPU and some memory. Together, these two things are what make it possible for your computer to run programs. On the most basic level, a computer program is nothing more than a collection of numbers stored in memory. Different numbers tell the CPU to do different things. The CPU reads the numbers one at a time, decodes them, and does what the numbers say. For example, if the CPU reads the number 64 as part of a program, it will add 1 to the number stored in a special location called AX. If the CPU reads the number 146, it will swap the number stored in AX with the number stored in another location called BX. By combining many simple operations such these into a program, a programmer can make the computer perform many incredible things. As an example, here are the numbers of a simple computer program: 184, 0, 184, 142, 216, 198, 6, 158, 15, 36, 205, and 32. If you were to enter these numbers into your computer's memory and run them under MS-DOS, you would see a dollar sign placed in the lower right hand corner of your screen, since that is what these numbers tell the computer to do.

Assembly Language:

Although the numbers of the above program make perfect sense to a computer, they are about as clear as mud to a human. Who would have guessed that they put a dollar sign on the screen? Clearly, entering numbers by hand is a lousy way to write a program. It doesn't have to be this way, though. Along time ago, someone came up with the idea that computer programs could be written using words instead of numbers. program called an assembler would then take the programmer's words and convert them to numbers that the computer could understand. This new method, called writing a program in assembly language, saved programmers thousands of hours, since they no longer had to look up hard-to-remember numbers in the backs of programming books, but could use simple words instead. The program above, written in assembly language, looks like this: MOV AX, 47104
MOV DS, AX MOV [3998], 36 INT 32

When an assembler reads this sample program, it converts each line of code in toone CPU-level instruction. This program uses two types of instructions, MOV and INT. On Intel processors, the MOV instruction moves data around, while the INT instruction transfers processor control to the device drivers or operating system. The program still isn't quite clear, but it is much easier to understand than it was before. The first instruction, MOV AX, 47104, tells the computer to copy the number 47104 into the location AX. The next instruction, MOV DS, AX, tells the computer to copy the number in AX into the location DS. The next instruction, MOV [3998], 36 tells the computer to put the number 36 into memory location 3998. Finally, INT 32 exits the program by returning to the operating system. Before we go on, I would like to explain just how this program works. Inside the CPU are a number of locations, called registers, which can store a number. Some registers, such as AX, are general purpose, and don't do anything special. Other registers, such as DS, control the way the CPU works. DS just happens to be a

segment register, and is used to pick which area of memory the CPU can write to. In our program, we put the number 47104 into DS, which tells the CPU to access the memory on the video card. The next thing our program does is to put the number 36 into location 3998 of the video card's memory. Since 36 is the code for the dollar sign, and 3998 is the memory location of the bottom right hand corner of the screen, a dollar sign shows up on the screen a few microseconds later. Finally, our program tells the CPU to perform what is called an interrupt. An interrupt is used to stop one program and execute another in its place. In our case, we want interrupt 32, which ends our program and goes back to MS-DOS, or whatever other program was used to start our program.

3. What is Macro? Define stepwise.


A macro instruction (abbreviated to macro) is simply a notational convenience for the programmer. A macro represents a commonly used group of statements in the source programming language. Replace each macro instruction with the corresponding group of source language statements. A macro processor is not directly related to the architecture of the computer on which it is to run Macro processors can also be used with high-level programming languages, OS command languages, etc. He twill ingredients of macro processing are the s: rmbol table aril the input text. This processor has a very small initial Sybil table (mostly consisting of processor option swig etches) so the input text contains the information to build the Sybil table. The facilities are of four skirls: (1) Substitution of text, (2) Manipulation of the Sybil table. (3) control of the substitution, (4) Others (e.g. comments, .processor options).

Basic Macro Processor Functions:

Expanded program

A program with Macro definitions and Macro invocations.

Macro Processor

A program without Macro definitions

Assembler

Object program

One-Pass Macro Processor: Prerequisite:

Every macro must be defined before it is called.

Sub-procedures:

Macro definition: DEFINE.

Macro invocation: EXPAND

DEFINE MACRO PROCESSLINE EXPAND NAMTAB DEFTAB

CALL ARGTAB

Keyword Macro Parameters:


Positional parameters Parameters and arguments were associated with each other according to their positions in the macro prototype and the macro invocation statement

Consecutive commas is necessary for a null argument GENER, DIRECT,,,,,,3

4. What is Macro concept? Give example.


Macro processors have been developed for some high-level programming languages. These special-purpose macro processors are similar in general function and approach; however, the details differ from language to language. Macros automate application tasks. For example, Corel WordPerfect macros automate tasks such as setting margins, selecting a font, or creating a merge file. Macros tasks consist of instructions called statements. There are several kinds of statements: assignments, conditions, loops, comments, and commands. For example, Margin Left (1.0") Margin Right (1.0") Are Corel WordPerfect commands that set one inch left and right margins (the task). Each command represents one instruction or statement. The simplest macro consists of only one statement. Complex macros have hundreds of statements. The sequence of statements determines how a macro performs its tasks.

Syntax:

Syntax refers to rules that govern the form of macro statements and expressions. For example, the following statement types John Doe: Type (Text: "John Doe") The next example lacks a closing parenthesis. Type (Text: "John Doe" The syntax is incorrect and produces an error message.

Expressions:
Expressions represent values. They are used in several statements (assignments, conditions, loops, and commands) to form a statement that the computer can understand. To create expressions, you use variables and constants. Variables represent data that can change while a macro is playing (for example, vCount), and constants represent data items that cannot change during macro play. You combine variables and constants with operators (+, -, *, %, etc.) to create an expression. For example, a valid expression would be count .

Assignment Statements:
Assignment statements assign the value of an expression to a variable. For example, x := "John Doe" Result: x equals John Doe y := 5 Result: y equals 5 z := 3 + 4 Result: z equals the result of 3 + 4 The assignment operator (:= or =) assigns the value of a right operand expression to a left operand variable.

Conditional Statements:
Conditional statements play a statement or group of statements (statement block) when a specified condition is met. Conditional statements are useful for displaying a list of options. A statement block is played depending on which option the user chooses. Conditional statements include Case, If, and Switch.

Loop Statements:
An expression is true or while an expression is true. The macro then exits the loop and continues to the next statement. Loop statements play a statement or statement block a specified number of times until statements include For, For Next, For Each, Repeat, and While.

Comment Statements:
Comment statements contain notes and other information that do not affect macro play. Use comment statements to explain the purpose of your macro, describe its components, or to prevent a statement from playing. Comment statements help if you have to modify a macro months after it is written or if someone else has to understand your macro. A comment either begins with // and ends with a hard return [HRt], or it begins with /* and ends with */. See Perfect Script // and /* */ commands.

Command Statements:
Command statements consist of a name and can include one or more parameters. Commands represent instructions to the parent application. There are three types of macro commands: product commands, OLE Object commands, and programming commands. Product commands are specific to a product; for example, Corel WordPerfect or Corel Quattro Pro. OLE object commands perform tasks on an OLE object. Programming commands work across applications; they are Perfect Script commands. Command names often describe an action, such as Font, Margin Left, Advance, and Foot note Options in Corel WordPerfect; or Align Objects Left, Bitmap Blur, Select All Objects, and Tool bar Copy in Corel Presentations; or Range in Microsoft Excel. Command names are not case sensitive and usually do

not contain spaces. Exceptions include programming commands that call a subroutine, such as Case Call or On Cancel Call. A macro can use more than one application product and OLE object. Commands to the non-default application or OLE Object require a prefix, which is specified in an Application or Object statement. In this example,
A1.AboutDlg ()

A1 (followed by a period) is the prefix. It tells the compiler to use the application or Object assigned A1 in a Perfect Script Application or Object statement.

Subroutines:
Subroutines consist of a statement or group of statements (statement block) that are played when the macro calls a subroutine. For example, Call (Sub Example) ...(other statements)... Label (Sub Example) ...statement block... Return The calling statement Call (Sub Example) calls (directs macro play to) the subroutine Label (Sub Example). Return directs macro play to the statement that follows Call (Sub Example). Subroutines include functions, procedures, and labels. Subroutines are useful because the statements in a subroutine are accessible to any part of a macro, and can be called any number of times during play.

Compilers:
A macro compiler is used to compile or "translate" macros so that Corel WordPerfect Suite applications can play them. Macros in Corel WordPerfect are compiled when you record, play, or save them, or when you click Save & Compile or Options Close Macro on the Macro Edit feature bar.

5. What is relocated concept?

Relocation is an important concept. To understand this concept we shall begin with linear map (one-dimensional view) of main memory. If we know an address we can fetch its contents. So, a process residing in the main memory, we set the program counter to an absolute address of its first instruction and can initiate its run. Also, if we know the locations of data then we can fetch those too. All of this stipulates that we know the

Figure : The relocation concept

absolute addresses for a program, its data and process context etc. This means that we can load a process with only absolute addresses for instructions and data, only when those specific addresses are free in main memory. This would mean we lose flexibility with regard to loading a process. For instance, we cannot load a process, if some other process is currently occupying that area which is needed by this process. This may happen even though we may have enough space in the memory. To avoid such a catastrophe, processes are generated to be reloadable. In Figure 4.1 we see a process resident in main memory. Initially, all the addresses in the process are relative to the start address. With this flexibility we can allocate any area in the memory to load this process. Its instruction, data process context (process control block) and any other data structure required by the process can be accessed easily if the addresses are relative. This is most helpful when processes move in and out of main memory. Suppose a process created a hole on moving out. In case we use non-relocatable addresses, we have the following very severe problem. When the process moves back in, that particular hole (or area) may not be available any longer. In case we can relocate, moving a process back in creates no problem. This is so because the process can be relocated in some other free area. We shall next examine the linking and loading of programs to understand the process of relocation better.

EXAMPLE:
There are 5 relocation table entries. The relocation table contains the offsets 0001, 0007,0000F, 001F, 0023.There is one external-table entry. It contains the nameoffset pair {submit, 0000}. There are no import-table entries.
Name Meaning length 003316 rlength 5 0001 0007 000F 001F 0023

rtable

xlength 1 xtable summit 0000 ilength 0 itable <null> code 51 bytes of code

If the code is loaded into memory at location 100016, the operand references for count, loop, done, array, and sum will still point to locations near zero. Consider the case of count, which has been relocated to 102816 and so the reference at location 000116, now at location 100116, should be relocated to contain the value 102816 instead of 002816.

The following algorithm can be applied: // base = relocation target for (int i = 0; i < r length; i++) { word = mem.get(r table[i]+base);

word += base; mem.put(r table[i]+base, word); }

6. What is linking concept? Explain with example.


The Linking System links separately compiled or assembled subprograms into a whole program before loading, at loading, or during execution. It can tailor a program for a specific application. It is for use with Universal Compiling System (UCS) compilers. It executes on OS 2200 systems.

Purpose:
The Linking System Programming Reference Manual introduces the Linking System for users unfamiliar with the Linking System and gives basic static and dynamic linking run streams. This reference manual supplies information on more advanced topics and assumes you are familiar with that guide. For example, it discusses individual static linking commands. It is written in a reference format to facilitate location of information.

Scope:
This reference manual covers the following major topics: Banking in extended mode Resolving references, including how files are searched for definitions Cross-referencing object modules Static linking Using fixed-gate shared subsystems Creating a process from a program written in a high-level language like C++ is done in several stages, and numerous actors are involved in this creation. Consider a program P that is written in C++ in several modules M1, Mn (several *.cpp files), and that uses some "build-in" library functions for I/O, string manipulation, etc.
1. 2.

Compiler - Separately compiles each one of the program's modules. Denote the object module resulting in compiling M1 by O1. Linker - Combines several object modules into one, coherent module. This module may be either executable object, or library of functions (see below).

3.

Loader - Brings an executable object into the memory and relocates it. Relocation is an object modification so that it can be loaded at address different than 0.

Here we describe several linking and loading paradigms, and discuss advantages and disadvantages of each one of them. This classification will provide us an ability to analyse our applications and to choose the appropriate linking/loading strategies for them.

Why Linking Is Needed:


Linking is the process of converting a program from the form produced by a compiler or assembler into a form that the computer hardware can execute. The two biggest tasks of linking are to Replace symbolic external references (calls) with the entry point addresses (Definitions) those references represent. This usually involves combining two or More separately compiled or assembled object modules (routines) into a new object Module or into a whole executing program. Adjust addresses affected by address relocation.

Linking Functions:
First, let understand what linking means and why it is necessary. Software is made up of instructions grouped in logical sections called subroutines or functions. Most functions perform a specific task based on a set of input and output variables. These individual functions are able to use or call each other using a process called linking. Calling one function from another function is based on knowing the address or location of each function. Once this address is known, the calling function executes or passes control to the called function. When the called function completes, control is returned to the calling function. Linking is simply the process of placing the address of a called function into the calling function's code. This is a fundamental software concept. Now consider a function F contains a call to a function H, and suppose that these two functions were defined in the same module (informally, the same *.cpp file). In this case, the linking between the call to H in F, and the address of H can be done by compiler, since both functions are given to it at the same compilation session. Well, this is nice and simple, but it is not always the case when the modularity is a part of our programming life. Suppose that the functions F and H are defined in two different modules M(F) and M(H). In this

case, the compiler can not resolve the problem of linking between the call to H in F, and the address of H. Note that the this situation is always the case if the function H was not written by us, but, for example, is a part of a library of functions that was created for general use. For example, in our C code, we are using the function socket (...) which is definitely was not written by us. In this case, some linking should be done after the separate compiling of the modules by a linker. In general, there are three types of linkers:

Static Linker (or linkage editor) - linking prior to load time. Implicit Dynamic Linker (or linking loader) - linking at load time. Explicit Dynamic Linker - linking is performed at execution time.

Static Linking:

The simplest form of linking is called static linking. Static linking is used to combine multiple functions into a single object module. These functions may be a part of your compiled program modules, and/or a part of various archive libraries that it uses. The result of static linking may be either an executable object, or an archive library. The notion of executable object is pretty intuitive -- an executable object contains specification of a process (or, informally, you can "run" it). The notion of archive library is novel and well-known for you at the same time -- until now, you never created one, but you used them many times. For example, if you are giving -socket as a parameter to your linking command (see your Make file for the Connector component in the third assignment), then you explicitly specify to your linking to look for the unresolved references in the lib socket.a archive library. As we said, the linker is given your compiled code, containing many unresolved references to library routines. It also gets archive libraries (for example /usr/lib/limbs) containing each library routine as a separate module. The linker keeps working until there are no more unresolved references and writes out a single file that combines your code and a jumbled mixture of modules containing parts of several libraries. The library routines make system calls directly, so a statically linked application is built to work with the kernel's system call interface. This static linking process takes place once - when the executable module is created. All internal references to functions within a module are resolved at this time. In some cases, an entire application is built out of a single statically linked executable module. In this case, the resulting module is an autonomous unit that

can execute without referencing any other Now, let present the advantages and disadvantages of the static linking:

modules.

Advantages o One linking for many executions. o Calls into the library routines have a little less overhead since they are linked together directly. o Start-up time at program loading is reduced as there is no need to locate and load the dynamic libraries. o Possibility to create archive libraries is unique for static linking. Disadvantages o Physical memory wasted by duplicating the same library code in every static linked process can be significant. For example, if all the window system tools were statically linked, several tens of megabytes of physical memory would be wasted for a typical user, and the user would be slowed down by a lot of paging. o Subsequent versions of the operating system contain better-tuned and debugged library routines, or routines that enable new functionality. Static linking locks in the old slow or buggy routines and prevents access to the new functionality. o A static-linked program contains a subset of the jumbled library routines. The library cannot be tuned as a whole to put routines that call each other onto the same memory page. The whole application could be tuned this way, but very few developers take the trouble. o During program development, recompiling for nearly every execution. As far as we are talking about developing an assignment for our course, this may be not so important parameter. However, if compiling of a product that has been developed is taking the whole night, then it the picture is completely different. o Since the size of the executable object may be relatively large, the storage waste is significant, especially in case of infrequently used program.

Dynamic Linking:

A second way to link functions together is through a process called dynamic linking. With dynamic linking, executable modules created by static linking can contain references to functions in other modules. All modules that reference each other must be linked together dynamically when they are executed. Thus the term dynamic linking. The central property of the dynamic linking is that even if many processes are using a particular, dynamically linked function, only a SINGLE copy of this function appears in the memory, and it is shared by all these processes. The dynamic linking process consists of two steps. First, the module that contains the external function must be located. Second, the address of the function within this module must be found. Once this address is found, the calling module can use this external function just as if it was linked statically. Dynamically imported functions are functions that are called from within one module but actually reside in another module. In order to import a function, the developer must provide the name of the module and the function name. These two items should uniquely identify the imported function. Dynamic linking occurs when this information is used to find the imported function's address. If the specified module is found, its headers are examined for the desired function name. If the specified function is also found, its address is dynamically placed in the calling module's code There are two dynamic linking methods:

Implicit Dynamic Linking Explicit Dynamic Linking

Implicit Dynamic Linking:

With implicit dynamic linking, the process of identifying imported functions is handled automatically by operating system at runtime. When using implicit dynamic linking, the importing module contains header fields for each implicitly imported function. These header fields are generated by a static linker, and each one of them contain the exporting module and imported function name. The static linker also adds to the executable object a start-up code to load the required libraries to the memory at runtime (if they are still not there!), and each library call goes through a jump table. The first time a routine is actually called, the jump table is patched to point at the library routine. For subsequent calls, the only overhead is the indirect reference.

Note that the operating system must terminate an application that references an implicitly linked module or function that can not be found. The central property of the implicit dynamic linking is that ALL the functions imported by a process should be loaded into memory before the beginning of its execution. Under UNIX or LINUX, use ldd to generate a list of shared libraries an executable object depends on. Likewise, under UNIX or LINUX, shared object libraries have a .so suffix and a version number (*.DLL files in Windows). For example, consider the UNIX standard program grep: % ldd /bin/grep libintl.so.1 => /usr/lib/libintl.so.1 libc.so.1 => /usr/lib/libc.so.1 libw.so.1 => /usr/lib/libw.so.1 libdl.so.1 => /usr/lib/libdl.so.1 These libraries include the main system interface library libc.so, the dynamic linking library libdl.so, wide character support (libw.so), and internationalization support (libintl.so). Note that on your version of OS the result of ldd /bin/grep may be different. Actually, this raises another good reason to use dynamic linking. Statically linked programs may not be able to take advantage of some internationalization, networking, and other features that may vary across configurations and environments.
Explicit Dynamic Linking:

With explicit dynamic linking, the process of linking to imported functions is handled by the application, not by the operating system. The operating system provides services that an application can use to explicitly load a module into memory. Once loaded, another service of the operating system can be used to extract an imported function's address using its name. If both of these stages are successful, the application can call the imported function. The explicit dynamic linking functions return an error if either an exporting module or an imported function is not found. Thus the application can handle either of these conditions without terminating. Most large-scale applications employ both implicit and explicit dynamic linking. The choice of technique depends on the developer's requirements. From the application user's perspective, once modules are properly dynamically linked, there is no difference in the two

techniques. While explicit dynamic linking is more complicated to implement from the developer's perspective, it does offer several advantages over implicit dynamic linking (see below).

Example of Dynamic Linking:

Linking loader v.s. linkage editor:

Linking loader:

Linkage editor:

Difference between a linkage editor and loader:

Linking loader Performs all linking and relocation operations, including automatic library search, and loads the linked program into memory for execution. Linkage editor Produces a linked version of the program, which is normally written to a file or library for later execution. A simple relocating loader (one pass) can be used to Load the program into memory for execution. The linkage editor performs relocation of all control sections relative to the start of the linked program. The only object code modification necessary is the addition of an actual load address to relative values within the program.

Dr. B.S.Patel (H.O.D) (Principal)

Dr.

D.

Mitra

NOTE: Submission can be hand written or soft copy. Do mention the references.

Vous aimerez peut-être aussi