Subject Name:- System Programming and Operating System 1 Savitribai Phule Pune University Third Year of Computer Engineering (2019 Course) 310243 : Systems Programming and Operating System Teaching Scheme: TH: 03 Hours/Week Credit : 03 Examination Scheme: In-Sem (Paper): 30 Marks End-Sem (Paper): 70 Marks 2 Course Objective To understand basics of System Programming. To learn and understand data structures used in design of system software. To learn and understand basics of compilers and tools. To understand functions of operating system. To learn and understand process, resource and memory management. Course Outcome On completion of the course, student will be able to– Analyze and synthesize system software Use tools like LEX & YACC. Implement operating system functions. 3 Unit 1 Introduction 4 Input / Output Process Model Input: keyboard, mouse, scanner, punch cards Processing: CPU executes the computer program Output: monitor, printer, fax machine Storage: hard drive, optical media, diskettes, magnetic tape 10 Typical Computer System 11 Major Types of Software Application software System software Hardware System Software Operating Systems Schedules computer events Allocates computer resources Monitor events Language translators Users Application Software Programming languages Assembly language FORTRAN, BASIC, PL/1 PASCAL, C Interpreters Compilers Utility programs Routine operations (e.g. sort, list, print) Manage data (e.g. create files, merge files 14 Need of System Programming System programming is the activity of programming computer system software. Application programming aims to produce software which provides services to the user directly (e.g. word processor), whereas systems programming aims to produce software and software platforms which provide services to other software. Its goal is to achieve efficient use of available resources. 15 Components of System Programming 1. 2. 3. 4. 1. 2. 3. 4. 5. 6. Program Development Environment: Text editors Compilers Assemblers Micro processors Run Time Environment Linker Loaders Debuggers Interpreter Operating System Device Driver 17 Text editors A text editor is a program that enables you to create and edit text files. The distinction between editors and word processors is not clear-cut, but in general, word processors provide many more formatting features. They compose, organise, study and manipulate computer-based information. They allow the user to enter the program in text Tasks of editor Facilitate programmer to enter and save Allow to modify the code, if needed code Examples: Note / Word pad, vi, gedit and many more 18 Compiler Software that translates a high-level language program into machine language Input to the compiler is a source file (created by word processor or editor) containing the text of a high-level language program. If it is syntactically correct, compiler will save in an object file which is a machine language instructions for the same job. 21 Interpreter Program execution requires either compiler or interpreter. Interpreter accepts the source program and performs the actions associated with the instructions. Interpreter reads tge source code one instructon/ line at a time and converts this lines to a machine code. 23 Debugger It is a tool to facilitate programmer to remove logical programming errors Features Execute program in various modes Complete, breakpoint, single stepping Display contents of the microprocessor and memory All the CPU registers, memory locations in use Present status of the execution Present status of flags (psw), location of next instruction (pc) 24 Role of Loader 29 Role of Both Loader and Linker 35 Linker Linker combines several object files, resolving cross references between the files, into one executable file (machine language program) 36 Role of Loader with Assembler 39 Operating System Software Component •Operating System –API: application program interface –File management –I/O –Kernel •Memory management •Resource scheduling •Program communication •Security –Network Module 41 Macro processors Concept Macro allows a sequence of source language code to be defined once and then referred to by name each time it is to be reffered. Each time this name occurs in the program the sequence of code is subsituted at that point. Macro Consist of : lName of the macro lSet of parameters lBody of the macro (Code) 43 Macro Processor Recognize macro definitions Save the macro definition Recognize macro calls Expand macro calls Source Code (with macro) Macro Processor Expande d Code Compiler or Assembler obj 44 IDE(Integrated Development Environment ) Most high-level languages now include an Integrated Development Environment (IDE) consisting of a simple word processor, compiler, linker and loader tools for finding errors This software package provides menus from which user can select the next step It leaves all versions of the program in memory. For safety, we need to explicitly save the source file to disk 45 Assembler: Definition An assembler is a translator which takes its input in the form of an assembly language program & produces machine language code as its output. Translating source code written in assembly language to object code.c 49 Machine code Machine code: Set of commands directly executable Commands in numeric code Lowest semantic level via CPU Machine code language Structure: Operation code OpCode Defining executable operation Operand OpAddress address Specification of operands Constants/register addresses/storage addresses 51 Elements of the Assembly Language Programming An Assembly language is a machine dependent, low level Programming language specific to a certain computer system. o Three features when compared with machine language are 1.Mnemonic Operation Codes 2.Symbolic operands 3.Data declarations Elements of the Assembly Language Programming Mnemonic operation codes: eliminates the need to memorize numeric operation codes. Symbolic operands: Symbolic names can be associated with data or instructions. Symbolic names can be used as operands in assembly statements (need not know details of memory bindings). Data declarations: Data can be declared in a variety of notations, including the decimal notation (avoids conversion of constants into their internal representation). Mnemonic Operation Codes Each statement has two operands, first operand is always a register and second operand refers to a memory word using a symbolic name and optional displacement. Assembly language-structure <Label> <Mnemomic> <Operand> Comments Label symbolic labeling of an assembler address (command address at Machine level) Mnemonic Symbolic description of an operation Operands Contains of variables or addresses if necessary Comments 55 Statement format An Assembly language statement has following format: [Label] <opcode> <operand spec>[,<operand spec>..] If a label is specified in a statement, it is associated as a symbolic name with the memory word generated for the statement. <operand spec> has the following syntax: <symbolic name> [+<displacement>] [(<index register>)] Eg. AREA, AREA+5, AREA(4), AREA+5(4) Meaning of Some instructions MOVE instructions move a value between a memory word and a register MOVER – First operand is target and second operand is source MOVEM – first operand is source, second is target All arithmetic is performed in a register (replaces the contents of a register) and sets condition code. condition code can be tested by a Branch on Condition (BC) instruction and the format is: BC <condition code spec> , <memory address> Machine Instruction Format sign is not a part of the instruction Opcode: 2 digits, Register Operand: 1 digit, Memory Operand: 3 digits In a Machine Language Program, all addresses and constants are shown in decimal as shown in the next slide Example: Machine Language Program Location Counter (LC) MOT (OPCODE) Register Address Assembly Language Statements An assembly program contains three kinds of statements: 1)Imperative Statements 2)Declaration Statements 3)Assembler Directives Imperative Statements: They indicate an action to be performed during the execution of an assembled program. Each imperative statement is translated into one machine instruction. Assembly Language Statements Declaration Statements: syntax is as follows: [Label] DS <constant> [Label] DC '<value>' The DS (declare storage) statement reserves memory and associates names with them. Ex: A DS 1 ; reserves a memory area of 1 word, associating the name A to it G DS 200 ; reserves a block of 200 words and the name G is associated with the first word of the block (G+6 etc. to access the other words) The DC (declare constant) statement constructs memory words containing constants. Ex: ONE DC '1’ ; associates name one with a memory word containing value 1 Assembly Language Statements Assembler Directive Assembler directives instruct the assembler to perform certain actions during the assembly of a program. Some assembler directives are described in the following: 1) START <constant> This directive indicates that the first word of the target program generated by the assembler should be placed in the memory word having address <constant>. 2) END [<operand spec>] This directive indicates the end of the of the source program. The optional <operand spec> indicates the address of the instruction where the execution of the program should begin. A simple Assembly Scheme Design Specification of an assembler There are four steps involved to design the specification of an assembler: Identify information necessary to perform a task. Design a suitable data structure to record info. Determine processing necessary to obtain and maintain the info. Determine task processing necessary to perform the A simple Assembly Scheme There are two phases in specifying an assembler: 1.Analysis Phase 2.Synthesis Phase(the fundamental information requirements will arise in this phase) Tasks Performed : Analysis Phase Isolate the labels, mnemonic, opcode and operand fields of a statement. If a label is present, enter (symbol, <LC>) into the symbol table. Check validity of the mnemonic opcode using mnemonics table. Update value of LC. Analysis Phase Primary function of the Analysis phase is to build the symbol table. It must determine the addresses with which the symbolic names used in a program are associated It is possible to determine some addresses directly like the address of first instruction in the program (ie.,start) Other addresses must be inferred To determine the addresses of the symbolic names we need to fix the addresses of all program elements preceding it through Memory Allocation. To implement memory allocation a data structure called location counter is introduced. Analysis Phase – Implementing memory allocation LC(location counter) : is always made to contain the address of the next memory word in the target program. It is initialized to the constant specified at the START statement. When a LABEL is encountered, it enters the LABEL and the contents of LC in a new entry of the symbol table. LABEL – e.g. N, AGAIN, SUM etc It then finds the number of memory words required by the assembly statement and updates the LC contents To update the contents of the LC, analysis phase needs to know lengths of the different instructions This information is available in the Mnemonics table and is extended with a field called length We refer the processing involved in maintaining the LC as LC Processing Tasks Performed : Synthesis Phase Obtain machine opcode corresponding to the mnemonic from the mnemonic table. obtain address of the memory operand from symbol table. Synthesize a machine instruction or machine form of a constant, depending on the instruction. Data structures in synthesis phase --built by the analysis phase The two primary fields are name and address of the symbol used to specify a value. Symbol Table Mnemonics Table --already present - The two primary fields are mnemonic and opcode, along with length. Synthesis phase uses these tables to obtain The machine address with which a name is associated. The machine op code corresponding to a mnemonic. The tables have to be searched with the Symbol name and the mnemonic as keys Data structures of an assembler During analysis and Synthesis phases Mnemon ic Opcod lengt e h ADD 01 1 SUB 02 1 Mnemonic Table Source Program Analysis Phase -------------------------------Synthesis -> Phase Target Program Symbol Table Symbol Address N 104 AGAIN 113 Data Access -- > Control Access Mnemonics table is a fixed table which is accessed by the analysis and synthesis phases Symbol table is constructed during analysis and used during synthesis Advantages of Assembly Language •The primary advantages of assembly language programming over machine language programming are due to the use of symbolic operand specifications. (in comparison to machine language program) •Assembly language programming holds an edge over HLL programming in situations where it is desirable to use architectural features of a computer. (in comparison to high level language program) Assembler’s functions Convert mnemonic operation codes to their machine language equivalents Convert symbolic operands to their equivalent machine addresses Build the machine instructions in the proper format Convert the data constants to internal machine representations Write the object program and the assembly listing Assembler Design Pass of a language processor – one complete scan of the source program Assembler Design can be done in: Single pass Two pass Single Pass Assembler: Does everything in single pass Cannot resolve the forward referencing Two pass assembler: Does the work in two pass Resolves the forward references Pass Structure of Assembler Single Pass Assembler Single Pass Assembler Two Pass Assembler Two Pass Assembler Task performed by Two Pass Assembler Task performed by Two Pass Assembler Two Pass Assembler Read from input line LABEL, OPCODE, OPERAND Source program Pass 1 OPTAB SYMTAB Intermediate file Pass 2 SYMTAB Object codes Assembler Design First pass: Validate Opcodes Scan the code by separating the symbol, mnemonic op code and operand fields . Assign Address to all the statements in the program Scan the source for labels and save their values Perform some processing of assembler directives Determine the length of areas defined by DC, DS Build the symbol table Perform LC processing Second Pass: Translate/assemble the instructions Generate Data Values defined by DC Process the rest of the assembler directives Write the Object Code and Assembly Listing Solves forward references Converts the code to the machine code Advanced Assembler Directives ORIGIN ORIGIN <address Specification> EQU <symbol> EQU <address specification> LTROG Allocated address for literals 84 Advanced Assembler Directives 1. ORIGIN < address specification> It sets the address of LC to the address given by address specification. 2. EQU <symbol> EQU <address specification> The EQU simply associate the <symbol> with the <address specifcation> e.g. BACK EQU LOOP The symbol BACK is set to the address of LOOP 3. LTORG At every LTORG statement , memory is allocated to the literals of the current pool of literals. 85 How to Design an Assembler : Two Pass Algorithm Pass 1 (Define symbols): a.Assign addresses to all statements (LOC) b.Save the addresses assigned to all labels in symbol table c.Perform some processing for assembler directives Pass 2 (Generate object code): a.Translate opcode and operands b.Generate data values for WORD c.Write object program 86 An enhanced machine opcode table (MOT) 87 Intermediate Code Generation 88 Intermediate Code Generation 89