0% found this document useful (0 votes)
11 views

Topic 1

Uploaded by

yadavsingh3012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Topic 1

Uploaded by

yadavsingh3012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

TOPIC 1

Introduction to Computer and Problem Solving


Reading: 1.1, 1.2, 1.3

- What’s a computer?
- Algorithms
- What’s a program?
- Data Representation

What are Computers?

2
Hardware and Software

• Hardware refers to all physical devices


– A computer consists of many pieces of hardware that all work
together
– Each piece of hardware does its own work
• A typical computer system contains:
– The CPU
– Main memory
– Secondary storage devices
– Input devices
– Output devices

The CPU

• The central processing unit is the part that actually runs programs
– The most important part of a computer
– Today CPUs are microprocessor
– Commonly used CPU vendors are Intel and AMD
Main Memory

• The computer’s work area


• Where the computer loads instructions of programs and data for
processing
• Commonly known as RAM, random-access memory
– Designed for CPUs to quickly access data stored at any random
location in the RAM
• They are a volatile type of memory
– When the computer is powered off, the contents in RAM are erased

Secondary Storage Devices

• Devices that can hold data for long periods of time, even when the power
is off
– Where important data and system files are stored
– Most commonly used is the disk drive which stores data by
magnetically encoding it onto a circular disk
– Solid state drives have no moving parts, and operate faster than a
traditional disk drive.
– Optical devices such as DVD-ROMs and CD-ROMs are also popular
– USB drives and SD memory cards are small devices that plug into a
port
Input Devices

• Input is any data the computer collects from people and


devices
• Devices that collect the data and send them to the
computer are called input devices
• Commonly used input devices are touch screens,
keyboards, mouses, scanners, microphones, and digital
cameras

Output Devices

• Output is any data the computer produces for people or devices


• The device that generates output for a computer is called an output device
• Commonly used output devices are screens, speakers, and printers
Software

• Categorized mainly into system software and application software


– System software are programs that control and manage the basic
operations of a computer. Subcategories are:
• Operating systems
• Utility programs
• Software development tools
– Application software are programs that perform special tasks
• But so is system software!

How Computers Store Data

• All data stored in a computer is converted to sequence of 0s and 1s;


each sequence is called a bit
• A computer’s memory is divided into tiny storage locations called
bytes
– Eight bits make a byte
• Combinations of bits, 0s and 1s, are used to represent characters. For
example,
– The character ‘A’ is 65 in ASCII code, which is converted to binary
format 1000001
– When you press ‘A’, 1000001 will be stored in computer’s memory
Digital and Digital Data

• “Digital” refers to anything that can only have two possible values
– Digital data is the data that is stored in binary.
– Digital devices are devices that work with binary data
– Computers are digital devices

Algorithms

12
Algorithm

An algorithm is an ordered set of unambiguous, executable steps that defines a process


which eventually terminates.
– Executing a step of an algorithm should not require any special skills, just the ability to
follow directions
– An algorithm contains:
- Conceptual (abstract) factors - the meaning
- Representation - the way it is presented

Properties of an Algorithm

• Every step is unambiguous


– You must specify exactly what to do.
• Input and output are clearly defined
– Bad: “Add up some values”
• What type of values? How many?
– What to do with the answer?
– Must be executable in finite amount of time
• Must finish before the end of time.

14
Algorithm

To represent an algorithm, we somehow need a formal language that is understood by


computers
i.e. English, French, etc.
Problems with “languages”:
- Primitives: the underlying concepts of a language
- Syntax: the representation of the language
- Semantics: meaning of the language
Drawbacks
- Ambiguous
- Not enough detail to represent information needed

Pseudocode

An algorithm involving a discrete structure is usually described using pseudocode.


– Pseudocode is not an actual programming language but retains much of what is
contained in regular programming languages so that it can easily be converted
– A series of steps in a pseudocode is called a block and is shown by the keywords
begin and end
Discovering an algorithm for a problem can often be found by using a flowchart
What are Programs?

17

How a Program Works

• A Program is a set of instructions (a “specified” algorithm) CPU reads


instructions written in machine language called instruction set
• A program will be copied into memory for CPU to execute
• CPU uses the “fetch-decode-execute” cycle for processing
– Fetch: Reads instructions from memory
– Decode: Decodes the instructions that were just read to determine
how to perform operations
– Execute: Actually performs
the operations
Programming Languages

– Machine languages:
• A sequence of 0s and 1s represent one instruction
• Differs from machine to machine
• low-level language
– High-level languages: more human readable languages that allow
programmers to create programs without knowing how CPU works.
• Modern languages are high-level

Keywords, Operators, and Syntax

• High level languages use keywords that have special meaning and cannot
be used for any purpose other than to write programs
• Operators are keywords that represent special program functions such as
addition, subtraction, and multiplication
• Syntax is a set of rules that must be strictly followed to write computer-
understandable instructions
– Syntax error is a mistake or violation of these rules
• Each instruction in a program is called a statement
Compilers and Interpreters

• Interpreter reads, translates, and executes the instructions of a high-level


language program
– Examples are PHP, Perl, Python, and ASP.NET

• A compiler translates a high-level language program to a separate machine


program for CPU to read and execute
– Source code: the statements a programmer writes in a high-level
language
– Compilation: translates a text-based source code to binary codes

The Compilation Process

Python: An interpreted language:


Editor
(IDLE) interpreter executing
algorithm program.py
program
machine
Note:
A Compiler would translate entire program in advance.

22
The Compilation Process 53 83 EC 08 E8 97 00 00
- Machine Language 00 00 81 C3 47 1D 00 00
8B 83 FC FF FF FF 85 C0
● A machine language can be processed 74 05 E8 32 00 00 00 83
directly by a computer. C4 08 5B C3 FF 35 04 A0
04 08 FF 25 04 A0 04 08
○ A program is a sequence of 00 00 FF 25 0C A0 04 08
instructions. 68 00 00 00 00 FF 25 10
A0 04 08 68 08 00 00 00
○ Each instruction is represented by a 00 E9 D0 FF FF FF FF 25
binary number and includes: 14 A0 04 08 68 10 00 00
00 00 E9 C0 FF FF FF 31
○ Operation code
ED 5E 89 E1 83 E4 F0 50
○ Optionally, an Operand 54 52 68 B0 84 04 08 68
○ No variable names or subroutine 40 84 04 08 51 56 68 40
84 04 08 E8 CF FF FF FF
names in machine language! F4 66 90 66 90 66 90 66
90 66 90 66 90 8B 1C 24
● Machine languages are very hard for
C3 66 90 66 90 66 90 B8
humans to write and understand.
Machine Code for part of the “Hello World!” program
23

Data Representation

24
Character Encodings

• Computers: made up of transistors.

• Transistors can only be ON or OFF.


• Therefore a computer can understand only two states (ON or OFF states of
its millions of transistors)
• These two states are represented by 0 and 1

• Thus a computer can understand only two numbers 0 and 1. (Computers


have only one finger!)

25

Character Encodings

• Recall: the main representation used in our computers are bits (binary
representation), which can be either 0 or 1.
– Question: So how does the computer know what we are trying to
input?
– Answer: Character encodings
• Using binary representation, we can obtain different types of data by
using that character encoding. Here are some examples:
– base n number systems
– ASCII
Character Encodings

Binary Representations:
• Bit - one binary digit (0,1)
• Nibble – a group of four bits
• Byte - a group of eight bits
• Word - a group of sixteen bits
• Bit string - a series of bits that the computer uses as data

Other Measures:
• Kilobyte = 210 bytes
• Megabyte = 220 bytes
• Gigabytes = 230 bytes

Character Encodings
- Base r number systems

Generalized Characters Encodings:


• Given n bits, we can encode M different characters, where 2n−1 < M ≤ 2n
• Another words, with n bits, we can make at most 2n different characters
n=2 n=3
Encoding Characters Encoding Characters
000 0
00 0
001 1
01 1
010 2
10 2 011 3
11 3 100 4
101 5
110 6
111 7
Character Encodings
- Base r number systems

• The base (radix) in a base r number system limits the number of


possible digits that a number can have. Here are some examples:
Name Base Possible Digits
Decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Binary 2 0, 1
Hexadecimal 16 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

Decimal: 0, 1, 2, … , 8, 9, 10, 11, 12, ………. 99, 100, 101, 102, ……….
Hexadecimal: 0, 1, 2, … , E, F, 10, 11, 12, ………. FF, 100, 101, 102, ……….

Binary: 0, 1, 10, 11, 100, 101, 110, ……….

Character Encodings
- Base r number systems

• A number is base n is denoted by xr


– In particular, 1010 and 102 are different numbers
– This is why base Conversions between two different bases is so
important in number systems
• Convert
– From base r to decimal (base 10)
– From decimal (base 10) to base r
– Between base 2 and base 16
Character Encodings
- Base r number systems
Counting in base r: decimal binary hex.
Given n bits, we can represent 2n different 0 0000 0
1 0001 1
numbers. These numbers range from 0 2 0010 2
to 2n−1 3 0011 3
4 0100 4
A 2 16 = 1010 0010 2 5 0101 5
6 0110 6
7 0111 7
10 1110 2 = 2 E 16 8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
16 10000 10

Character Encodings
- base r number systems

Procedure to convert binary to decimal


Input: a base 2 number
Output: a base 10 number
Step 1: Record the values of the columns that contains a 1 in the binary input number.
Step 2: Output the sum all recorded values.

Convert 1100012 into base 10.

Columns: 5 4 3 2 1 0

1100012 = 25 + 24 + 20 10 = 49 10
Character Encodings
- base r number systems

Procedure to convert decimal to binary


Input: a base 10 number
Output: a base 2 number
Step 1: Divide the decimal by 2 and record the remainder.
Step 2: Continue dividing the newest quotient obtained by 2; recording the remainders as
long as a quotient of 0 is NOT obtained.
Step 3: The Output is then the remainders recorded; starting from the last remainder going all
the way to the first remainder in the order they were recorded.

Convert 6210 into base 2. 62 / 2 = 31 R0


31 / 2 = 15 R1
15 / 2 = 7 R1
=1111102 7/2=3 R1
3/2=1 R1
1/2=0 R1

Character Encodings
- Base r number systems

Addition in base r number systems


– is done the same as in decimal (base 10)
– add from right hand side and carry when needed
Character Encodings
Negative number systems

Typically, in subtraction, signed integers are defined by using codewords of a


fixed length.
– i.e. For n bits, half of the codewords are assigned to positive numbers and
half to negative numbers
– Left-most Bit: 0 = positive number, 1 = negative number
– But what about the number 0?

Sign-Magnitude:

01110SM = + 14 10
11110SM = - 14 10

Character Encodings
Negative number systems

For signed integers, Two’s Complement is used:


- If a number is positive, it is the same representation as sign-magnitude
- If a number is negative, complement the positive representation of that
number and add 12.

011102C = +14 10
111102C = - (00001 + 1)2 = - (00010)2 = - 210
Character Encodings
Negative number systems

Advantages of two’s complement:


– Retains numeric value when added/subtracted
– No ambiguous representations (i.e. −0 and +0)
+0
-1 +1
00112C 01112C -2 1111
0000
0001 +2
+ 01002C 1110 0010
+ 00102C -3 +3
1101 0011
01112C 10012C Overflow! 1100 0100
-4 +4
1 1011 0101
-5 +5
11102C 11102C
-6 10101001 0111
0110
+ 11012C + 10012C 1000 +6
-7 +7
-8
1 10112C 1 01112C Overflow!

Character Encodings
- other encodings
In general,
Besides base r number systems, there are other ways to encode a character
(for input, output, memory, etc.)
• ASCII - includes 7 bits, codes 128 characters (next slide)
• Typically, we add a zero at the beginning to make it an 8 bit (1 byte)
representation.
• Unicode - extends ASCII to 16 or 32 bits: 4,294,967,296 different
characters
http://www.unicode.org/charts/
http://macchiato.com/unicode/chart/
ASCII table
B6 B5 B4
B3 B2 B1 B0 000 001 010 011 100 101 110 111
0000 null dle sp 0 @ P ` p
0001 soh dc1 ! 1 A Q a q
0010 stx dc2 “ 2 B R b r
0011 etx dc3 # 3 C S c s
0100 eot dc4 $ 4 D T d t
0101 enq nak % 5 E U e u
0110 ack syn & 6 F V f v
0111 bel etb ‘ 7 G W g w
1000 bs can ( 8 H X h x
1001 ht em ) 9 I Y i y
1010 lf sub * : J Z j z
1011 vt esc + ; K [ k {
1100 ff fs , < L \ l |
1101 cr gs - = M ] m }
1110 so rs . > N ^ n ~
1111 si us / ? O _ o del

You might also like