CS170: Game Design Studio 1 UC Santa Cruz School of Engineering [email protected] 31 October 2008.
Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary...
Transcript of Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary...
![Page 1: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/1.jpg)
Lecture 2
![Page 2: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/2.jpg)
Outline
• Binary number system – Concept of Memory cell/devices – Bits and bytes – Combing adjacent bytes
• Computer instructions – Structure and operation of the CPU
– Program flow
– CPU instructions
• Introduction to algorithms – GCD algorithm
• Programming a computer – High level languages / compilers
![Page 3: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/3.jpg)
The switch is a memory device
• The electrical switch is a memory device:
• The electrical switch can be in one of these 2 states:
• off (we will call this state 0)
• on (we will call this state 1)
![Page 4: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/4.jpg)
Memory cell used by a computer
• One switch can be in one of 2 states
• A row of n switches:
can be in one of 2n states !
![Page 5: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/5.jpg)
The binary number system
• The binary number system uses 2 digits to encode a number:
• That means that you can only use the digits 0 and 1 to write a binary number
– Example: some binary numbers
• 0 = represents no value
• 1 = represents a unit value
• 0
• 1
• 10
• 11
• 1010
• and so on.
![Page 6: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/6.jpg)
The binary number system (cont.)
Example:
Binary number Value encoded by the binary number
0 0×20 = 0
1 1×20 = 1
10 1×21 + 0 ×20 = 2
11 1×21 + 1 ×2
0 = 3
1010 1×23 + 0×22 + 1×21 + 0×20 = 8 + 2 = 10
![Page 7: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/7.jpg)
What does all this have to do with a computer ? (cont.)
• The connection between the computer memory and the binary number system is:
• The computer system uses the binary number encoding to store the number
Example:
![Page 8: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/8.jpg)
CMOS Switch
![Page 9: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/9.jpg)
Computer memory (cont.)
Details
• In order to store text information in a computer, we need to encode:
• 26 upper case letters ('A', 'B', and so on)
• 26 lower case letters ('a', 'b', and so on)
• 10 digits ('0', '1', and so on)
• 20 or so special characters ('&', '%', '$', and so on)
for a total of about 100 different symbols
• The nearest even power 2n that is larger than 100 is:
• 27 = 128 ≥ 100
• For a reason beyond the scope of this course, an 8th switches is added
![Page 10: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/10.jpg)
Combining adjacent memory cells (cont.)
• Each pattern can are encoded exactly one number:
Therefore, one byte can store one of 256 possible values
(You can store the number 34 into a byte, but you cannot store the number 456, the value is out of range)
• 00000000 = 0
• 00000001 = 1
• 00000010 = 2
• 00000011 = 3
• ...
• 11111111 = 255
![Page 11: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/11.jpg)
Combining adjacent memory cells (cont.)
• The computer can combine adjacent bytes (memory cells) and use it as a larger memory cell
Schematically:
A 16 bits memory cell can store one of 216 = 65536 different patterns.
Therefore, it can represent (larger) numbers ranging from: 0 − 65535.
![Page 12: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/12.jpg)
Combining adjacent memory cells (cont.)
• Example: how a computer can use 2 consecutive bytes as a 16 bits memory cell:
• The bytes at address 0 and address 1 can be interpreted as a 16 bits memory cell (with address 0)
![Page 13: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/13.jpg)
CPU instructions
![Page 14: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/14.jpg)
Structure of a CPU
![Page 15: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/15.jpg)
Components of a CPU
• General purpose registers:
• Arithmetic and Logic Unit (ALU):
– Register is a synonym for memory in computer science
– A general purpose register is a memory cell
– Each general purpose register has a unique name
– It is used to store (and recall) intermediate result of complex computations
– Is a complex electrical circuit that can perform Mathematical (+, -, ×, /) and logical operations (<, ≤ >, ≥, and, or)
– The output (result) of the computation (obtained by the ALU) is often stored in a general purpose register
![Page 16: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/16.jpg)
Structure of a CPU (cont.)
• Instruction register (IR):
• Program counter (PC):
• Contains the current instruction being executed by the CPU
• The CPU will perform the operation indicated by the instruction code contained in the instruction register
• The program counter is a register (memory cell) !
• This register contains the address (location in memory) of the next instruction after the CPU finishes executing the current instruction in the instruction register
• The value in the program counter will be increased after the CPU finishes executing one instruction
![Page 17: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/17.jpg)
Structure of a CPU (cont.)
• Processor status register (PSR):
• This register contains the various information about the CPU
• Among the information contained in the PSR is:
• When the CPU compares 2 numbers a and b, the outcome of the comparison is stored in the PSR The outcome of a compare operation will allow the CPU to determine the following fact between a and b:
• The result of a comparison operation
• equal
• not equal
• less than
• less than or equal
• greater than
• greater than or equal
![Page 18: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/18.jpg)
Operation of a CPU (cont.)
• The following figures illustrates the CPU's execution cycle (that goes on indefinitely):
The CPU will cause the memory to retrieve (recall) the value stored at memory address (location) 1
1. The CPU first sends a request to retrieve (recall) the data stored at memory location given by the program counter (PC) (in the figure, the value of the PC = 1):
![Page 19: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/19.jpg)
Operation of a CPU (cont.)
• In response, the RAM memory sends the value stored in memory location 1 (= the number 4) to the CPU which stores it in the instruction register (IR):
2. In response, the RAM memory sends the value stored in memory location 1 (= the number 4) to the CPU which stores it in the instruction register (IR):
This number (4) will be interpreted as a code for a computer instruction.
The old instruction code (0) will be over-written by the new instruction code
![Page 20: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/20.jpg)
3. The program counter is then increased:
Notice that the CPU is now ready to fetch the next instruction when the current instruction (stored in the instruction register) is processed.
Operation of a CPU (cont.)
![Page 21: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/21.jpg)
Operation of a CPU (cont.)
4. The CPU will now execute the instruction in the program counter (instruction code = 4)
When the CPU is finished, it will repeat these steps again (and again, until you turn the computer off)...
![Page 22: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/22.jpg)
• In Computer Science, a pointer always points to a location in memory
Examples:
Pointers: pointing to a location in memory (cont.)
Pointer to memory location 1 Pointer to memory location 4
![Page 23: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/23.jpg)
Pointers: pointing to a location in memory (cont.)
• The computer does not have "pointing finger" that point to some location in memory
• The pointer is represented in the computer by the value of the address (location) of the memory where the pointer is pointing at
![Page 24: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/24.jpg)
Pointers: pointing to a location in memory (cont.)
• Examples:
Pointer to memory location 1 Pointer to memory location 4
• Storing the value 1 in some register/memory cell will remember the memory location 1
• The value 1 in the register/memory cell is effectively pointing to the memory location 1
• Storing the value 4 in some register/memory cell will remember the memory location 4
• The value 4 in the register/memory cell is effectively pointing to the memory location 4
![Page 25: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/25.jpg)
Example of a pointer: the program counter (PC)
• The program counter (PC) is in fact a pointer Example
When the program counter (PC) contains the value 1:
the program counter (PC) is in fact pointing to the memory location 1:
![Page 26: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/26.jpg)
Program flow
• Definition: program flow
• Default program flow:
• Program flow = the sequence of instructions from the program executed by the CPU
• After executing a non-branching (or non-jumping) instruction at memory location n, then next instruction that is executed is the instruction at memory location n+1
![Page 27: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/27.jpg)
Program flow (cont.)
• Example: • All of the instructions in the figure are non-branching
• Therefore, the sequence of instructions executed by the CPU starting at memory location 0 is:
1. 0 ("add")
2. 4 ("compare")
3. 1 ("subtract")
4. And so on (in sequence)
![Page 28: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/28.jpg)
Branching: changing the program flow
• Branch instruction:
• A branch instruction can alter (change) the default program flow
• When the CPU executes a branch x instruction, the next instruction that will be executed by the CPU is the instruction at memory location x
![Page 29: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/29.jpg)
Branching: changing the program flow (cont.)
• Example:
• Notice there is a branch to location 4 instruction in the example.
• Therefore, the sequence of instructions executed by the CPU starting at memory location 0 is:
1. 0 ("add")
2. 64 ("branch to location 4")
3. 56 ("negate" --- this is the instruction at memory location 4)
4. 0 ("add")
5. And so on (in sequence again)
![Page 30: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/30.jpg)
The types of instructions that a computer can execute
• The computer is a computing (reckoning) machine
• All existing computers (actually the CPU) execute the following 3 types of instructions:
Arithmetic and logic operations
The result of an arithmetic and logic operation is often stored in a general purpose register
• +
• −
• ×
• /
• AND
• OR
• NOT
![Page 31: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/31.jpg)
The types of instructions that a computer can execute (cont.)
• Memory transfer operations
• Transfer the content from some specific memory location to a specific register (memory cell) in the CPU.
• and vice versa (transfer the content from some specific register (memory cell) in the CPU to some specific memory location).
![Page 32: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/32.jpg)
The types of instructions that a computer can execute (cont.)
• Branch operations
• A branch instruction will cause the CPU to branch (jump) to the specified location in memory
• After the jump has occurred, the CPU will continue to execute the instructions in sequence, until another branch/jump instruction is encountered
• There are 2 kinds of branch operations:
1. A unconditional branch instruction will always cause the
CPU to jump to the target location
2. A conditional branch instruction will only cause the CPU
to jump to the target location when the specified
condition is met
![Page 33: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/33.jpg)
Computer Algorithms
![Page 34: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/34.jpg)
Fact: computers are dumb machines
• Basic property of a computer (a machine):
• Computers do what we tell them to do • Unfortunately, computer do not necessarily do what we want them to do.... (Because we can make mistake in telling the computer what we want to do... These mistakes are called "bugs")
![Page 35: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/35.jpg)
Algorithm
• Definition: algorithm
Dictionary definition:
• Algorithm = a step-by-step procedure for solving a problem or accomplishing some task, especially by means of a computer
![Page 36: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/36.jpg)
Computer Algorithms
• Computer Algorithm:
is an algorithm that can be executed by a computer
![Page 37: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/37.jpg)
Computer Algorithms (cont.)
• Properties of computer algorithms:
• The steps in an algorithm must be consists of operations that can be executed by a computer
• The step in an algorithm must be unambiguous
(Remember that a dumb machine like a computer will do what it is told to do.
Resolving ambiguity requires some thinking (intelligence) which computers cannot do !)
![Page 38: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/38.jpg)
A real life algorithm
• Here is a famous algorithm that you can try out:
• Take 2 pieces of paper and write on one piece "A" and on the other piece "B":
• Write a positive integer on each piece of paper.
![Page 39: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/39.jpg)
• Euclid's Algorithm:
A real life algorithm (cont.)
As long as one of the number is not zero (0) do
{
if ( number on A ≥ number on B )
replace the number on A by the value (A - B)
otherwise
replace the number on B by the value (B - A)
}
The Greatest Common Divisor (GCD) of the numbers A
and B is the non-zero number on one of the papers
![Page 40: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/40.jpg)
• Example of the Euclid Algorithm:
A real life algorithm (cont.)
• We start with the following 2 number:
• Since number on A < number on B, we replace the
number on B (= 36) by the value (B − A) = 8:
![Page 41: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/41.jpg)
A real life algorithm (cont.)
• Now number on A ≥ number on B, so we replace the number on A
(= 28) by the value (A − B) = 20:
• Again, number on A ≥ number on B, so we replace the number on
A (= 20) by the value (A − B) = 12:
![Page 42: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/42.jpg)
A real life algorithm (cont.)
• Yet again, number on A ≥ number on B, so we replace the number
on A (= 12) by the value (A − B) = 4:
• This time, number on B > number on A, so we replace the number
on B (= 8) by the value (B − A) = 4:
![Page 43: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/43.jpg)
A real life algorithm (cont.)
• Now, number on A ≥ number on B, so we replace the number on A
(= 4) by the value (B − A) = 0:
• One of the number on the papers is now zero
According to the Euclid's Algorithm, the other number (= 4) is equal
to the GCD of (28, 36)
![Page 44: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/44.jpg)
Programming a computer
![Page 45: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/45.jpg)
What does programming a computer mean ?
• Programming a computer:
• Since a computer can only execute machine instructions (encoded in binary numbers), this means:
• Programming a computer = instruct a computer to perform a task/solve a problem
• Write the steps of an algorithm using machine instructions !!!
![Page 46: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/46.jpg)
High level programming language
• A high level programming language consists of (English-like) "people" language to simplify the writing computer algorithms
• A high level programming language allows the programmer to write sentences in this language which can be easily translated into machine instructions
• The sentences written in a high level programming language are called:
• statements
![Page 47: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/47.jpg)
A taste of computer algorithm
• Recall the Euclid's Algorithm: (to find the GCD)
As long as one of the number is not zero (0) do
{
if ( number on A ≥ number on B )
replace the number on A by the value (A - B)
otherwise
replace the number on B by the value (B - A)
}
The Greatest Common Divisor (GCD) of the numbers A
and B is the non-zero number on one of the papers
![Page 48: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/48.jpg)
A taste of computer algorithm (cont.) • Here is the Euclid Algorithm written in the Java programming language:
public class Euclid
{
public static void main (String args[])
{
int A; // Memory cell named "A"
int B; // Memory cell named "B"
// These memory cells are like the 2 pieces of paper
// we used above. They can store and recall a value
A = 28; // Write "28" on the piece of paper named "A"
B = 36; // Write "36" on the piece of paper named "B"
System.out.println("A = " + A + " B = " + B); // Print A and B
while ( A != 0 && B != 0 )
{
if ( A >= B )
A = A - B; // Replace the number on A by (A-B)
else B = B - A; // Replace the number on B by (B-A)
System.out.println("A = " + A + " B = " + B); // Print new A and B
}
System.out.println("GCD = " + B);
}
}
![Page 49: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/49.jpg)
A taste of computer algorithm (cont.)
• Example Program: (Demo previous code)
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/01/Progs/Euclid.java
How to use this program:
• Right click on the link and save in a scratch directory
• Compile with: javac Euclid.java
• Run with: java Euclid
![Page 50: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/50.jpg)
Computer Programs
• Computer Program:
• Computer program = a computer algorithm that is expressed (written) in some programming language
• Most programming languages are “English-like”
They use words from the English language
![Page 51: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/51.jpg)
The compilation (= translation) process (cont.)
• Before the computer can execute the instructions in a C program, we must first compile (translate) it into machine operations using a C translator (compiler):
• cc is the command to run the C compiler (translator)
• The second word prog.c is the name of the file that contains the C program (that is being translated)
• The machine operations that result from the translation process is stored in the file:
• cc prog.c
a.out
![Page 52: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/52.jpg)
The compilation (= translation) process (cont.)
• Now we can execute the program in machine instruction by using the command:
• The following figure summarizes the translation process:
>> a.out
![Page 53: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/53.jpg)
The compilation (= translation) process (cont.)
• Note:
• A C compiler is a program that is executed by a computer
• Therefore, a C compiler consists of machine instructions!!
(Computers only execute machine instructions !)
![Page 54: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/54.jpg)
Writing computer programs: trust your compiler ! (cont.)
• Therefore:
• The high level programming language used in this course is:
• We will not dwell on machine code any longer in this course
• We will learn to write computer programs in a high level programming language
![Page 55: Lecture 2 - Emory Universitydlagun/cs170/lectures/lecture-2.pdf · Lecture 2 . Outline •Binary number system –Concept of Memory cell/devices –Bits and bytes ... • Program](https://reader033.fdocuments.us/reader033/viewer/2022060315/5f0be7627e708231d432ca03/html5/thumbnails/55.jpg)
Outline
• Binary number system – Concept of Memory cell/devices – Bits and bytes – Combing adjacent bytes
• Computer instructions – Structure and operation of the CPU
– Program flow
– CPU instructions
• Introduction to algorithms – GCD algorithm
• Programming a computer – High level languages / compilers