Connecting with Computer Science, 2e Chapter 14 Programming IConnecting with Computer Science, 2e 2 Objectives • In this chapter you will: – Learn what a program is and how it’s developed – Understand the difference between a low-level and high-level language – Be introduced to low-level languages, using assembly language as an example – Learn about program structure, including algorithms and pseudocodeConnecting with Computer Science, 2e 3 Objectives (cont’d.) • In this chapter you will (cont’d.): – Learn about variables and how they’re used – Explore the control structures used in programming – Understand the terms used in object-oriented programmingWhy You Need to Know About... Programming • Examples of programs in everyday functions: – Cars, space shuttles, ATMs, and microwaves • It is important to develop a quality programming product – People depend on it • Programming is essential to future computing career Connecting with Computer Science, 2e 4What Is a Program? • Program: – Collection of statements or steps • Solves a problem • Converted into a language the computer understands to perform tasks • Algorithm: – Logically ordered set of statements • Used to solve a problem • Interpreter: – Translates program’s statements into a language the computer understands Connecting with Computer Science, 2e 5What Is a Program? (cont’d.) • Compiler: – Application reading all the program’s statements • Converts them into computer language • Produces an executable file running independently of an interpreter • Programs are developed to help perform tasks – Communicate with users to meet their needs • Causes of program failure: – Piece of logical functionality left out of the program – Contains logic errors in one or more statements Connecting with Computer Science, 2e 6Connecting with Computer Science, 2e 7 I Speak Computer • First step in programming – Determine language to communicate with the computer – Computers only speak binary • Many choices: – Ada, Assembly, C, C++, C# – COBOL, FORTRAN, Delphi (Pascal) – Java and JavaScript – Lisp, Perl, Smalltalk, Visual Basic • Each has its own strengths and weaknessesI Speak Computer (cont’d.) • Low-level language: – Uses binary code for instructions • Machine language: – Lowest-level programming language • Consists of binary bit patterns • Assembly language: – One step up from machine language • Assigns letter codes to each machine-language instruction Connecting with Computer Science, 2e 8Connecting with Computer Science, 2e 9 I Speak Computer (cont’d.) • Assembler: – Program that reads assembly-language code and converts it into machine language • High-level language: – Written in a more natural language that humans can read and understandConnecting with Computer Science, 2e 10 Figure 14-1, Different types of programming languages I Speak Computer (cont’d.)Low-Level Languages • Few people code in machine language • Assembly language: – Simulates machine language – Written with more English-like statements – Advantages: • Corresponds to one machine instruction • Programs are usually smaller and run faster than programs in higher-level languages • Powerful – Closely tied to the CPU type • Assemblers written for every type of CPU Connecting with Computer Science, 2e 11Assembly-Language Statements • Registers in a CPU – Special memory locations for storing information programs can use – Registers: AX, BX, CX, and DX • General-purpose registers (GPRs) • Used mainly for arithmetic operations or accessing an element in an array • Consists of text instructions – Converted one by one into machine (binary) instructions • Disadvantage: hard to read and understand Connecting with Computer Science, 2e 12Assembly-Language Statements (cont’d.) • Syntax: – Rules for how a programming language’s statements must be constructed • mov: moves values around – Example: move the value of 8 into the CX register • mov cx, 8 – Can move a value from memory to a register, from a register to memory, from register to register • mov dx, cx Connecting with Computer Science, 2e 13Assembly-Language Statements (cont’d.) • add: takes a value on the right and adds it to the value on the left – Example: storing value of 11 in DX register • mov cx, 3 • mov dx, 8 • add dx, cx • inc: adds 1 to the register being used – Example: add 1 to DX register to get 12 • inc dx Connecting with Computer Science, 2e 14Assembly-Language Statements (cont’d.) • sub: tells the assembler to subtract one number from another number – Example: DX = DX – CX • CX register still contains value 4 • DX register contains value 3 • mov cx, 4 • mov dx, 7 • sub dx, cx Connecting with Computer Science, 2e 15Assembly-Language Statements (cont’d.) • cmp: tells assembler to compare two values – Result sets flag bits in the flags (FL) register • If the result of the compare equals 0, zero (ZR) flag is set to a binary 1, and the sign (SF) flag is set to 0 • If the result of the compare is a negative number, ZR flag bit is set to a binary 0, and the SF flag is set to 1 – Example: DX – CX = 0, ZR flag is set to 1 • mov cx, 4 • mov dx, 7 • cmp dx, cx Connecting with Computer Science, 2e 16Assembly-Language Statements (cont’d.) • jnz: tests value of ZR flag maintained by the system – If set to 1: jump somewhere else in the program – Not set: assembler continues to process code on the next line – Example: • mov cx, 4 • mov dx, 7 • cmp dx, cx • jnz stop Connecting with Computer Science, 2e 17High-Level Languages • Writes programs independent of computer or CPU • Advantages: – Easier to write, read, and maintain – Can accomplish much more with a single statement – No one-to-one relationship between a statement and a binary instruction • Disadvantages: – Programs generally run slower • Must be compiled or interpreted • Examples: Java, C++, Delphi, and C# Connecting with Computer Science, 2e
View Full Document