Software: A collection of programs Computer Software: Introduction Program: Sequence of instructions for the computer to carry out Programs written using a programming language Types of languages: Machine language Assembly language High-level languages Everything computer uses when running is stored in main memory This means that programs are stored in binary format Ultimately, the CPU can only understand binary instructions Every language characterized by 1. Syntax Rules that specify the form of valid sentences in the language Rules called a grammar 2. Semantics The meanings associated with language constructs 1
Computer Software: Machine Language Each instruction is in pure binary format Instructions of form opcode operands There is an opcode for each specific operation the CPU can perform Operands may take the form of Values (e.g., 105, c ), or Addresses (RAM - which represent variables - or registers) Instructions may have from 0 to 3 operands Each family of chips (CPUs) uses a set of instructions specific to it For example: 10100101 01100000 11010101 00000110 10000101 01100001 6502 chip 8 bit opcode, 8 bit operand Performs x = y + 6 Problems: Instructions not easy to remember Easy to make mistakes Hard to find mistakes Programmer must explicitly manage memory 2
Uses mnemonic instructions Computer Software: Assembly Language 1:1 correspondence between assembly language and machine language For example: LDA 96 ADC 6 STA 97 6502 chip Performs x = y + 6: 1. Load Accumulator from address 96 (y) 2. Add 6 to contents of accumulator 3. Store contents of accumulator to address 97 (x) CPU cannot directly execute program because not binary Assembler: Program that converts assembly language into machine language Comparison to machine language: Easier to understand than machine code Less easy to make mistakes Easier to correct mistakes Requires extra step (assembly) Every time change code, must reassemble 3
Uses English-like instructions Computer Software: High-level Language Uses symbolic references (variables) to memory For example: x = y + 6 Example languages: BASIC, FORTRAN, COBOL, C, C++, Lisp, Java These are independent of any CPU Must be converted to machine language 1. Interpreter Reads one HL instruction at a time Translates that instruction to machine code Executes the machine code Repeats until every HL instruction executed 2. Compiler Entire HL program converted into a machine language program Machine program executed 4
Comparison of interpreters v compilers Interpreters: Computer Software: High-level Language (2) Programs execute slowly (must wait for each instruction to be translated) Catch errors exactly where they occur Compilers: Programs execute fast (cause is machine code) Generated errors may occur far from the actual problem Programs must be recompiled after every change There must be a compiler/interpreter for every family of chips Comparison of high-level v low-level languages Low-level: Most efficient code Can take advantage of chip s special features High-level Faster to write ( 10X faster) Self-documenting Allows programmer to ignore low-level details Portable 5
Computer Software: Systems Software Purpose is to manage the resources of the computer The main piece is the operating system (OS) Program that controls the operation of the computer (e.g., Windows, Mac OS X, Linux) OS provides the following services: 1. Interface Provides means for user to communicate with computer When user issues a command, the OS (a) Interprets the command (b) Schedules the corresponding program (c) After program done executing, returns control to user 2. Security Handles user names and passwords Controls access to system resources based on privileges 3. Efficient allocation of resources Handling requests for devices Multiple requests for a resource placed in a queue Multitasking: Doing several things at once Some tasks much slower than others; e.g., IO When waiting for slow operations to complete, can be working on another task Time sharing: Multiple users on the same computer Must insure no one monopolizes the CPU Give each user a time slice of computing 4. Safe use of resources Prevents situations that could cause computer to crash E.g., Access of areas of memory allocated to system programs and data Concurrency issues 6
Other types of system software: Computer Software: Systems Software (2) 1. Language translators (e.g., compilers) 2. Memory managers E.g., loaders and linkers (see later) Determine where things are stored in RAM 3. Information managers E.g., file systems, database systems 4. Scheduler Responsible for determining order of task execution 5. Utilities E.g., word processors, graphics tools, web browsers 7
Every program has 3 aspects: Computer Software: Program Development 1. Input - Data to be processed is made accessible to program 2. Process - Data is processed (producing meaningful info) 3. Output - Processing results are made available to user Software Life Cycle (Program Development Cycle) General process describing steps used to develop programs: 1. Analyze Basic problem-solving - identifying what the problem is Generate program requirements/specifications: Id input and output 2. Design Identify program constructs and processing required to solve problem Involves designing an algorithm: A step-by-step sequence of unambiguous instructions for solving a problem in a finite amount of time Example: Algorithm to convert binary to decimal Given: binary integer n Let power = 2 0 Let ans = 0 Let length = number of digits in n Let d = rightmost digit of n While have not processed all length digits yet Let ans = ans + power d Let power = next power of 2 Let d = digit to left of d return ans 8
Computer Software: Program Development (2) Example: Another algorithm to convert binary to decimal Given: binary integer n Let ans = 0 Let length = number of digits in n Let d = leftmost digit of n While have not processed all length digits yet Let ans = 2 ans + d Let d = digit to right of d return ans 3. Implement Write code **Compile **Debug (Syntax errors) 4. Test Generate test cases Run/execute the program Debug (Logic and semantic errors) 5. Maintain Includes documentation and tweaking as needed 9
Computer Software: Program Structure In general, all programming languages have same set of constructs Major differences are in syntax and paradigm (imperative, object-oriented, functional, declarative) General differences include 1. Fixed v free format Fixed format languages (e.g., FORTRAN) expect instructions to be placed just so on a line, usually one instruction per line Free format languages can place instructions anywhere without restriction Lines separated by special characters called delimiters 2. Case sensitive v non-case sensitive In languages that are case-sensitive, upper v lower case makes a difference 10
Computer Software: Language Components Every language consists of a small set of conceptual components 1. An alphabet A set of symbols from which the language is constructed 2. Reserved (key) words A set of words with special meaning in the language They cannot be redefined by the programmer 3. Syntax The rules governing valid sentences in the language Rules called a grammar For example, the following programs (in C and Lisp) to convert binary to decimal demonstrate syntax differences in languages 11
Computer Software: Language Components (2) //************************************************ //* Convert binary to decimal: C version * //************************************************ #include <stdio.h> #include <string.h> void reverse (char []); int main () { char input[32]; int i; int ans; int length; int power; scanf("%s", input); reverse(input); length = strlen(input); ans = 0; power = 1; for (i = 0; i < length; i++) { if (input[i] == 1 ) ans = ans + power; power = power * 2; } printf("result is %d\n", ans); } return 0; void reverse (char s[]) { int c, i, j; } for(i = 0, j = strlen(s) - 1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } 12
Computer Software: Language Components (3) ;************************************************ ;* Convert binary to decimal: Lisp version * ;************************************************ (defun bintodec () (let ((input (read)) (ans 0)) (setq input (reverse (princ-to-string input))) (do* ((i 0 (+ i 1)) (power 1 (* power 2))) ((= i (array-total-size input))) (if (char= #\1 (char input i)) (setq ans (+ ans power))) ) ans ) ) 4. Semantics The meaning behind the constructs of the language Learning a language is learning these components 13
Computer Software: Program Constructs In general, all programming languages have same set of constructs 1. Comments Documentation used to (a) Indicate history (e.g., programmer names, creation date, date last modified) (b) Explain algorithms used (c) Explain uses of variables (d) Etc. Comments ignored by compiler 2. Identifiers Strings/words that represent program components, e.g., (a) Variables (b) Constants (c) Reserved words 3. Variables Represent values stored in memory Have specific data type allocated specific amount of storage 4. Declarations Specify data type (integer, float, string,...) of construct Needed so (a) Computer allocates proper amount of storage (b) Computer knows how to interpret the binary value stored there 5. IO statements Accept input from mouse, keyboard, etc. Write output to file, display, printer, etc. 6. Assignment statement Assigns values to variables 14
7. Control statements Computer Software: Program Constructs (2) Control order in which statements are executed Sequence (default) Statements simply executed in order that they appear in the program Loop (iteration, repetition) Causes set of statements to be repeated E.g., for, while, do Usually number of times loop repeats controlled by a loop control variable Inifinite loop is one that loops forever, usually the result of incorrect usage of LCV Conditional (if) Determines which of a set of instructions are executed based on a condition (predicate) Conditions expressed in terms of (a) Relational operators (<, >, >=,...) (b) Boolean operators (AND, OR, NOT) Subprogram call Causes control to jump to a subprogram (function, method, subroutine) Subprogram executes like a miniprogram When done, control returns to point after call Values called parameters can be passed into the subprogram for processing Values can be returned by the subprogram 15
Computer Software: Program Constructs (3) Example: //************************************************ Comments //* Convert binary to decimal: C version * //************************************************ #include <stdio.h> #include <string.h> void reverse (char []); int main () { char input[32]; int i; int ans; int length; int power; Precompiler statements Function declaration Start of program Variable declarations scanf("%s", input); reverse(input); length = strlen(input); ans = 0; power = 1; for (i = 0; i < length; i++) { if (input[i] == 1 ) ans = ans + power; power = power * 2; Input statement Function call Assignment statements Start of loop statement Conditional statement } End of loop printf("result is %d\n", ans); Output statement } return 0; void reverse (char s[]) { int c, i, j; Function definition } for(i = 0, j = strlen(s) - 1; i < j; i++, j--) { c = s[i]; s[i] = s[j]; s[j] = c; } 16
Computer Software: Creating an Executable Program The process of compiling and executing a program involves a number of substeps 1. Precompiling Instructions to the precompiler are indicated with special symbols The precompiler searches the source code for them, and converts the code into instructions in the rogramming language 2. Compiling The modified source code is converted into assembly code It is most likely not complete It may access code that is stored in software libraries that has already been compiled Merging the component pieces of code occurs later 3. Assembling The assembly code is converted into machine code 17
4. Linking Computer Software: Creating an Executable Program (2) The linker finds the references in the object program to external code from the libraries The pieces of code are merged into a complete program 5. Loading The completed object program consists of relocatable code Variables, etc. do not have specific addresses The loader assigns program structures to memory and insures that all references are consistent The program and its variables are now in memory ready to run 6. Execution The address of the first instruction is loaded into the PC 18