esc101-lec4
TRANSCRIPT
1
ESc101: Fundamentals of Computing
2011-12-Monsoon Semester
Please switch off your mobile phones.
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
1
Announcements
• Section E11 will have a lab on Fridays.
• Section E15 will have a lab on Wednesdays.
• Monday lab and grading policy is on moodle.
• Tuesday/Wednesday labs will be on moodle today.
2
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
2
Recap
• Algorithm – Converting binary to decimal– Flow chart is on moodle
• Started with basic model of a computer
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
3
Recap: Model of a Computer
• From the perspective of programming, the main components are:– CPU: Central Processing Unit
• ALU: Arithmetic and Logic Unit
• Control Unit
– RAM: Random Access Memory
• Additional devices of interest are:– Storage (e.g., Disks)
– Input (e.g., keyboard, mouse)
– Output (e.g., monitor, printer)
3
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
4
Recap: Memory
• Bit: Having a value of 0 or 1
• Byte: Having 8 bits
• Kilobyte (KB): 1024 bytes
• Megabyte (MB): 1024 KB
• Gigabyte (GB): 1024 MB
• Is generally byte addressable– 32 bit addresses would mean a maximum memory of 4 GB.
• Any byte can be read at any time – random access
• Programs and Data are stored in memory.
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
5
Recap: ALU
• Can execute arithmetic and logical operations– Add, Subtract, Multiply, Divide (for integers)
– Right shift, Left shift
– Comparison of two bytes or two numbers
– What operations needs to be done next on what inputs is controlled by the program
4
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
6
Recap: Control Unit
• Responsible to bring the next instruction of the program.
• It then decodes the instruction and tells ALU what to execute next
• It is also responsible to bring in operands from memory to ALU.
• Similarly, it stores the results of the operation back to memory.
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
7
Recap: Binary Format
• The basic memory unit in computer is a bit– Computer can only understand binary: 0 and 1.
– Computer can only operate on binary input and produce binary output.
– For example, if number 17 has to be stored in a byte, it will be stored as 00010001.
5
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
8
Program
• The Control Unit can only understand a sequence of binary bits.
• So the program has to be in binary format, also known as “machine language.”
• Program is a sequence of instructions in machine language.
• The execution happens as follows:
– Control unit fetches an instruction
– If operands are in memory locations, fetch them as well.
– Ask ALU to execute the instruction on the operands
– The result needs to be stored in the memory location.
– Next instruction is fetched, unless the previous instruction asked the Control Unit to fetch a difference instruction.
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
9
A Small Program
0000010010011001 – read memory location 0010000010010011010 – read memory location 0100000011011001000 – add two numbers read
0000010011010001 – store the result in memory location 001
6
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
10
Assembly Language
• Extremely difficult to write program in machine language.
• Even more difficult to understand machine language.
• Hence the designers of CPU also design a higher level language called, Assembly Language.
• This has mnemonics for both operations and the operands.– Hence somewhat easier to follow
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
11
Example Program
• MOV NUM1, R1
• MOV NUM2, R2
• ADD R1, R2
• MOV R1, NUM1
• Move contents of memory location NUM1 to location R1
• Move contents of memory location NUM2 to location R2
• Add contents of R1 and R2 and store the result in R1
• Move the contents of location R1 to Memory location NUM1
7
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
12
Assembler
• Assembler translates the assembly language program to a machine language program.
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
13
Need for a Higher Level Language
• Very difficult to write large programs in assembly language.
• New programming languages have been designed to write programs easier.
• These include: C, Pascal, Fortran, Cobol, C++, Java, Lisp, Python, …
8
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
14
Compiler
• Compiler is a program that translates a higher level language to machine language
• There are many C Compilers available.– We will be using ‘gcc’.
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
15
Input/Output
• To solve problems, we need to provide input to the program, and the program needs to give output.
• The program should be able to read from keyboard, understand mouse clicks, etc.
• The program should be able to write on the monitor or display unit.
• The programs should be stored on permanent storage, e.g., disks.
• All this is done with the help of Operating System.
9
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
16
Operating System
• Manages all resources of the computer.– Allocates space on disk for files.
– Allocates memory for your programs
– Allows multiple programs to be running in “parallel.”
– Reads what you type on keyboard.
– Maintains access control• Files of one user cannot be read by another user, unless
allowed.
• Examples: Linux, Windows, Solaris, Android, …
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
17
Our First ‘C’ Program
/* This program computes Body-Mass Index */
#include <stdio.h> /* To include I/O Header files */main () /* Main function heading */{
int mass; /* To store weight of the body in KGs */float height; /* To store height in meters */float bmi; /* To store body-mass-index */
printf (“Body Weight in KG = ?”); /* Prompt user for Weight */scanf (“%d”, &mass); /* Read Weight in KG */printf (“Height in Meters = ?”); /* Prompt user for height */scanf (“%f”, &height); /* Read Height in meters */
bmi = mass / (height * height); /* Compute BMI */
printf (“BMI value is: %f\n”, bmi); /* Print the answer */}
10
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
18
Points to Note
• #include
• Comments
• main ()
• int
• float
• printf
– %f, \n
• scanf
– %d, %f, &
• Variable names
• Arithmetic expressions
• Assignment
• Types
• Integer versus Floating point operations
Lec-04 Dheeraj Sanghi, CSE Dept., IIT KanpurESc101, 2011-12-Monsoon
19
Any Questions?