computer programming i. today’s lecture components of a computer program programming language ...

27
Computer Programming I

Upload: kerry-warren

Post on 30-Dec-2015

236 views

Category:

Documents


3 download

TRANSCRIPT

Computer Programming I

Today’s Lecture

Components of a computer

Program Programming language

Binary representation

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-3

Computer Components

HardwareCPU IO deviceMain memory

Software How does computer work?

Computer Components

The Central Processing Unit“brain” of the computer.

Memory

Random Access Memory (RAM)Temporary memoryMain memory

Read Only Memory (ROM)For start-up directionspermanent memory.

The execution speed of a program depends on

CPURAMWhy?

Programs

Computer Hardware Software

Programs that run on a computerOperating systemApplication software

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-9

Programming Languages

Three types of programming languages

1. Machine languages Strings of numbers giving machine

specific instructions Example:

+1300042774

+1400593419

+1200274027

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-10

Programming Languages

Three types of programming languages

2. Assembly languages English-like abbreviations representing

elementary computer operations (translated via assemblers)

Example:LOAD BASEPAY

ADD OVERPAY

STORE GROSSPAY

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-11

Programming Languages

Three types of programming languages

3. High-level languages Codes similar to everyday English Use mathematical notations (translated via

compilers) Example:

grossPay = basePay + overTimePay

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-12

Programming Languages (3)

Machine Languages

Assembly Languages

High-Level Languages

+1300042774+1400593419+1200274027

LOAD AADD B

STORE C

C=A+B

Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-13

C++

C++ is a third generation language Why C++ not C

C++ is an object oriented language

Decimal System

Positional base 10 numeral systems 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Use same symbol for different orders of

magnitude For example, “1262” in base 10

1*103+2*102+6*101+2*100

Binary

A computer is a “bistable” device A bistable device:

Easy to design and build Has 2 states: 0 and 1

One Binary digit (bit) represents 2 possible states (0, 1)

Bits

With 2 bits, 4 states are possible (22 = 4)

Bit1 Bit0 State

0 0 1

0 1 2

1 0 3

1 1 4

Decimal to Binary representation 0: 0 1: 1 2: 10

3: 11 4: 100

5: 101 6: 110 7: 111 8: 1000

9: 1001

10: 1010 11: 1011 12: 1100

13: 1101 14: 1110 15: 1111 16: 10000 17: 10001

Convert Binary to Decimal

18

Interpret binary numbers (transform to base 10) 1101

= 1*23+1*22+0*21+1*20=8+4+0+1=13 Translate the following binary number to

decimal number 101011

Convert Decimal to Binary

Procedure:

1. Divide the decimal number by 2

2. Make the remainder the next digit to the left of the answer

3. Replace the decimal number with the quotient

4. If quotient is not zero, Repeat 1-4; otherwise, done

Algorithm

A finite set of well-defined instructions for accomplishing some task which, given an initial state, will terminate in a corresponding recognizable end-state.

Examples: Select the largest number from a set of number (n)

Suppose n numbers are a1, a2, …an Set LG=a1; For i=2 to n, do

if LG<ai, then set LG=ai; Else do nothing;

The largest number is LG

Convert Decimal number 100 to Binary Number

100 % 2 = 0=> last digit100 / 2 = 5050 % 2 = 0 => 2nd last digit50/2 = 2525 % 2 = 1 => 3rd last digit25 / 2 = 1212 % 2 = 0 => 4th last digit

12 / 2 = 66 % 2 = 0 => 5th last digit6 / 2 = 3 3 % 2 = 1 => 6th last digit3 / 2 =1 1 % 2 = 1 => 7th last digit1 / 2 = 0

The result is 1100100

Convert Decimal Number 1492 to Binary Number

Bytes and Words A group of 8 bits is a byte A byte can represent 28 = 256 possible

states Several bytes grouped together to form a

word Word length of a computer, e.g., 32 bits

computer, 64 bits computer

Memory Size Metrics

1KB = 1024 bytes 1MB = 1024 x 1024 bytes 1GB = 1024 x 1024 x 1024 bytes

Representing Text Text is a series of characters

letters, punctuation marks, digits 0, 1, …9, spaces, return (change a line),…

How many bits do we need to represent a character? 1 bit can be used to represent 2 different things 2 bit … 2*2 = 22 different

things n bit 2n different things

In order to represent 128 different character Solve 2n = 128 for n n=7

ASCII The American Standard Code for Information

Interchange 128 characters 7 bits could be used to represent ASCII

characters However, in 1960s, an 8-bit byte was

becoming hardware standard, therefore, it was decided to use 1 byte (8 bits) to store the ASCII code (first 7 bits), with the eighth bit being used as a parity bit to detect transmission errors

ASCIITable