introduction 1 (read chap. 1) what is programming? for some given problem: design a solution for it...

15
Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data -- develop algorithms (procedures) to process it; code this solution as a program; test the program; maintain the program (fix it, upgrade it, …). What is a program? — a collection of statements that — implement the design plan, and CS 104: Applied C++ OCD in C+ + debug "real world "

Upload: angela-meagan-allen

Post on 17-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Introduction

1

(Read Chap. 1)

What is Programming?For some given problem:

• design a solution for it -- identify, organize & store the problem's

data-- develop algorithms (procedures) to

process it;• code this solution as a program; • test the program;• maintain the program (fix it, upgrade it, …).

What is a program?— a collection of statements that— implement the design plan, and — are written in a programming language — a language that the computer can understand.

CS 104: Applied C++

OCD

in C++

debug

"real world"

Page 2: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

What kinds of statementsdo computers understand?

• A computer only understands a language specially designed for it called machine language.

• Machine-language statements are stored in a computer’s memory, which is a sequence of two-state devices (on-off switches). They are retrieved from memory and executed one at a time.

• The "character set" for machine language:__ representing "off"__ representing "on"

2

RISCCISC

01

Page 3: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Machine language programs thus consists of strings of bits ( ).

3

0001000000000000000001000000001010010010000000000000010000000001001000110000000000000100000000100001000100000000000001000000001100010000000000110100010000000000001001000000000000000100000110010010001100000000000001000000001000010001000000001000010000000011000111000000000000010...

Example (hypothetical)

binary digits

Page 4: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

4

Bits are usually grouped into bytes (8 bits) andwords (e.g., 32 or 64 bits). Each machine language instruction might be stored in one word.

opcode

0001000000000000000101000000000000010010000000000000010000000001001000110000000000000100000000100001000100000000000001000000001100010000000000110100010000000000001001000000000000000100000110010010001100000000000001000000001000010001000000001000010000000011000111000000000000010...

•instruction #1instruction #1

first operand second operand

e.g.,

"Store" 1

in memory location with this address

Page 5: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

5

So a sequence of machine language instructions might be stored in a sequence of consecutive words.

0001000000000000000101000000000000010010000000000000010000000001001000110000000000000100000000100001000100000000000001000000001100010000000000110100010000000000001001000000000000000100000110010010001100000000000001000000001000010001000000001000010000000011000111000000000000010...

instruction #1

#4

#3

#2

Page 6: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

SPARC executable machine code

000001110111010100000101 000001000110000100000110000000000000010000000010 000000000000010000000000 000000000000000000000000 000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000010 000000000000000000000010000000000000000000000000 000000000000000000000001 000000000000000000000001 000000000011010000110000000000000000000000000000 000000000000000001100100000000000000000000000000 000000010011000100100100000000000000000000000000 000000000000000000000000 000000000000000001100100 000000000000000001000000000000000000000000000101 000000000000000001010000000000000000000000110011 000000000000000000110001000000000000000000000000 000000000000000000000110 000000000000000000000000 000000000000000001100100000000000000000000000001 000000000000000001100100000000000000000000000000 000000000000000000000000000000000000000000000000 000000000000001001000000000000000000000000000000 000000000000001001000000000000000000000000000000 000000000000000000000101000000000000000000000000 000000000000000000000000000000000000000000000000 000000000000000000000011 000000000000000000000000 000000000000001100100100000000000000000000000000 000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000 000000000000000000100001

… this goes on for another 1600 lines...

Intel Pentium executable machine code

000000100111000000010001 000001100100010101000110000001100010010101010100 000000100001000000010001000001100111000101010001 000001110001000101100100000000100111000101010111 000000100001000101000011000000000101000000010101 000001100001010101000111000000110001000101000011 000001100001010100110111000001100110010101010111 000001100100010101100000000001100010010101010100 000000100111000101000100000000000110010001110010 000001010111010000010010000001010111010100110111 000001100111000101000111000001010111010101100101 000001100111010101000011000001110000000101010101 000001100110000101010001000001100010000101000101 000001100001010100110111000000000110010001110010 000000000100010000010010000001100010000001010110 000001100011000101000101000001010111010000010001 000001010111010100110111000001100000010101010101 000001100111000101010001000000000100010001110011 000001110001010001010110000001100110000101000011 000000110001000000010001000000000100010001110011 000001110010000001010110000001110000000101110001 000000000100010101000101000000110001000001100011 000000000100010001110011000001100010010001010110 000001100010000101010110000001100011000101000101 000000000101000000010101000001110010000001010110 000001110100000101000101000000000110010101100100 000000000100010000010010

… this goes on for another 74 lines...

A Real Machine-Language Example

6

int main(){ int x, y; x = 1; y = x + 2; return 0;}

C++

Page 7: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Early Computers

•Required a programmer to write in machine language.

– Very easy to make mistakes!

– And they were hard to find!

– Programs were not portable.They could only be run on one kind of machine!

•Result: programming was very difficult and programs weren't widely used.

7

Page 8: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

An Early Innovation

Create a machine-language program called an assembler to input each assembly language instructionand translate it into machine language.

100000001010100111000100000000101000000110111100

LOAD xADD 2STORE y

Assembler

8

Devise a set of mnemonics (abbreviations), one for each machine language instruction; this was called anassembly language.

Page 9: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Intel Pentium assembly language:

_main:pushl %ebpmovl %esp,%ebpsubl $24,%espcall ___mainmovl $1,-4(%ebp)movl -4(%ebp),%eaxaddl $2,%eaxmovl %eax,-8(%ebp)xorl %eax,%eaxjmp L2.align 4

L2:movl %ebp,%esppopl %ebpret

000001110111010100000101 000001000110000100000110000000000000010000000010 000000000000010000000000 000000000000000000000000 000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000010 000000000000000000000010000000000000000000000000 000000000000000000000001 000000000000000000000001 000000000011010000110000000000000000000000000000 000000000000000001100100000000000000000000000000 000000010011000100100100000000000000000000000000 000000000000000000000000 000000000000000001100100 000000000000000001000000000000000000000000000101 000000000000000001010000000000000000000000110011 000000000000000000110001000000000000000000000000 000000000000000000000110 000000000000000000000000 000000000000000001100100000000000000000000000001 000000000000000001100100000000000000000000000000 000000000000000000000000000000000000000000000000 000000000000001001000000000000000000000000000000 000000000000001001000000000000000000000000000000 000000000000000000000101000000000000000000000000 000000000000000000000000000000000000000000000000 000000000000000000000011 000000000000000000000000 000000000000001100100100000000000000000000000000 000000000000000000000000000000000000000000000000 000000000000000000000000

000000000000000000000000 000000000000000000100001

IntelAssembler

9

The Real Example

SPARC assembly language:

main:save %sp, -120, %spmov 1, %o0st %o0, [%fp-20]ld [%fp-20], %o0add %o0, 2, %o1st %o1, [%fp-24]mov 0, %i0b .LL2nopmov 0, %i0b .LL2nop

.LL2:retrestore

000000100111000000010001 000001100100010101000110000001100010010101010100 000000100001000000010001000001100111000101010001 000001110001000101100100000000100111000101010111 000000100001000101000011000000000101000000010101 000001100001010101000111000000110001000101000011 000001100001010100110111000001100110010101010111 000001100100010101100000000001100010010101010100 000000100111000101000100000000000110010001110010 000001010111010000010010000001010111010100110111 000001100111000101000111000001010111010101100101 000001100111010101000011000001110000000101010101 000001100110000101010001000001100010000101000101 000001100001010100110111000000000110010001110010 000000000100010000010010000001100010000001010110 000001100011000101000101000001010111010000010001 000001010111010100110111000001100000010101010101 000001100111000101010001000000000100010001110011 000001110001010001010110000001100110000101000011 000000110001000000010001000000000100010001110011 000001110010000001010110000001110000000101110001 000000000100010101000101000000110001000001100011 000000000100010001110011000001100010010001010110 000001100010000101010110000001100011000101000101 000000000101000000010101000001110010000001010110 000001110100000101000101000000000110010101100100 000000000100010000010010

SunAssembler

int main(){ int x, y; x = 1; y = x + 2; return 0;}

Page 10: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Assembly Languages

Allowed a programmer to use mnemonics, which were more natural than binary.

+Much easier to read programs

+And much easier to find and fix mistakes

– Still not portable to different machines

– Still quite difficult to write, read, and debug programs

10

Page 11: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Next Major Advance: High Level Languages &

Compilers

To improve on assembly language:

Devise a set of statements called a high-level language that are closer to human language and methods of writing expressions

and

a program called a compiler to translate them into machine language.

11

Why not just use human language?It’s too complex and ambiguous; e.g.,

“Time flies like an arrow”

1950sFORTRAN

COBOLLISP

Page 12: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Compilers vs. AssemblersAn assembler translates one assembly-language statement into one machine-language statement.A compiler translates one high-level statement into multiple machine-language statements, so it is much more difficult to write a correct compiler than an assembler.

LOAD bADD cSTORE temp1LOAD aMULT temp1STORE z

Assembler

1000000000110101 11000100001101101000000101000001100000000011010011000110010000011000000100110111

z = a * (b + c); Compiler

12

Page 13: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Advantages of High-Level Languages

With programming in high-level languages (e.g., C++):

+Programs are much easier to read.

+Mistakes are much easier to find and fix.

+Programs are (or can be) portable from one machine to another (provided they conform to the language standard). Just need a compiler for that language written in the machine language of that machine.

Not so simple that just anyone can use them (otherwise this course wouldn't exist) 13

Page 14: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Objectives in Programming

A program to solve a problem should be:

+correct (it actually solves the problem)

+readable (understandable by another person)

+user-friendly (designed in a way that is easy for its user to use).

14

Grading criteria

Later

+efficient (doesn’t waste time or space)

Page 15: Introduction 1 (Read Chap. 1) What is Programming? For some given problem: design a solution for it -- identify, organize & store the problem's data --

Fredrick P. Brooks, Jr. (1931- )

We enjoy designing things because we are created in the image of God.

The woes of programming

:

Products become obsolete too

quickly. 15

The computer is a powerful and rewarding tool to use.

The joys of programming:

The “mindless” details can be

excessively tedious.