introduction to computer science 1 with examples in visual basic, c, c++, and java 1 mata-toledo,...

58
Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Upload: aleesha-dorsey

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Introduction to Computer Science1

With Examples in Visual Basic, C, C++, and Java

1Mata-Toledo, Ramon A. & Cushman, Pauline K.McGraw-Hill, Copyright@2000

Page 2: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Computers

• Computer: a device which, under the direction of a program, can process data, alter its own program instructions, and perform computations and logical operations without human intervention– Two different levels: architecture and its

implementation• Architecture: user-visible interface as seen by the programmer

• Implementation: Construction of that interface using specific hardware and software components

Page 3: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Program

• Refers to a specific set of instructions give to the computer to accomplish a specific task

Page 4: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Computer Structures

1. High-speed memory unit

2. Central Processing Unit (CPU)

3. Peripheral (I/O Subsystem)

Page 5: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Basic Computer Structure

I/O Bus

KeyboardControl Unit

Status Register

PrimaryMemory

Input & Output bus Program Counter

Arithmetic Logic Unit

General RegistersR0

R1

Page 6: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Memory Unit

• Main or physical memory – where all the instructions and data that the CPU can directly access and execute– Known as RAM (Random Access Memory)

• Divided into logical units of the same size– Called a byte; 8 consecutive bits or binary digits

• Can be magnetized to one of two states (on or off) 1or 0• Each byte is associated with a unique address

– Which can increase right to left or left to right

Page 7: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Address Space

• Set of all unique addresses that a program can reference– The address of a byte is fixed whereas its content will

vary

– Number of bits used to represent the address determines the size of the address space

• Calculated as 2N where N is the number of bits used to represent the address

– The size of the memory of a computer is measured in bytes

Page 8: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Memory Units

• 1 nibble• 1 byte• 1 word• 1 long word• 1 quad word• 1 octa-word

• 8 consecutive bits• 4 consecutive bits• 2 consecutive bytes• 4 consecutive bytes• 8 consecutive bytes• 16 consecutive bytes

Page 9: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Larger Units of Memory

• 1 Kilobyte• 1 Megabyte• 1 Gigabyte• 1 Terabyte• 1 Petabyte• 1 Exabyte

• 1024 bytes• ~106 bytes• ~109 bytes• ~1012 bytes• ~1015 bytes• ~1018 bytes

32 Mb = 32*103 Kb = 32 * 103 *1024 bytes = 32,768,000 bytes

Page 10: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Central Processing Unit• Brain of the computer

– Fetching instructions from memory and executing them

• Divided into – Arithmetic Logic Unit (ALU)

• Additions, subtractions, comparisons

– Control Unit (CU)• Manages movement of data within the processor

• Contains general registers that provide local high-speed storage for the processor

• Contains status registers that provide information about the state of the processor, the instruction being processed and any special circumstances

Page 11: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Sample Processing Sequence• Two numbers in main memory are added

1. Instruction is transferred from memory into the CPU2. Location of the instruction being processed is updated in the

instruction counter (IC) or program counter (PC)3. The instruction just fetched is stored in the instruction

register (IR) 4. CU decodes the instruction to add two numbers

• operator [operand1], [operand2], [operand3]• ADDW3 first_no, second_no, answer

• W = words, 3 = no of operands in the instruction

5. Numbers are located in main memory6. Fetched into internal registers of the ALU by the CU7. Addition is carried out by ALU8. Sum stored in new memory location by CU9. The IC is updated to point to the next instruction

Page 12: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Communication

• During the process of described, two additional registers facilitate the communication between the CPU and main memory– Memory address register (MAR)

• Holds the address to or from which the data is being transferred

– Memory data register (MDR)• Contains the data to be written into or read out of the address

location

Page 13: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

I/O• Most common input devices

– Keyboard– Mouse

• Most common output devices– Monitor– Printer

• Dual purpose– Hard drives– Tapes– Jazz or Zip Drives– Floppies

Page 14: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Bus Structure

• BUS: A collection of wires to transfer data within different components of a computer

– Generally 3 such busses1. Address bus

2. Data Bus• Must have as many wires as ther are bits of data in the

memory unit of the computer

3. Control bus

Page 15: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Destructive Operations

• If a location isn’t specified for a result, it is often stored in the second operand– A destructive operation

• If a location is specified for a result, it is used– A non-destructive operation

Page 16: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Representing Data

• The computer knows the type of data stored in a particular location from the context in which the data are being used; i.e. individual bytes, a word, a longword, etc– 01100011 01100101 01000100 01000000

• Bytes: 64(10, 68 (10, 101 (10, 99(10

• Two byte words: 17,472 (10 and 24,445 (10

• Longword: 1,667,580,992 (10

Page 17: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Number Systems and Codes

• We use the DECIMAL (10 system• Computes use BINARY (2 or some shorthand for it like

OCTAL (8 or HEXADECIMAL (16

• Given any positive integer basis or (RADIX) N, there are N different individual symbols that can be used to write numbers in the system. The value of these symbols range from 0 to N-1

• All systems we use in computing are positional systems– 495 = 400 + 90 +5

Page 18: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

ConversionsDecimal Binary Octal Hex

1 0001 1 12 0010 2 23 0011 3 34 0100 4 45 0101 5 56 0110 6 67 0111 7 78 1000 10 89 1001 11 910 1010 12 A11 1011 13 B12 1100 14 C13 1101 15 D14 1110 16 E15 1111 17 F

Page 19: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Decimal Equivalents

• Assuming the bits are unsigned, the decimal value represented by the bits of a byte can be calculated as follows:

1. Number the bits beginning on the right using superscripts beginning with 0 and increasing as you move left. Remember, 20, by definition is 1

2. Use each superscript as an exponent of a power of 23. Multiply the value of each bit by its corresponding

power of 24. Add the products obtained

Page 20: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Horner’s Method

• Another procedure to calculate the decimal equivalent of a binary number– This method works with any base

• Horner’s Method:– Step 1: Start with the first digit on the left

– Step 2: Multiply it by the base

– Step 3: Add the next digit

– Step 4: Multiply the sum by the base

– Step 5: Continue the process until you add the last digit

Page 21: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Hex to Binary

• Step 1: Form four-bit groups beginning from the rightmost bit of the binary number– If the last group (at the leftmost position) has less than

four bits, add extra zeros to the left of the group to make it a four-bit group

• 0110011110101010100111 becomes• 0001 1001 1110 1010 1010 0111

• Step 2: Replace each four-bit group by its hexadecimal equivalent– 19EAA7(16

Page 22: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Converting Decimal to Other Bases

• Step 1: Divide the number by the base you are converting to (r)

• Step 2: Successively divide the quotients by (r) until a zero quotient is obtained

• Step 3: The decimal equivalent is obtained by writing the remainders of the successive division in the opposite order in which they were obtained– Know as modulus arithmetic

• Step 4: Verify the result by multiplying it out

Page 23: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Addition & Subtraction Terms

• A + B – A is the augend– B is the addend

• C – D– C is the minuend– D is the subtrahend

Page 24: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Addition Rules

Addition• Step 1: Add a column of numbers• Step 2: Determine if there is a single

symbol for the result• Step 3: If so, write it and go to the next

column. If not, write the accompanying number and carry the appropriate value to the next column

Page 25: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Subtraction Rules

• Step1: Start with the rightmost column, if the column of the minuend is greater than that of the subtrahend, do the subtraction, if not…

• Step 2: Borrow one unit from the digit to the left of the once being processed– The borrowed unit is equal to “borrowing” the radix

• Step 4: Decrease the column form which you borrowed by one

• Step 3: Subtract the subtrahend from the minuend and go to the next column

Page 26: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Addition of Binary Numbers

• Rules for adding or subtracting very similar to the ones in decimal system– Limited to only two digits

• 0 + 0 = 0

• 0 + 1 = 1

• 1 + 0 = 1

• 1 + 1 = 0 carry 1

Page 27: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Addition & Subtraction of Hex

• Due to the propensity for errors in binary, it is preferable to carry out arithmetic in hexadecimal and convert back to binary

• If we need to borrow in hex, we borrow 16

• It is convenient to think “in decimal” and then translate the results back to hex

Page 28: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Representing No.s in a Computer

• Remember, all numeric data is represented inside the computer as 1s and 0s– Arithmetic operations, particularly subtraction raise the

possibility that the result might be negative

• Any numerical convention needs to differentiate two basic elements of any given number, its sign and its magnitude– Conventions

• Sign-magnitude• Two’s complement• One’s complement

Page 29: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Representing Negatives

• It is necessary to choose one of the bits of the “basic unit” as a sign bit– Usually the leftmost bit– By convention, 0 is positive and 1 is negative

• Positive values have the same representation in all conventions

• However, in order to interpret the content of any memory location correctly, it necessary to know the convention being used used for negative numbers

Page 30: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Comparing the Conventions

Bit Pattern UnsignedSign-

Magnitude1's

Complement2's

Complement000 0 0 0 0001 1 1 1 1010 2 2 2 2011 3 3 3 3100 4 -0 -3 -4101 5 -1 -2 -3110 6 -2 -1 -2111 7 -3 -0 -1

Page 31: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Sign-Magnitude

• For a basic unit of N bits, the leftmost bit is used exclusively to represent the sign

• The remaining (N-1) bits are used for the magnitude

• The range of number represented in this convention is –2 N+1 to +2 N-1 -1

Page 32: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Sign-magnitude Operations

• Addition of two numbers in sign-magnitude is carried out using the usual conventions of binary arithmetic– If both numbers are the same sign, we add their

magnitude and copy the same sign– If different, determine which number I– has the larger magnitude and subtract the other from it.

The sign of the result is the sign of the operand with the larger magnitude

– If the result is outside the bounds of –2 n+1 to +2 n-1 –1, an overflow results

Page 33: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Two’s Complement Convention

• A positive number is represented using a procedure similar to sign-magnitude

• To express a negative number1. Express the absolute value of the number in binary2. Change all the zeros to ones and all the ones to zeros (called

“complementing the bits”)3. Add one to the number obtained in Step 2

– The range of negative numbers is one larger than the range of positive numbers

– Given a negative number, to find its positive counterpart, use steps 2 & 3 above

Page 34: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Two’s Complement Operations

• Addition:– Treat the numbers as unsigned integers

• The sign bit is treated as any other number

– Ignore any carry on the leftmost position

• Subtraction– Treat the numbers as unsigned integers– If a borrow is necessary in the leftmost place,

borrow as if there were another “invisible” one-bit to the left of the minuend

Page 35: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Overflows in Two’s Complement

• The s range of values in two’s-complement is –2 n+1 to +2 n-1 –1

• Results outside this band are overflows

• In all overflow conditions, the sign of the result of the operation is different than that of the operands

• If the operands are positive, the result is negative

• If the operands are negative, the result is positive

Page 36: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

One’s Complement• Devised to make the addition of two numbers with

different signs the same as two numbers with the same sign

• Positive numbers are represented in the usual way• For negatives

– STEP 1: Start with the binary representation of the absolute value

– STEP 2: Complement all of its bits

• Operations– Treat the sign bit as any other bit– For addition, carry out of the leftmost bit is added to

the rightmost bit – end-around carry

Page 37: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Binary & Alphanumeric Codes

• A binary code is a group of n bits that assume up to 2n distinct combinations of 1’s and 0’s with each combination representing one element of the set that is being coded

• With two bits we can form a set of four elements

• With three bits we can represent 8 elements

• With four bits we can represent 16 elements

Page 38: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Weighted Codes

• A sequence of binary digits representing a decimal digit is called a code word– The code with weights 8, 4, 2, 1 is know as the Binary-

Coded-Decimal (BDC) code– Another code could be weighted 2-4-1-2

• Some codes are not unique in representing decimal numbers– These codes cannot be used interchangeably– The correct code is self-complementing

• The value of 9-N can be obtained by complementing the bits of the code

Page 39: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Alphanumeric Codes

• American Standard Code for Information Interchange (ASCII)– 7-bit code– Since the unit of storage is a bit, all ASCII codes are

represented by 8 bits, with a zero in the most significant digit

– H e l l o W o r l d– 48 65 6C 6C 6F 20 57 6F 72 6C 64

• Extended Binary Coded Decimal Interchange Code (EBCDIC)

Page 40: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Error Detection– When binary data is transmitted, there is a possibility of – an error in transmission due to equipment failure or

noise• Bits change from 0 to 1 or vice-versa

– The number of bits that have to change within a byte before it becomes invalid characterizes the code

• Single-error-detecting code– To detect single errors have occurred we use an added parity

check bit – makes each byte either even or odd

• Two-error-detecting code

– The minimum distance of a code is the number of bits that need to change in a code word to result another valid code word

– Some codes are self-correcting (error-correcting code)

Page 41: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Even Parity Example

• Bytes Transmitted

• 11100011

• 11100001

• 01110100

• 11110011

• 10000101 Parity Block

B

I

T

• Bytes Received

• 11100011

• 11100001

• 01111100

• 11110011

• 10000101 Parity Block

B

I

T

Page 42: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000

Hamming Code

• This method of multiple-parity checking can be used to provide multiple-error detection

Page 43: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 44: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 45: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 46: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 47: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 48: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 49: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 50: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 51: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 52: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 53: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 54: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 55: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 56: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 57: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000
Page 58: Introduction to Computer Science 1 With Examples in Visual Basic, C, C++, and Java 1 Mata-Toledo, Ramon A. & Cushman, Pauline K. McGraw-Hill, Copyright@2000