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 Hardware CPU IO device Main memory Software How does computer work?
Computer Components
The Central Processing Unit “brain” of the computer.
Memory Random Access Memory (RAM) Temporary memory Main memory Read Only Memory (ROM) For start-up directions permanent memory.
The execution speed of a program depends on CPU RAM Why?
Programs Computer Hardware Software Programs that run on a computer Operating system Application 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:
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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 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 Programming Languages (3) Machine Languages Assembly Languages High-Level Languages LOAD A ADD B STORE C C=A+B
Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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* * * *10 0
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 (2 2 = 4) Bit 1 Bit 0 State
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: 17: 10001
Convert Binary to Decimal 18 Interpret binary numbers (transform to base 10) 1101 = 1*2 3 +1*2 2 +0*2 1 +1*2 0 = =13 Translate the following binary number to decimal number
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 digit 100 / 2 = % 2 = 0=> 2 nd last digit 50/2 = % 2 = 1=> 3rd last digit 25 / 2 = % 2 = 0=> 4th last digit 12 / 2 = 6 6 % 2 = 0 => 5 th last digit 6 / 2 = 3 3 % 2 = 1 => 6 th last digit 3 / 2 =1 1 % 2 = 1 => 7 th last digit 1 / 2 = 0 The result is
Convert Decimal Number 1492 to Binary Number
Bytes and Words A group of 8 bits is a byte A byte can represent 2 8 = 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 = 2 2 different things n bit 2 n different things In order to represent 128 different character Solve 2 n = 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
ASCII Table