Introduction
Introduction
Computer Programming
Autumn 2023-24
1
Textbooks
2
Online Resource
3
Course Evaluation
• Weightage
Quizzes (4): 20%
Midsem: 30%
Endsem: 50%
4
Quiz Details
• Four Quizzes
• Dates will be announced one or two days prior
to the exam date.
• For the lab groups A3a and A3c, the quizzes
will be conducted in the lab session.
• A makeup quiz may be conducted only on
producing a genuine medical certificate from
an institute doctor.
5
Attendance
• 75% attendance is mandatory.
• The rest includes your sick leave, casual leave,
etc.
• Class bunk will be considered as absent.
6
Questions?
7
Computer Fundamentals and C
Programming
8
What Is A Computer?
A computer is an electronic device,
• Accepts input
• Processes data
• Stores data
• Produces output
9
Hardware Devices that comprise a computer system
Printer
(output)
Storage devices
Mouse (CD-RW, Floppy,
(input) Hard disk, …)
Scanner Keyboard
(input) (input)
10
11
CPU
12
CPU
The CPU is a silicon chip that contains millions of tiny
electrical components.
13
Performs calculations and
decisions
Arithmetic / Logic Unit
Coordinates
Control Unit processing steps
Small, fast
Registers storage areas for
instructions and
data
14
15
Registers
Registers are small, fast memory within the
CPU
Different registers hold different things like
instructions and addresses of instructions, data
(operands) and results of operations
Two types
1. Special Purpose Registers contain specific information
the CPU needs.
Instruction Register (IR) contains the actual instruction
which is currently being executed by the CPU.
• The Program Counter (PC) contains the address of the
next instruction to be executed by the program.
2.General Purpose Registers hold:
the operands for arithmetic and logical operations (ie. the
values on which the operation will be performed)
the results of such operations
So General Purpose Registers are used for holding and
manipulating data used by the CPU
17
Memory
18
Memory
Computer Memory : millions/billions of on/off charges
• Bits: 0 or 1
• Bytes: Groups of 8 bits, A byte is the smallest unit of storage.
(Can hold one text character)
• Words: Groups of bits/bytes (8, 16, 32, 64-bits)
Storage is usually too large to be expressed in bytes or words.
Kilobyte (KB) = 1024 bytes (210 bytes)
Megabyte (MB) = 1024 x 1024 bytes or one million bytes (220 bytes)
Gigabyte (GB) = 1024 x 1024 x 1024 bytes or 1 trillion bytes (230
bytes)
Terabyte (TB) = 1024 x 1024 x 1024 x 1024 bytes one quadrillion
bytes (240 bytes)
19
Memory
• What is the difference between followings:
– RAM
– CACHE
– SSD
– HDD
MEMORY CLASSIFICATION
Registers
2ns
Cache (I, II, III)
Volatile SRAM
DRAM
Primary SDRAM
(Semiconductor – RAM EDRAM
chip). Main Memory
EDO
FLASH RAM
Memory Types &
Storage Devices PROM
Non-volatile EPROM
ROM EEPROM
Tape
Magnetic memory HD, Zip Disk
Secondary FDD
(Devices)
Optical
CD-ROM, CD-R, CD-RW
Memory DVD-ROM, DVD-R 5ms
DVD- RW
Memory
Main Memory
27
Cache Memory
• But as CPU speeds became faster, the main
memory couldn’t provide the CPU with the
instructions at a fast enough rate.
• So even faster memory ( cache memory) is now
placed between the CPU and main memory to
provide the instructions at an quicker rate to
the CPU.
Cache Memory
33
Secondary Storage Devices
37
CPU and Memory
• CPU can interact with main memory in two
ways:
– It can write a byte/word to a given memory location.
• The previous bits that were in that location are
destroyed
• The new bits are saved for future use.
– It can read a byte/word from a given memory location.
• The CPU copies the bits stored at that location and
stores them in a CPU register
• The contents of the memory location are NOT
changed.
Access to Instructions
40
**Program**
41
Program
• A computer program is a collection
of instructions that performs a specific task
when executed by a computer.
• Most computer devices require programs to
function properly.
42
Programming Language
• A programming language is a formal
language, which comprises a set of
instructions that produce various kinds
of output.
43
Programming paradigms (models)
• Unstructured
• Structured
• Object oriented
44
Unstructured programming
• Writing small and simple programs consisting
of only one main function in the program.
45
Structured programming
• A program is broken down into small
independent tasks that are small enough to be
understood easily, without having to
understand the whole program at once.
• Example – C language
46
What is?
• Editor
• Translator
– Complier
– Assembler
– Interpreter
• Linker
• Loader
Translator
• A translator is a programming language
processor
– that converts a computer program from one
language to another.
• It takes a program written in source code and
converts it into machine code.
• It discovers and identifies the error during
translation.
48
49
Complier
• A compiler is a program that accepts a source
program in a “high-level language “and
produces a corresponding object program.
Assembler
• Input to an assembler is an assembly language
program.
• Output is an object program plus information
that enables the loader to prepare the object
program for execution.
Loader
• A Loader is a routine that loads an object
program and prepares it for execution.
• In general, the loader must load, relocate and
link the object program.
• Loader is a program that places programs into
memory and prepares them for execution. The
loader places into memory the machine
language version of the user’s program and
transfers control to it.
First Program
#include<stdio.h>
/*first program*/
int main()
{
printf(“Hello-CP”);
return 0;
}
53
Second Program
#include<stdio.h>
/*first program*/
int main()
{
int a;
a=4+7;
printf(“value=%d”,a);
return 0;
}
54
How a simple program (C/C++)
executes??
What happens when we turn on
computer?
• Bootstrap program is loaded at power-up or
reboot
– Typically stored in ROM or EPROM, generally
known as firmware
– Initializes all aspects of system
– Loads operating system kernel and starts
execution
System Boot
• When power initialized on system, execution starts at a
fixed memory location
– Firmware ROM used to hold initial boot code
• Operating system must be made available to hardware
so hardware can start it
– Small piece of code – bootstrap loader, stored in ROM or
EEPROM locates the kernel, loads it into memory, and
starts it
– Sometimes two-step process where boot block at fixed
location loaded by ROM code, which loads bootstrap
loader from disk
• Common bootstrap loader, GRUB, allows selection of
kernel from multiple disks, versions, kernel options
• Kernel loads and system is then running
What is the difference between 32 bit
and 64 bit OS??
Answer
• When talking about CPUs, the number of bits
usually refers to the size of a CPU’s register.
Each register bit can reference a byte in
memory.
• 16 bit computers can reference up to 64KB of
RAM and 32 bit computers can reference up
to 4GB of RAM.
• A 64 bit CPU can reference up to 16EB (8TB) of
RAM.
Answer
• In computing, 32-bit and 64-bit are two different types
of processors. The bit number (usually 8, 16, 32, or 64)
refers to how much memory a processor can access
from the CPU register.
• A 32-bit system can access 232 (4,294,967,296) memory
addresses. Practically speaking, a 32-bit computer is
limited to accessing 4,294,967,296 bytes (4 GB) of RAM
.
• A 64-bit processor can access 264memory addresses.
This means a 64-bit machine could theoretically access
18,446,744,073,709,551,616 memory addresses or
more than 18 billion GB of RAM.
ALGORITHMS AND FLOWCHARTS
• Implementation phase
– implement the program in some programming language
Steps in Problem Solving
• First produce a general algorithm (one can use
pseudo code)
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
• Write an algorithm and draw a flowchart to
convert the length in feet to centimeter.
Pseudo code:
• Input the length in feet (ft)
• Calculate the length in cm (cm) by multiplying
ft with 30
• Print length in cm (cm)
Algorithm Flowchart
Print
Lcm
STOP
Complier
• A compiler is a program that accepts a source
program in a “high-level language “and
produces a corresponding object program.
Assembler
• Input to an assembler is an assembly language
program.
• Output is an object program plus information
that enables the loader to prepare the object
program for execution.
Loader
• A Loader is a routine that loads an object
program and prepares it for execution.
• Loader is a program that places programs into
memory and prepares them for execution. The
loader places into memory the machine
language version of the user’s program and
transfers control to it.
First Program
#include<stdio.h>
/*first program*/
int main()
{
printf(“Hello-CP”);
return 0;
}
77
Second Program
#include<stdio.h>
/*first program*/
int main()
{
int a;
a=4+7;
printf(“value=%d”,a);
return 0;
}
78