Sei sulla pagina 1di 10

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester

Please switch off your mobile phones.

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.

Lec-04

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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 Kanpur ESc101, 2011-12-Monsoon

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)
Lec-04 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 3

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 Kanpur ESc101, 2011-12-Monsoon 4

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

Lec-04

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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 Kanpur ESc101, 2011-12-Monsoon

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.

Lec-04

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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.
Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

Lec-04

A Small Program

0000010010011001 read memory location 001 0000010010011010 read memory location 010 0000011011001000 add two numbers read 0000010011010001 store the result in memory location 001

Lec-04

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

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 Kanpur ESc101, 2011-12-Monsoon

10

Example Program
MOV MOV ADD MOV NUM1, R1 NUM2, R2 R1, R2 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
Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 11

Lec-04

Assembler
Assembler translates the assembly language program to a machine language program.

Lec-04

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

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,

Lec-04

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

13

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 Kanpur ESc101, 2011-12-Monsoon

14

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.
Lec-04 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15

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 Kanpur ESc101, 2011-12-Monsoon

16

Our First C Program


/* This program computes Body-Mass Index */ #include <stdio.h> main () { int mass; float height; float bmi; printf (Body Weight in KG = ?); scanf (%d, &mass); printf (Height in Meters = ?); scanf (%f, &height); bmi = mass / (height * height); printf (BMI value is: %f\n, bmi); } /* To include I/O Header files */ /* Main function heading */ /* To store weight of the body in KGs */ /* To store height in meters */ /* To store body-mass-index */ /* Prompt user for Weight */ /* Read Weight in KG */ /* Prompt user for height */ /* Read Height in meters */ /* Compute BMI */ /* Print the answer */

Lec-04

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17

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 Kanpur ESc101, 2011-12-Monsoon

18

Any Questions?

Lec-04

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

19

10

Potrebbero piacerti anche