* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Introduction
Structured programming wikipedia , lookup
Java performance wikipedia , lookup
Library (computing) wikipedia , lookup
Parallel computing wikipedia , lookup
History of compiler construction wikipedia , lookup
Go (programming language) wikipedia , lookup
Program optimization wikipedia , lookup
C Sharp (programming language) wikipedia , lookup
Introduction to Computers and Programming Hardware Software Computer languages Compiling, interpreting and assembling 1 Computer Basics A computer system consists of both hardware and software. Hardware - the physical components Software - the instructions, or rather, computer programs, that tell the hardware what to do 2 Common Hardware Components Memory Input Devices Output Devices Processor (CPU) (such as mouse and keyboard) Processor: (such as video display or printer) Input devices: Central Processing Unit (CPU) keyboard, mouse, touch-screen interprets and executes program instructions game controllers Memory: sensors Output device(s) volatile and non-volatile video display, printer holds data and instructions robotic devices 3 Classification of Memory At a high-level there are two types of memory: Volatile – contents are lost when power is turned off: Main memory (stores programs and data during execution) Cache memory Fastest and most expensive form of memory, per byte Non-Volatile – contents are maintained when power is turned off: Hard drive (internal or external, stores programs and data permanently) CD, DVD Flash drive, floppy disk Tape (still used extensively) Slowest and cheapest form of memory, per byte 4 Memory Organization Bit = one binary digit, either 0 or 1 Byte = 8 bits Word = machine dependant, typically 4 bytes Larger groupings: (number of bytes) name approximation exact Kilobyte (KB) 2^10 10^3 Megabyte (MB) 2^20 10^6 Gigabyte (GB) 2^30 10^9 Terabytes (TB) 2^40 10^12 Petabyte (PB) 2^50 10^15 Exabyte (EB) 2^60 10^18 Zetabyte (ZB) 2^70 10^21 Yottabyte (YB) 2^80 10^24 5 Binary Encodings Everything in memory is “encoded” in binary, i.e., as a sequence of bits. “A” 45 “DOG” => => => 1000001 101101 01000100 01001111 01000111 Generally, the encoding of an object is unique among all similar objects, e.g., the encoding of “A” is different from the encoding of “B.” How many binary sequences are there on n bits? How many bits are needed to uniquely encode k items? 6 Main Memory Organization Main memory: A list of locations, each containing one byte of data. Each location has an associated “number,” which is commonly referred to as its’ address. Is said to be byte addressable. Also called Random Access Memory (RAM). The number of bytes per data item may vary from one item to another, and from one computer system to another. Integer => 4 bytes (1 word) Character => 1 or 2 bytes 7 Address Contents 3021 1111 0000 3022 1100 1100 3023 1010 1010 Item 2 (1 byte) 3024 1100 1110 Item 3 (3 bytes) 3025 0011 0001 3026 1110 0001 3027 0110 0011 3028 1010 0010 3029 … Item 1 (2 bytes) Item 4 (2 bytes) Next Item, etc. Running (Executing) a Program A (computer) program is a set of instructions for a computer to follow, or rather, execute. The term application is sometimes used to informally refer to a computer program (we will use the term more formally later). Program Data (input for the program) Output Computer 8 Many Types of Programs System Software - Part of the computers “infrastructure,” and necessary for the system to operate: Operating Systems - DOS, Microsoft Windows, MacOS, Linux, UNIX, etc. Database Systems – Oracle, IBM DB2, SQL Server, Access Networking Software Web Servers Application Servers User Applications - Not required for the system to operate: Games Office Applications – Word, Powerpoint, Excel Web Browsers Text Editors – textedit, vi, emacs, notepad 9 Various Types of User Interfaces Graphical User Interface (GUI) Windows, menus, buttons, sliders, etc. MacOS, Windows Sometimes also called “event-driven” interfaces First developed by Xerox Corporation Command-Line: User types in commands one line at a time DOS (Start -> run -> cmd) Unix xterm Application Program Interface (API) Allows one program to communication, interact or “interface” with another, or with some external, physical device. ODBC, JDBC, Swing, AWT 10 Programming Language Hierarchy Programs are written, or coded, in a programming language. There are many different types of programming languages. High-Level Language Assembly Language Machine Language 11 High-Level Languages High-Level Language (HLL): Java, C, C++, C#, COBOL, FORTRAN, BASIC, Lisp, Ada, etc. closest to natural language words, numbers, and math symbols multi-line statements/commands relatively easy for people to read (it’s for people) not directly understood by hardware “portable” (hardware independent) A program in a HLL is frequently referred to as: a source program source code source file source 12 High-Level Language Example (Java) public class SimpleProgram { public static void main(String[] args) { System.out.println(“Hello out there.”); System.out.println(“I will add two numbers for you.”); int x; double d; x = 3752; d = 3.14156; System.out.println(“The sum of ” + x + “ and ” + d + “ is:”); System.out.println(x + d); } } 13 Machine Languages Machine Language (lowest level): just 0s and 1s single-line commands very difficult for humans to read directly understood by hardware not portable (hardware dependent) A program in machine language is frequently referred to as: an object program object code executable program executable code executable 14 Machine Language Example 000000 000001 000010 000110 000000 100000 100011 000011 001000 000000 000001 000100 : : 15 Assembly Languages Assembly Language: a more readable version of machine language (also for people) words, abbreviations, letters and numbers replace 0s and 1s single-line statements/commands easily translated to machine executable code like machine code, not portable (hardware dependent) So, if we already have HLLs for people, why do we need assembly language? compilation results in a loss of meaning…sort of control over the resulting machine code 16 Assembly Language Example PUSH DX MOV AH 08 INT 21 CMP AL 30 JB 0203 CMP AL 46 JA 0203 CMP AL 39 JA 021B MOV AH 02 MOV DL AL INT 21 SUB AL 30 POP DX RET CMP AL 41 JB 0203 MOV AH 02 MOV DL AL INT 21 SUB AL 37 POP DX RET 17 Getting from Source to Machine Code Translating a program in a high-level language to machine code is called compiling. A program that compiles programs is called a compiler. A program that translates an assembly language program into machine code is called an assembler. Compilers and assemblers need to know the specific target hardware. There is, however, one additional type of translation called interpreting… 18 Compilers vs. Assemblers vs. Interpreters Compilers and Assemblers: translation is a separate user step from execution entire source program is translated before execution sometimes said to be “off-line,” i.e. not at run time Interpreters: (another way to translate source to object code) translation is not a separate user step from execution translation and execution occur “line at a time” sometimes said to be “on-line,” i.e. at run time The difference between compiling (or assembling) and interpreting a computer program, is analogous to the difference between translating a book, and interpreting a speaker. 19 Java Program Translation Executing a java program involves both compilation and interpretation. Java Program Translation & Execution: Step #1: A java program is compiled; this produces a program in byte code. Similar to assembly code, but hardware independent. Step #2: An interpreter, called the Java Virtual Machine (JVM) translates the byte code program to hardware-specific machine code, and executes it. Execution is done in an interpretive manner Question: Why not compile directly to machine code, rather than byte code? Answer: To make a long story short, portability over the internet. 20 Java Program Translation Including Linker Oh, and, one other part of the translation process is linking… Java Program Previously Compiled Helper Programs Java Compiler Byte-Code Program Byte-Code Interpreter Machine-Language Instructions Class Loader (i.e., Linker) Computer Execution of Machine-Language Instructions 21