Programming and Data Structures: Debasis Samanta
Programming and Data Structures: Debasis Samanta
Debasis Samanta
Computer Science & Engineering
Indian Institute of Technology Kharagpur
Spring-2017
Lecture #1
Introduction to Digital Computers
The ancient Greek-designed Antikythera mechanism, dating between 150 and 100
BC, is the world's oldest analog computer.
CS 10001 : Programming and Data Structures 6 Lecture #01: © DSamanta
Brief history of computer…
Colossus was the first electronic digital programmable computing device, and
was used to break German ciphers during World War II.
CS 10001 : Programming and Data Structures 10 Lecture #01: © DSamanta
Brief history of computer…
ENIAC was the first Turing-complete device, and performed ballistics trajectory
calculations for the United States Army.
CS 10001 : Programming and Data Structures 11 Lecture #01: © DSamanta
Brief history of computer…
Processing
The input data is processed by
a well-defined and finite
sequence of steps (also called
program.
Output
Some data available from the
processing step are output (at
least one) to the user.
Processing unit
The central processing unit (CPU) is the brain of the computing device and performs the basic processing
steps. A CPU typically consists of:
An arithmetic and logical unit (ALU): This provides the basic operational units of the CPU. It is made up of units (like
adders, multipliers) that perform arithmetic operations on integers and real numbers, and of units that perform logical
operations (logical and bitwise AND, OR etc.).
A control unit: This unit is responsible for controlling flow of data and instructions.
General purpose registers: A CPU usually consists of a finite number of memory cells that work as scratch locations for
storing intermediate results and values.
Output devices
These devices notify the user about the outputs of a computation.
Example: Screen, printer, speaker, plotter, etc.
CS 10001 : Programming and Data Structures 18 Lecture #01: © DSamanta
Fundamental in Processing
etc.
Any number is represented by,
Example:
MSB
Integer fraction
.3750
0 .7500
1 .5000
1 0
Thus,
Convert to binary
quotient remainder
(231)8
2 1 0
2
× 8 +3 × 8 +1× 8
The complement of is
Example: 101101
+ 010111
1000100 C=A+B
A better way
The subtraction of
Steps
• Add the minuend M to the complement of the subtracted N. Thus,
M=72532
complement of N=96750
+169282
Discard the end carry-100000
Answer 69282
Example: Using complement, subtract
M= 03250
complement of N= (+) 27468
Sum 30718
There is no end carry
Obtain (a)
(b)
a) X=1010100
complement of Y=0111101
Sum (+) 10010001
Discard end carry 10000000
Answer 0010001
b) Y=1000011
complement of X=0111101
Sum (+) 1101111 There is no end carry
Answer complement of 1101111
=
Example
Unsigned Signed
0 +0
1 +1
2 +2
3 +3
4 -0
5 -1
6 -2
7 -3
Secondary memory
This is relatively inexpensive, bigger and low-speed memory.
It is normally meant for off-line storage, i.e., storage of programs and data for future processing.
One requires secondary storage to be permanent, i.e., its contents should last even after shut-down.
Examples of secondary storage include floppy disks, hard disks and CDROM disks.
Buses
A bus is a set of wires that connect the above components. Buses are responsible for movement of data from
input devices, to output devices and from/to CPU and memory.
CS 10001 : Programming and Data Structures 35 Lecture #01: © DSamanta
Basic components of a digital computer…
Feature Specification
Hard disk 1 TB
Machine
3. Execution: Operating system
Code CPU
Program
2. Translation: Compiler
Many problems and hence many programs. The same computer can be used to
solve many problems.
This is why computer is “general purpose” digital device!
• Machine language
• Machine can understand only two voltage levels{ 0 (low voltage) and 1 (high
voltage)}
• Instruction should be given in such a way, so that machine can understand
• Example: 001100110001111010
• The machine language is so low-level that writing a program in this language is
a very formidable task. One ends up with unmanageably huge codes that are
very error-prone and extremely difficult to debug and update.
MOV A,X ; A = X
ADD A,Y ; A = A + Y
ADD A,Z ; A = A + Z
DIV A,3 ; A = A / 3
MOV RES,A ; RES = A
• You then require computer programs that convert your high-level description to
the assembly-level descriptions of individual machines, one program for each kind
of CPU. Such a translator is called a compiler.
• Therefore, your problem solving with computers involves the following three
steps:
• Write the program in high-level language, For example, Z = (A + B + C)/3
• Compile your program to get machine level program.
• Run the machine executable file.
Machine
3. Execution: Operating system
Code CPU
Program
2. Translation: Compiler
Instruction area
The instruction area stores a sequence of instructions that define the steps of the program.
Under the control of a clock, the computer carries out a fetch-decode-execute cycle in which
instructions are fetched one-by-one from the instruction area to the CPU, decoded in the
control unit and executed in the ALU.
The CPU understands only a specific set of instructions. The instructions stored in memory must
conform to this specification.
Execution of a program
CS 10001 : Programming and Data Structures 49 Lecture #01: © DSamanta
How can one program?
• Program writing in a high-level language
• You should use a text editor to key in your program. In the laboratory we instruct you to use the emacs editor. You
should also save your program in a (named) file.
• We are going to teach you the high-level language known as C.
• There are only 32 keywords and its strength lies in its built-in functions.
• C is highly portable, since it relegated much computer dependent features
to its library functions.
This program takes no input, but outputs the string "Hello, world!" in a line.
#include <stdio.h>
main ()
{
printf("Hello, world!\n");
}
#include <stdio.h>
#include <stdio.h>
main
main ()()
{{
intint
n; n;
scanf("%d",&n);
scanf("%d",&n);
printf("%d\n",n);
printf("%d\n",n);
}}
#include <stdio.h>
#include <stdio.h>
main
main ()()
{{
intint
n; n;
scanf("%d",&n);
scanf("%d",&n);
printf("%d\n",n*n);
printf("%d\n",n);
}}
#include <stdio.h>
#include <stdio.h>
main
main ()()
{{
intint
n; n;
scanf("%d",&n);
scanf("%d",&n);
printf("%d\n",1/n);
printf("%d\n",n);
}}
#include <stdio.h>
#include <stdio.h>
main
main ()()
{{
intint
n; n;
scanf("%d",&n);
scanf("%d",&n);
printf("%f\n",1.0/n);
printf("%d\n",n);
}}
4. What is cache memory? What is a virtual memory? What are there uses?
5. What is the difference between a “compiler” and an “interpreter”?. What are the merits and demerits in
them?
6. What was a punch card (once it was extensively used in IBM 360/20 system)?
7. Why a keyboard is called the standard input device and VDU is called the standard display device?
8. A CPU with 32-bits: What it does mean? Does a CPU with 64-bits better than the 32-bits one?
9. Who carries user input from user to CPU and output from CPU to user?
10. How 250.67 can be converted into a value in binary number system?
13. What is the other higher order for memory chunks beyond GB?
14. Find a method to convert hexa-decimal number into binary and vice-versa