Académique Documents
Professionnel Documents
Culture Documents
8/22/2013
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
void main(){ int i; for (i = 2; i < 20; i++) if (isPrime(i)) printf("%d\n", i);
10
Libraries of commonly used functions (like `printf') are available Other functions (like `isPrime') must be written by the programmer
8/22/2013
Bus
I/O
I/O
I/O
14
Program = Instructions+Data
We will next learn more about data
15
8/22/2013
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
i= 0
d i 10i
21
=0 =1 =1 ???
=0 0 =0 1 =0 1 =1 0
a b
1 bit adder
i= 0
d i 10i
s c
n 1
+b n 2 2
n 2
+ .. . +b 2 2 +b1 2 +b0 2
1 +1
i= 0
b i 2i
8/22/2013
a1 b1
s0 c
1 bit adder
a0 b0
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
27
28
8/22/2013
Aside: <<
C << operator
e.g., y = x << 3;
i= 0
b i 2i
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
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
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
( 1 )s 1. f 2e 127
33
34
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
35
36
8/22/2013
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
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
0 01100101 1 2 3
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
rows
11111111
5 6 7 8
1,073,741,823
11 12 13 14 15 16 17 18
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
45
46
12
8/22/2013
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
factorial(N);
Stack
N 100
52
13