EST102: Programming in C: Foundations of Computing
EST102: Programming in C: Foundations of Computing
Chapter 1
Foundations of computing
Narasimhan T.
1.1 Introduction
Computer is an advanced electronic device that takes raw data as input from the user and
processes these data under the control of a set of instructions (called program) and gives
the result (output) and saves the output for future use. It can perform both numerical
(arithmetic) and non-numerical (logical) calculations.
1. Hardware – consists of the physical devices of a computer. The devices are required
for input, output, storage and processing of the data. Keyboard, monitor, hard disk
drive, processor are some of the hardware devices.
3. Data – are isolated values or raw facts, provided as input to the computer, which are
processed to generate some meaningful information.
4. Users – are people who write computer programs or interact with the computer. Pro-
grammers, data entry operators, computer hardware engineers fall into this category.
1
2 CHAPTER 1. FOUNDATIONS OF COMPUTING
CPU
Arithmetic Input
Logic Unit devices
Memory
(data and Registers
instructions)
Control Output
logic devices
3. Memory Unit.
The I/O unit consists of the input unit and output unit. The input unit accepts data
from the user and the output unit provides the processed data (information) to user. CPU
performs calculations and processing on the input data, to generate the output. The memory
unit is used to store the data, the instructions and the output information. Figure 1.1
illustrates a typical architecture of a computer.
1. Human data entry devices – the user enters data into the computer either by typing
or by pointing a device to a particular location. Some examples are
(b) Mouse – the most common pointing device. You move the mouse around on
a mouse pad and a small pointer called cursor follows your movements on the
computer screen.
(c) Track ball – an alternative to a mouse which has a ball on the top. The cursor
on the computer screen moves in the direction in which the ball is moved.
(d) Joystick – has a stick whose movement determines the cursor position.
(e) Graphics tablet – converts hand-drawn images into a format suitable for com-
puter processing.
(f) Light pen – can be used to “draw” on the screen or to select options from menus
presented on the screen by directly pointing the pen on the screen.
2. Source data entry devices – use special equipment to collect data at the source, cre-
ate machine-readable data and feed them directly into the computer. This category
comprises:
(a) Audio input devices – uses human voice or speech to give input. Microphone is
an example.
(b) Video input devices – gives input in the form of video or images. Video camera,
webcam are examples.
(c) Optical input devices – uses optical technology (light source) to input the data
into the computers. Some common optical input devices are scanner, bar code
reader. Other examples include Optical Character Recognition (OCR), Magnetic
Ink Character Recognition (MICR) and Optical Mark Recognition (OMR).
1. Hard copy devices – The output obtained in a tangible form on a paper or any
surface is called hard copy output. The hard copy can be stored permanently and is
portable. The hard copy output can be read or used without a computer. Examples
in this category include:
(a) Printer – prints information on paper. The information could be textual or even
images. Many printer types like drum printers, laser printers, inkjet printers
exist.
(b) Plotter – used to produce very large drawings on paper sizes up to A0 (16 times
as big as A4). A plotter draws onto the paper using very fine pens. Flat bed
plotter and drum plotter are the two types.
(c) COM (Computer Output on Microfilm) – stores the output (mostly as images)
on a microfilm. The copy of the image on microfilm retains its original clarity.
4 CHAPTER 1. FOUNDATIONS OF COMPUTING
2. Soft Copy Devices – generates soft copy of the output (output obtained in an intan-
gible form) on a visual display, audio unit or video unit. The soft copy can be stored
and sent via e-mail to other users. It also allows corrections to be made. The soft
copy output requires a computer to be read or used. This category comprises of:
(a) Monitor – main output device of a computer. Monitors can be of two types:
monochrome (black and white) or colour. It forms images from tiny dots, called
pixels that are arranged in a rectangular form.
(b) Video output devices – produces output in the form of video or images. An
example is screen image projector or data projector that displays information
from the computer onto a large white screen.
(c) Audio output devices – speakers, headset or headphone are used for audio output
(in the form of sound) from computer. The signals are sent to the speakers via
sound card that translates the digital sound back into analog signals.
1.3.2.2 Registers
Registers are high-speed storage areas within the CPU, but have the least storage capacity.
They store data, instructions, addresses and intermediate results of processing. Some of
the commonly used registers are:
• Accumulator (ACC) stores the result of arithmetic and logic operations.
• Instruction Register (IR) contains the instruction that is currently being executed.
• Program Counter (PC) contains the address of next instruction to be processed.
• Memory Address Register (MAR) contains the address of the data to be fetched.
• Memory Buffer Register (MBR) temporarily stores data fetched from memory or the
data to be sent to memory. It is also called Memory Data Register (MDR).
• Data Register (DR) stores the operands and any other data.
1.3. ARCHITECTURE OF A COMPUTER 5
CPU
registers internal
memory
cost, speed Cache memory
increases
capacity Main memory
decreases
Magnetic disk
external
Optical disk memory
Magnetic tape
1. responsible for generating control signals which control the various operations of the
computer.
2. obtains the instructions from the memory, interprets them, and directs the computer
to execute the instructions by communicating with the ALU and the registers.
4. decides when to fetch the data and instructions, what operation to perform, where to
store the results, the ordering of various events during processing etc.
3. Optical disk – a flat and circular disk which is coated with reflective plastic material
that can be altered by laser light. The data bits 1 and 0 are stored as spots that are
relatively bright and light, respectively. CDs, DVDs are examples.
The remainder of the instruction fields differs from one computer type to other. The
operation code represents action that the processor must execute. It tells the processor
what basic operations (add, subtract etc.) to perform. The operand code defines the data
(operands) on which the operations are to be performed. It specifies either the locations
where to fetch the operands or the value of the operands itself.
from its memory, determines what actions to perform, and carries out those actions. The
instruction cycle is shown in Figure 1.3.
start
fetch
store decode
execute
1.1 Fetch the instruction from memory whose address is held currently in program
counter.
1.2 Store the fetched instruction in instruction register.
1.3 Increment the program counter so that it then has the address of the next in-
struction to be fetched.
2.1 Based on the instruction set architecture, the instruction is broken into opcode
and operand codes
2.2 The operation to be performed is identified.
3.1 The operation identified in the decode step is now performed by the CPU.
4.1 The result generated by the operation is stored in the main memory, or sent to
an output device.
1. Machine Language :- is what the computer can understand but it is difficult for the
programmer to understand. Machine languages consist of binary numbers only.
2. High level language :- is easier to understand and use for the programmer but difficult
for the computer. The programs written in high level languages contain English-like
statements as well as mathematical expressions.
Machine languages and assembly languages are also called low-level languages.
1. The computer can understand the programs written in machine language directly. No
translation of the program is needed.
2. Program written in machine language can be executed very fast (Since no translation
is required).
4. The machine-level instructions have an opcode field which specifies the actual opera-
tion (such as add or compare), and some other fields for the operands. For example if
the opcode for addition is 1010, then to add 3 and 6 (which are known as operands),
the binary code (assuming that the opcode and operands occupy 4 bits) would be
|{z} 0011
1010 |{z} 0110
|{z}
add 3 6
10 CHAPTER 1. FOUNDATIONS OF COMPUTING
1. Assembly language programs are easier to write than the machine language programs,
since assembly language programs use short, English-like representation of machine
code. For example to add 3 and 6, we would write ADD 3,6.
3. Although assembly language programs use symbolic representation, they are still diffi-
cult to write. They are generally written when the efficiency and the speed of program
matters.
1. Programs are easier to write, read and understand in high-level languages than in
machine language or in assembly language.
2. Most of the operations like arithmetic and logical operations are denoted by operators.
Thus to add 3 and 6, we just write 3+6. Here ‘+’ is the operator for addition.
3. Since the programs written in high-level languages are not machine-dependent, they
are easily portable from one computer to another.
1.7.1 Assembler
Assembler converts a program written in assembly language into machine code. There is
usually a one-to-one correspondence between the assembly statements and the machine lan-
guage instructions. The machine language is dependent on the processor architecture. Thus
the converted assembly language programs also differ for different computer architectures.
1.7.2 Compiler
Compiler is the software that translates the program written in a high-level language to
object code which can be then executed independently. Each programming language has its
own compiler. The compilation process generally involves breaking down the source code
into small pieces and creating an intermediate representation, and then constructing the
object code from the intermediate representation. The compiler also reports any errors in
the source code.
1.7.3 Interpreter
The interpreter also converts the high-level language program into machine code. However,
the interpreter functions in a different way than a compiler. Interpreter reads the source
code line-by-line, converts it into machine understandable form, executes the line, and then
proceeds to the next line. Some languages that use an interpreter are BASIC and Python.
The key differences between a compiler and an interpreter are shown in Table 1.1.
Compiler Interpreter
1. translates the entire source code at
1. translates the source code line-by-line.
once.
2. converts the entire source code into 2. converts a line of source code into ma-
object-code which is then executed by the chine executable form, executes the line,
user. and proceeds with the next line.
3. Once the object code is created, it
can be executed multiple number of times 3. During each execution, the source code
without the need to compile during each is first interpreted and then executed.
execution.
4. During execution of an object code, 4. Both interpreter and the source code
the compiler is not required are required during execution.
5. faster 5. slower
6. Languages like C, C++ and Java are 6. Languages like BASIC, Pascal and
compiled. Python are interpreted.
12 CHAPTER 1. FOUNDATIONS OF COMPUTING
• Loader – loads the executable object code into main memory for execution.
• Linker – links several object code modules into a single executable code.