160 likes | 169 Views
Programming Languages. How would we code this pseudocode with our machine language?. Procedure negative ( x ) If x < 0 Then return 1 Else return 0.
E N D
Programming Languages How would we code this pseudocode with our machine language? Procedure negative (x) Ifx < 0 Then return 1 Else return 0 (We’ll assume that the value of x has been stored in main memory at address 5A, and that the returned value (0 or 1) will be placed at address 5B.) Chapter 6 – Programming Languages
Let’s take advantage of the fact that if an 8-bit two’s-complement number is ANDed with 10000000, the result is 00000000 if the number is positive, and 10000000 if the number is negative. Chapter 6 – Programming Languages
Assembly (“Second-Generation”) Languages To make programming easier to handle, special languages (unique to the kinds of computers running them) have been developed. Programs written in such an assembly language are executed by first passing through a special program (called an assembler) that translates the assembly language code into machine language. Chapter 6 – Programming Languages
The Problems With Assembly Languages While assembly languages are easier to use than machine languages, they still have two big problems: 1. Assembly languages are still machine-dependent. A program written in one machine’s assembly language cannot be executed on a computer with a different instruction set and register configuration. 2. Assembly language programming is still too nitpicky. Programmers are forced to concentrate on the tiny details necessary to choreograph the computer’s activity, instead of focusing on the overall solution of the problem being tackled. Chapter 6 – Programming Languages
Third-Generation Languages Third-generation languages (e.g., BASIC, FORTRAN, COBOL, C) were developed as a solution to the assembly language problems. Third-generation languages are structured to avoid considering machine operations at all, instead concentrating on relatively straightforward instructions on how the data is being manipulated. Each type of computer that executes programs in a TGL has a special program (called a compiler) that translates the TGL code into the computer’s machine language. Chapter 6 – Programming Languages
Lexical Analysis Code Generation Parsing Object Program (in Machine Language) Compilation Source Program (in TGL) Lexical Analysis The compiler takes the TGL program (called the source program) and determines which strings of characters form separate items (e.g., “if (count > 100)” is split into “if”, “(”, “count”, “>”, “100”, and “)”), and all comments and white space (blanks, line feeds, etc.) are deleted. Parsing The compiler then analyzes the grammatical syntax of the program (e.g., “if”, “(”, “count”, “>”, “100”, “)” is determined to make sense, but a syntax error would be noticed in “if”, “count”, “>”, “100”, “)”.) Code Generation Once the program has been satisfactorily parsed, the compiler generates an equivalent program in machine language (called the object program). Chapter 6 – Programming Languages
Object Program Load Module Executable Program Link Load Compile Linking & Loading Since the individual portions of the TGL program are compiled as separate units (e.g., your program, a math library, a graphics library, etc.), the resulting machine code cannot be executed until all of the units are connected together as a single machine language program. Source Program Linking A TGL programmer usually relies on pre-compiled libraries of code (math functions, graphics routines, I/O operations, etc.) that are connected to the programmer’s code prior to execution by a linker program. Loading Finally, a special loader program places the resulting machine code in main memory, tying up all loose ends (e.g., setting the instruction addresses for JUMP instructions) so the code is ready for execution. Chapter 6 – Programming Languages
Declarative Statements Constant and variable values representing terms that will be manipulated as the program is executed. Imperative Statements The procedural specification of the algorithm itself. Standard Source Program Organization Source programs in most third-generation languages generally follow a standard pattern: void main() { const int maxCount = 10; int count; int value; float sum = 0.0; cout << “Input values” << endl; count = 0; while (count < maxCount) { cin >> value; sum += value; count++; } cout << “Mean value: ” << sum/count << endl; } Chapter 6 – Programming Languages
Data Types Data types are used to specify how the bit patterns representing data shall be interpreted. int count; float GPA; bool flag; Scalar: Single-valued data types (e.g., integer, floating-point, character, boolean) Structured: Multiple-valued data types Built-In: Arrays, character strings float WebGooOuncesPerDay[5]; char nickname[] = “Spidey”; User-Defined: Specially constructed struct Student { char name[30]; int examScore[5]; int quizScore[25]; int paperScore[2]; char letterGrade; }; Student FALL111[25]; Chapter 6 – Programming Languages
Imperative Statements I: Assignment & I/O Assignment statements are used to assign a value to a variable. x = 127; count = count + 1; E = m * c * c; Input/output statements are used to retrieve external values (input) and to file away or print information (output). cout << “Enter user’s name: ”; cin >> username; dataFile >> nextDataValue; if (nextDataValue > 0) positiveFile << nextDataValue; Chapter 6 – Programming Languages
Imperative Statements II: Control Statements Conditional statements are used to enable alternative steps based on a condition. switch (AreaCode) { case 701: cout << “ND”; break; case 218: case 507: case 612: cout << “MN”; break; } if (total == 0) cout << “Possible Drop”; else cout << “Total: ” << total; Iterative statements are used to loop through a sequence of instructions. total = 0; for (i = 0; i <= 24; i++) { quizFile >> score[i]; total += score[i]; } while (flag == false) { cin >> newValue; if (newValue > 0) flag = true; } Chapter 6 – Programming Languages
Imperative Statements III: Procedures & Functions Procedures and functions are used to conveniently write programs in a modular fashion. typedef int intList[100]; void getList(intList list) { int count; for (count = 0; count < 100; count++) cin >> list[count]; } int maximum(intList list) { int maxSoFar; int count; maxSoFar = list[0]; for (count = 1; count < 100; count++) if (list[count] > maxSoFar) maxSoFar = list[count]; return maxSoFar; } void main() { intList IQlist; intList SATlist; int maxIQ; int bestSAT; getList(IQlist); maxIQ = maximum(IQlist); getList(SATlist); bestSAT = maximum(SATlist); cout << “The highest IQ is ” << maxIQ << “ and the ” << “best SAT score is ” << bestSAT; } Chapter 6 – Programming Languages
Example: What Does This Program Do? typedef int intList[4]; void getList(intList list) { int count; for (count = 0; count < 4; count++) cin >> list[count]; } int barker(intList list, int item) { int bestSoFar, bestIndex, index; bestIndex = -1; bestSoFar = 0; for (index = 0; index < 4; index++) if ((list[index] > bestSoFar) && (list[index] <= item)) { bestIndex = index; bestSoFar = list[index]; } return bestIndex; } void main() { intList estimate; int bestGuesser; int price; cin >> price; getList(estimate); bestGuesser = barker(estimate, price); if (bestGuesser == -1) cout << “NO WINNER”; else { cout << bestGuesser << “ WINS! ”; if (estimate[bestGuesser] == price) cout << “WITH A BONUS!!!”; } } What would be the output of this program for the following input file? 600 450 375 525 700 Chapter 6 – Programming Languages
Object-Oriented Programming Until recently, programming used a “procedure-oriented” approach, in which the way something was done was the center of attention for the programmer. More recently, with the advent of graphical user interfaces and massive databases, the focus has shifted to an “object-oriented” approach, emphasizing what is being manipulated instead of how. Chapter 6 – Programming Languages
The Three Principles of OOP • Encapsulation • “Hide” information from objects that don’t need it. • Is the search being performed sequential or binary? • Is the data in an array or separate variables? • Is the input coming from the user or from a file? • The code will be more robust if it’s not unnecessarily dependent on • information that it can perform without! • Inheritance • Don’t “reinvent the wheel” when creating new data types. • A GUI Window is rectangular with a title bar. • A Document Window also has a menu bar, and max & min buttons. • Why not let the Document Window “inherit” as much behavior as possible • from the GUI Window (e.g., how to draw it, how to place text in its title bar)? • Polymorphism • Some objects are “similar”, without being the same. • A Triangle object needs its own method for “Drawing”. • A Circle object needs its own method for “Drawing”. • With polymorphism, you can write code to invoke “Drawing” without • having to spell out what type of “Drawing” is intended. Chapter 6 – Programming Languages
Parallel Processing Computers with multiple CPUs need special programming languages to enable them to efficiently split one task between the processors. Chapter 6 – Programming Languages