Top Banner
Computer Programming I
27

Computer Programming I. Today’s Lecture Components of a computer Program Programming language Binary representation.

Dec 30, 2015

Download

Documents

Kerry Warren
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

Computer Programming I

Page 2: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

Today’s Lecture

Components of a computer

Program Programming language

Binary representation

Page 3: 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?

Page 4: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

Computer Components

Page 5: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

The Central Processing Unit“brain” of the computer.

Page 6: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

Memory

Random Access Memory (RAM)Temporary memoryMain memory

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

Page 7: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

The execution speed of a program depends on

CPURAMWhy?

Page 8: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

Programs

Computer Hardware Software

Programs that run on a computerOperating systemApplication software

Page 9: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 10: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 11: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 12: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 13: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 14: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 15: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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)

Page 16: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 17: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 18: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 19: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 20: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 21: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 22: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

Convert Decimal Number 1492 to Binary Number

Page 23: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 24: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

Memory Size Metrics

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

Page 25: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 26: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

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

Page 27: Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.

ASCIITable