931 likes | 2.66k Views
Assembly & Machine Language. Assembly Language. Machine Language. ST 1,[801] ST 0,[802] TOP: BEQ [802],10,BOT INCR [802] MUL [801],2,[803] ST [803],[801] JMP TOP BOT: LD A,[801] CALL PRINT. 00100101 11010011 00100100 11010100 10001010 01001001 11110000
E N D
Assembly & Machine Language Assembly Language Machine Language ST 1,[801] ST 0,[802] TOP: BEQ [802],10,BOT INCR [802] MUL [801],2,[803] ST [803],[801] JMP TOP BOT: LD A,[801] CALL PRINT 00100101 11010011 00100100 11010100 10001010 01001001 11110000 01000100 01010100 01001000 10100111 10100011 11100101 10101011 00000010 00101001 11010101 11010100 10101000 10010001 01000100
As a C/C++ program set memory[801] to hold 00000001 set memory[802] to hold 00000000 if memory[802] = 10 jump to instruction #8 increment memory[802] set memory[803] to 2 times memory[801] put memory[803] in to memory[801] jump to instruction #3 print memory[801] x=1; i=0; while (i!=10) { i++; x=x*2; } printf("%d",x); }
Compiler C++ Compiler C++ Program int main() { int i=1; . . . Machine Language Program 01001001 10010100 Created with text editor or development environment
Computer Organization • Six logical units in every computer: • Input unit • Obtains information from input devices (keyboard, mouse) • Output unit • Outputs information (to screen, to printer, to control other devices) • Memory unit • Rapid access, low capacity, stores input information • Arithmetic and logic unit (ALU) • Performs arithmetic calculations and logic decisions • Central processing unit (CPU) • Supervises and coordinates the other sections of the computer • Secondary storage unit • Cheap, long-term, high-capacity storage • Stores inactive programs
Evolution of Operating Systems • Batch processing • Do only one job or task at a time • Operating systems • Manage transitions between jobs • Increased throughput • Amount of work computers process • Multiprogramming • Computer resources are shared by many jobs or tasks • Timesharing • Computer runs a small portion of one user’s job then moves on to service the next user
Personal Computing, Distributed Computing, and Client/Server Computing • Personal computers • Economical enough for individual • Distributed computing • Computing distributed over networks • Client/server computing • Sharing of information across computer networks between file servers and clients (personal computers)
Computer Languages • Machine language • Generally consist of strings of numbers - Ultimately 0s and 1s - Machine-dependent • Assembly language • English-like abbreviations for elementary operations • Incomprehensible to computers - Convert to machine language • Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY • High-level languages • Similar to everyday English, use common mathematical notations • Compiler • Example: grossPay = basePay + overTimePay
C++ • C++ is an extension of C. • C++ was first developed in the early 1980s (back in the last century!). • Focus was on Object Oriented Programming • view computer programs as collection of objects. • Objects have attributes and actions.
Structure of a C++ Program • A C++ program is a collection of definitions and declarations: • data type definitions • global data declarations • function definitions (subroutines) • class definitions • a special function called main() (where the action starts).
History of C and C++ • History of C • Evolved from two other programming languages • BCPL and B: “Typeless” languages • Dennis Ritchie (Bell Lab): Added typing, other features • 1989: ANSI standard/ ANSI/ISO 9899: 1990 • History of C++ • Early 1980s: Bjarne Stroustrup (Bell Lab) • Provides capabilities for object-oriented programming • Objects: reusable software components • Object-oriented programs • Building block approach” to creating programs • C++ programs are built from pieces called classes and functions • C++ standard library: Rich collections of existing classes and functions
Structured/OO Programming • Structured programming (1960s) • Disciplined approach to writing programs • Clear, easy to test and debug, and easy to modify • E.g.Pascal:1971: Niklaus Wirth • OOP • “Software reuse” • “Modularity” • “Extensible” • More understandable, better organized and easier to maintain than procedural programming
The C++ Standard Library • C/C++ programs consist of pieces/modules called functions • A programmer can create his own functions • Advantage: the programmer knows exactly how it works • Disadvantage: time consuming • Programmers will often use the C/C++ library functions • Use these as building blocks • Avoid re-inventing the wheel • If a pre-made function exists, generally best to use it rather than write your own • Library functions carefully written, efficient, and portable
Basics of a Typical C++ Environment • C++ systems • Program-development environment • Language • C++ Standard Library • C++ program names extensions • .cpp • .c
Program is created in the editor and stored on disk. Preprocessor program processes the code. Compiler creates object code and stores it on disk. Compiler Linker links the object code with the libraries, creates a.out and stores it on disk Primary Memory Loader Loader puts program in memory. Primary Memory CPU takes each instruction and executes it, possibly storing new data values as the program executes. Preprocessor Linker Editor Disk Disk Disk Disk Disk CPU . . . . . . . . . . . . Basics of a Typical C++ Environment • Phases of C++ Programs: • Edit • Preprocess • Compile • Link • Load • Execute
Hello World++ comment // Hello World program #include <iostream.h> int main() { cout << "Hello World\n"; return 0; } Allows access to an I/O library Starts definition of special function main() output (print) a string Program returns a status code (0 means OK)
Multiple stream insertion statements produce one line of output. 1 // C++ Prgram 2 // Printing a line with multiple statements. 3 #include <iostream> 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome "; 9 std::cout << "to C++!\n"; 10 11 return0; // indicate that program ended successfully 12 13 } // end function main Welcome to C++!
Using newline characters to print on multiple lines. 1 // C++ Program 2 // Printing multiple lines with a single statement 3 #include <iostream> 4 5 // function main begins program execution 6 int main() 7 { 8 std::cout << "Welcome\nto\n\nC++!\n"; 9 10 return0; // indicate that program ended successfully 11 12 } // end function main Welcome to C++!
Comments • Comments contain text that is not converted to machine language (it's just there for humans). • Everything after "//" is ignored by the compiler. • Everything between "/*" and "*/" is ignored. • Document programs • Improve program readability • Ignored by compiler • Single-line comment
Preprocessor Directives • Preprocessor directives: Begin with # Processed before compiling • # include • # define
Includes • The statement: #include <myfile.h> inserts the contents of the file myfile.h inside your file before the compiler starts. • Definitions that allow your program to use the functions and classes that make up the standard C++ library are in these files. • You can include your own file(s): #include "myfile.h"
Some common includes • Basic I/O: iostream.h • I/O manipulation: iomanip.h • Standard Library: stdlib.h • Time and Date support: time.h • Mathematics support: math.h
C++ Preprocessor • C++ Compilers automatically invoke a preprocessor that takes care of #include statements and some other special directives. • You don't need to do anything special to run the preprocessor - it happens automatically.
Preprocessing Temporary file (C++ program) C++ Preprocessor C++ Compiler C++ Program Executable Program
The Preprocessor • Lines that start with the character '#' are special instructions to a preprocessor. • The preprocessor can replace the line with something else: • include: replaced with contents of a file • Other directives tell the preprocessor to look for patterns in the program and do some fancy processing.
#define (macro) Example #define square(a) (a * a) y = square(x); z = square(y*x); becomes y = (x * x); becomes z = (y*x * y*x);
Basics of a Typical C++ Environment • Common Input/output functions • cin • Standard input stream • Normally keyboard • cout • Standard output stream • Normally computer screen • cerr • Standard error stream • Display error messages
A Simple Program: Printing a Line of Text • Standard output stream object • std::cout • “Connected” to screen • << • Stream insertion operator • Value to right (right operand) inserted into output stream • Namespace • std:: specifies that entity belongs to “namespace” using binary scope resolution operator(::) • std:: removed through use of using statements • Escape characters: \ • Indicates “special” character output
Another Simple Program: Adding Two Integers • Variables • Location in memory where value can be stored • Common data types • int - integer numbers • char - characters • double - floating point numbers • Declare variables with name and data type before use int integer1; int integer2; int sum; • Can declare several variables of same type in one declaration • Comma-separated list int integer1, integer2, sum;
Adding Two Integers • Variables • Variable names • Valid identifier • Series of characters (letters, digits, underscores) • Cannot begin with digit • Case sensitive
Adding Two Integers • Input stream object • >> (stream extraction operator) • Used with std::cin • Waits for user to input value, then press Enter (Return) key • Stores value in variable to right of operator • Converts value to variable data type • = (assignment operator) • Assigns value to variable • Binary operator (two operands) • Example: sum = variable1 + variable2;
Another C++ Program // C++ Addition of integers #include <iostream.h> int main() { int integer1, integer2, sum; cout << "Enter first integer\n"; cin >> integer1; cout << "Enter second integer\n"; cin >> integer2; sum = integer1 + integer2; cout << "Sum is " << sum << endl; return 0; }
1 // C++ Program 2 // Addition program. 3 #include <iostream> 4 5 // function main begins program execution 6 int main() 7 { 8 int integer1; // first number to be input by user 9 int integer2; // second number to be input by user 10 int sum; // variable in which sum will be stored 11 12 std::cout << "Enter first integer\n"; // prompt 13 std::cin >> integer1; // read an integer 14 15 std::cout << "Enter second integer\n"; // prompt 16 std::cin >> integer2; // read an integer 17 18 sum = integer1 + integer2; // assign result to sum 19 20 std::cout << "Sum is " << sum << std::endl; // print sum 21 22 return0; // indicate that program ended successfully 23 24 } // end function main Declare integer variables. Use stream extraction operator with standard input stream to obtain user input. Calculations can be performed in output statements: alternative for lines 18 and 20: std::cout << "Sum is " << integer1 + integer2 << std::endl; Stream manipulator std::endl outputs a newline, then “flushes output buffer.” Concatenating, chaining or cascading stream insertion operations.
Enter first integer 45 Enter second integer 72 Sum is 117
45 45 72 45 72 integer2 integer1 integer1 integer1 integer2 117 sum Memory Concepts • Variable names • Correspond to actual locations in computer's memory • Every variable has name, type, size and value • When new value placed into variable, overwrites previous value • std::cin >> integer1; • Assume user entered 45 • std::cin >> integer2; • Assume user entered 72 • sum = integer1 + integer2;