Vous êtes sur la page 1sur 13

8/22/2013

What is a Computer Program?


A description of algorithms and data structures to achieve a specific objective Algorithm?
A step by step description of how to achieve the objective Example: A recipe contains a description of an algorithm to achieve a cooking objective

What is a Computer Program?


A description of algorithms and data structures to achieve a specific objective Algorithm Data structure?
Details of the data that is going to be manipulated by the algorithm to achieve the objective Example: A recipe contains a list of the required ingredients to achieve the required cooking objective
2

What is a Computer Program?


A description of algorithms and data structures to achieve a specific objective Could be done in any language, even a natural language like English Programming language: A Standard notation for writing programs Examples: C, C++, Java An extreme example: Machine language

Example: Calculating Interest


English: To calculate the simple interest, multiply the principal amount by the rate of interest by the number of years C: W = X * Y * Z; Even better C statement: SimpIeInterest = Principal * Rate * Years;

8/22/2013

Calculating Interest ...


Machine language: (transformed into a human readable form)
movss movss mulss movss mulss movss x(%rip), %xmm1 y(%rip), %xmm0 %xmm0, %xmm1 z(%rip), %xmm0 %xmm1, %xmm0 %xmm0, w(%rip)

A Problem
It is difficult for us to write our programs in this primitive language It would be even more difficult to build a computer that can directly `understand' programs written in a language like C The solution: Translate the C program into an equivalent machine language program This can be done by a program called a compiler e.g., gcc
6

Observation: This is much more `primitive' than the English description `more primitive', `closer to how the machine works', `at a much lower level'

C Program example
Objective Print out all of the prime numbers < 20 Prime number: Divisible only by 1 and itself
1. Start with 2

C Program example ..
1. Start with 2 1. number = 2

2. Determine whether it is a prime number 3. If so, print it out 4. Go on to the next number

2. If number is a prime, print it out 3. Determine the next number to be checked 4. If it is less than 20, go back to step 2

2. Determine whether it is a prime number 3. If so, print it out 4. Go on to the next number

8/22/2013

C Program example ...


List of sub-objectives that have arisen How to determine if a number is prime How to print out a number Figure out how to handle each of these subobjectives Write the steps in the C language

C Program example ....


1. number = 2

2. If number is a prime, print it out 3. Determine the next number to be checked

void main(){ int i; for (i = 2; i < 20; i++) if (isPrime(i)) printf("%d\n", i);

4. If it is less than } 20, go back to step 2

10

C Program example ....


void main(){ int i; for (i = 2; i < 20; i++) if (isPrime(i)) printf("%d\n", i); } int isPrime

C Program example ....


A C program is made up of units called `functions' Every program contains a function called `main'
void main(){ int i; for (i = 2; i < 20; i++) if (isPrime(i)) printf("%d\n", i); } int isPrime /* TO BE WRITTEN */ printf()
11 12

Libraries of commonly used functions (like `printf') are available Other functions (like `isPrime') must be written by the programmer

8/22/2013

What does gcc do for you?


% gcc primes.c
primes.c gcc source: Program that you wrote in the C language and typed into the file primes.c executable: File generated by gcc. The a.out file contains an equivalent program in machine language that can be executed on a computer system 13 a.out

Basic Computer Organization The `brains


of the computer system Processor or CPU Memory Where things are remembered program, data

Bus

I/O

I/O

I/O

14

Program = Instructions+Data
We will next learn more about data

Different kinds of data


How does one piece of data differ from another? Constant vs Variable 1 (the number one) is a constant; it does not change in value So is 0.65 So is the character `a' In our primes program, i changed in value from 2 to 19 when the program ran It is a variable
16

15

8/22/2013

Different kinds of data


How does one piece of data differ from another? Constant vs Variable Basic vs Structured
The variable i has only one value associated with it at a time There can be a piece of data which has multiple values associated with it at a time Think about the complex value 6 + 5j or the vector 3 + 17 or an array of values
17

Different kinds of data


How does one piece of data differ from another? Constant vs Variable Basic vs Structured Of different types
Data like 2 and 17 are of the same type You can multiply, divide, add, subtract data like 2 and 17 Data like 2 and 'a' are not of the same type Can you divide 'a' by 2? Are 3 and 2.5 of the same type?
18

Different kinds of data


How does one piece of data differ from another? Constant vs Variable Basic vs Structured Of different types
C programming language supports types like character (char) real (float) integer (int) unsigned int as well as signed int
19

Digital Computers..
Inside current digital computers, values vary between 2 discrete values 0 and 1 high and low 0 Volts and 3 Volts

20

8/22/2013

Digital Circuits
Electrical circuits in which voltages only take on a discrete number of values There are digital circuits that can do calculations, others that can be used to remember values, etc

Example: Binary Addition


Binary: Base 2 number system You can generalize the decimal number system, which uses 10 digits (0,1,,9) to work with any radix or base
( d n 1 d n 2 .. . d 2 d 1 d 0 )ten =d n 1 10 n 1 +d n 2 10 n 2 +. .. +d 2 10 2 +d 1 101 +d 0 10 0
i=n 1

i= 0

d i 10i

Example: Decimal number 7613 = 7 x 103 + 6 x 102 + 1 x 101 + 3 x 100

21

Example: Binary Addition


Binary: Base 2 number system You can generalize the decimal number system, which uses 10 digits (0,1,,9) to work with any radix or base
( d n 1 d n 2 .. . d 2 d 1 d 0 )ten =d n 1 10 n 1 +d n 2 10 n 2 +. .. +d 2 10 2 +d 1 101 +d 0 10 0
i=n 1

Binary Addition .. 1 bit adder


A circuit that can add one bit to another bit The different cases
0 +0 0 +1 1 +0
2 1 0

=0 =1 =1 ???

=0 0 =0 1 =0 1 =1 0
a b
1 bit adder

i= 0

d i 10i

s c

Binary digits (called bits): 0 and 1


( b n 1 b n 2 .. . b 2 b1 b 0 )two =bn 1 2
i=n 1

n 1

+b n 2 2

n 2

+ .. . +b 2 2 +b1 2 +b0 2

1 +1

i= 0

b i 2i

Example: Decimal 47 is Binary 101111

One bit adder has 2 inputs and 2 outputs

8/22/2013

Binary Addition .. 2 bit adder


A circuit that can add one 2 bit value (a1a0) to another 2 bit value (b1b0)
a1 a0 + b1 b0 s2 s1 s0 s1 s2
Full 1 bit adder

How is Data Represented?


On a digital computer Binary
Base 2 number system Two values: 0 and 1 Bit (Notation: b); Byte (Notation: B) 8 bits Other notation: K, M, G, T, P etc
K: 210 , M: 220 , G: 230, etc 1G = 1,073,741,824 2 GB of RAM, 1 TB hard disk drive

a1 b1

s0 c

1 bit adder

a0 b0

This idea can be extended to design a 32 bit adder or a 64 bit adder

26

Character Data
Typically represented using the ASCII code ASCII: American Standard Code for Information Interchange Each character is represented by a unique 8 bit ASCII code word Example: a is represented by 01100001, 1 is represented by 00110001

How is Data Represented?


Character data: ASCII code Integer data
In computer systems, you usually find support for both signed integers and unsigned integers
e.g., C programming int x; +ve or -ve whole number values unsigned int y; +ve whole number values

27

28

8/22/2013

Unsigned Integer Data


Representation: Binary number system
( b n 1 b n 2 ... b 2 b1 b 0 )two =b n 1 2 n 1 +b n 2 2 n 2 + ... +b 2 2 2 +b1 21 +b0 2 0
i=n 1

Aside: <<

C << operator
e.g., y = x << 3;

Left Shift by 3 bits


Shifts each bit bi to the left by 3 bits The 3 bits on the extreme left go away 3 Zeros come in at the right extreme end

i= 0

b i 2i

e.g., Decimal 1000 is represented as 1111101000


i.e. 0000001111101000 in 16 bits 00000000000000000000001111101000 in 32 bits

e.g., in 8 bits, 11001101 << 3 is 01101000 Claim: Shifting an unsigned int left by one bit is the same as multiplication by 2
Qualification: If the product can be computed i.e., all of the bits that go away are Zero

29

30

Aside: << ..
Proof: Consider n bit value I = ( b n 1 b n 2 ... b 2 b1 b 0 )two Shifted left by 1 bit and bn-1 = 0 we get
( b n 2 b n 3 ... b1 b 0 0 )two
i=n 2

Signed Integer Data


2s Complement Representation The n bit quantity least significant bit

x n 1 x n 2 ... x 0
represents the signed integer value
n 2

bi i= 0 i=n 2

i+ 1

x n 1 2 n 1 + x i 2 i
i= 0

=2

i= 0

bi 2 i

Example: In 8 bits

-128 + 64 + 32 + 16 + 2 + 1

= 2I

13 is represented as 00001101 -13 is represented as 11110011


32

8/22/2013

Real data
Real numbers: points on the infinitely long real number line
There are an infinitely many points between any two points on the real number line

Floating Point Representation


IEEE Floating Point Standard (IEEE 754) 32 bit value with 3 components ( s, e, f )
1. s (1 bit sign) 2. e (8 bit exponent) 3. f (23 bit fraction)

represents the value

( 1 )s 1. f 2e 127

33

34

Example: IEEE Single Float


Consider the decimal value 0.5 Equal to 0.1 in binary

Example: Zero
Consider the decimal value 0.0 What are the corresponding s, e, f?

1.0 2 1 ( 1 ) 1. f 2e 127
s

( 1 )s 1. f 2e 127
It is not possible to represent zero Zero is handled as a special case There are other special cases

s: 0, e: 126, f: 000000 In 32 bits, 0 01111110 00000000000000000000000

35

36

8/22/2013

Basic Computer Organization


Main parts of a computer system:
Processor: Executes programs Main memory: Holds program and data I/O devices: For communication with outside

Basic Computer Organization

Processor or CPU Memory

Machine instruction: Description of primitive operation that machine hardware is able to execute e.g. ADD these two integers Instruction Set: Complete specification of all the kinds of instructions that the processor hardware was built to execute
37

Bus

I/O

I/O

I/O

38

Aside: About Memory


What is memory?
Devices that can remember things

Main Memory.
Holds instructions and data View it as a sequence of fixed sized locations, each referred to by a unique memory address In many computers, the size of each memory location is 1 Byte

8 bits

There are different kinds of memory in a computer system


Some remember by the state an electrical circuit is in e.g., SRAM Others remember by the amount of electrical charge stored in a capacitor e.g., DRAM Memory Yet others remember by magnetic or optical properties e.g., Hard disk drive/Mag Tape, CD/DVD

0 01100101 1 2 3

They can vary substantially in their speed and capacity


39

1,073,741,823 (if memory is of size 1GB) 40

10

8/22/2013

Main Memory..
Values that occupy more than 8 bits would occupy more than one, neighbouring memory locations e.g., 32 bit signed integer 01100101111111110000000011100111 would occupy 4 neighbouring memory locations, maybe as shown

8 bits

0 1 2 3
01100101 00000000 11100111

Data Structures: Arrays


char A[4] char B[4][2]
columns 0 1 0 B[0][0] B[0][1]

rows

11111111

5 6 7 8

A[0] A[1] A[2] A[3]

1 B[1][0] B[1][1] 2 B[2][0] B[2][1] 3 B[3][0] B[3][1]

1,073,741,823

11 12 13 14 15 16 17 18

B[0][0] B[0][1] B[1][0] B[1][1] B[2][0] B[2][1] B[3][0] B[3][1]

Data Structures: Linked Lists


struct node { char data; / a character: 8 bits in size

Data differing in lifetimes


100 300

struct node *next; / a pointer: 32 bits in size } 300 < 700

Lifetime: Interval between time of creation and end of existence How long can the lifetime of a datum be? We will consider 3 possible lifetimes

struct node *head; / a pointer: 32 bits in size / in memory, say at address 100

700

>

44

11

8/22/2013

Data: Different Lifetimes.


1. Lifetime = Execution time of program
Initialized/uninitialized data Must be indicated in executable file The space in memory for all of this data can be assigned when program execution starts (Static Allocation)

Data: Different Lifetimes.


1. Lifetime = Execution time of program 2. Lifetime = Time between explicit creation of data & explicit deletion of data
Dynamic memory allocation In C you create new data using a function like malloc() The memory space for this data is managed dynamically when the malloc/free is executed (Heap allocation)

45

46

Data: Different Lifetimes..


1. Lifetime = Execution time of program 2. Lifetime = Time between explicit creation of data & explicit deletion of data 3. Lifetime = During execution of a function (i.e., time between function call and return)
Local variables, parameters of the function The memory space for this data is assigned when the function is called and reclaimed on return from the function (Stack allocation) Stack: Like a pile of books on a table
47

Stack: Function Local Variables


When the program starts executing

Local Variables of main

Top of Stack Pointer

What if main() then calls function func1()?


48

12

8/22/2013

Stack allocated: Function Local Variables.


While executing in function func1()

Stack: Function Local Variables..


Executing in main() once again

Top of Stack Pointer Local variables of func1


Local Variables of main Local Variables of main

Local Variables of main

Top of Stack Pointer

What happens on return from the call to func1()?


49 50

Recursion vs Iteration
Example: Compute N! , N > 0
{ int N, fact, j; int factorial (int j) { if (j == 0) return(1); for (fact=1, j=1; j <= N; j++) fact = fact * j; } j fact N 100 J=0 J=1

During program execution


Code (machine language program) Data (initialized and uninitialized) Code and Data dont change in size while the program is executing Heap (for dynamically allocated data) Stack (for function local variables) Heap and Stack change in size as program executes Code Initialized Uninitialized Heap

return (j * factorial(j-1)); } void main() { int N;

factorial(N);

Stack

N 100

J=98 } J=99 J=100

52

13

Vous aimerez peut-être aussi