Professional Documents
Culture Documents
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 1
Programming & Data Structure: CS11001/19001
Section - 9/C
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 2
Instructors
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 3
Tutorial
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 4
Feedback
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 5
Laboratory Classes
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 6
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 8
• Intel Pentium IV
– Clock : 1.7GHz,
– Cache Memory : 12K µOP ET Cache + 8KB
L1 Data Cache, 256KB L2 Unified Cache,
– 32-bit processor.
• Main Memory : 256MB,
• Hard Disk : 40GB.
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 9
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 10
A Computer
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 11
VDU
Processor
Memory
Disk etc.
Keyboard
Mouse
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 12
CPU−Memory Bus
Cache Memory
Main
DMA etc. Memory
MMU
IO Bus
CPU
IO Controller IO Controller
Mouse IO Controller
Disk I
Graphics Network
Disk II VDU
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 13
CPU IO
BUS
User Prog
Compiler
OS
Memory
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 14
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 15
Instruction
Set
Control
Instruction
and Data
from
Registers
Memory
FPU ALU
Data Path
Figure 1: A CPU
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 16
A Pentium Processor Chip is not just a CPU
but CPU + MMU + Cache Memory
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 17
Memory Subsystem
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 18
Data
C
P
U Instruction
Disk
10 GB
L1 Cache L2 Cache Main Memory
KB 100 KB 100 MB
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 19
A Compiler
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 20
Source
Language
Program
Compiler A Compiler is a
Software
Target
Language
Program
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 21
C Program
C Preprocessor
Object
Modified C Program
Module/
Library C Compiler
Assembly Language
Program
Linker Assembler
Object
Executable Module
Module (a.out) (test.o)
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 22
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 23
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 24
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 25
C Program to · · ·
$ cc -Wall first.c
$ ./a.out
The First C Program
$ ls -l first.c a.out
-rwxr-xr-x 1 goutam users 11370
Jan 1 10:36 a.out
-rw-r--r-- 1 goutam users 153
Jan 1 10:36 first.c
$
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 27
$ cc -Wall -S first.c
$ ls -l first.* a.out
-rwxr-xr-x 1 goutam users 11370
Jan 1 10:36 a.out
-rw-r--r-- 1 goutam users 153
Jan 1 10:36 first.c
-rw-r--r-- 1 goutam users 399
Jan 1 10:45 first.s
$
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 28
$ cc -Wall -c first.c
$ ls -l first.*
-rw-r--r-- 1 goutam users 153
Jan 1 10:36 first.c
-rw-r--r-- 1 goutam users 816
Jan 1 10:50 first.o
-rw-r--r-- 1 goutam users 399
Jan 1 10:45 first.s
$
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 29
.file "first.c"
.section .rodata
.LC0:
.string "The First C Program\n"
.text
.align 2
.globl main
.type main,@function
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 30
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
subl $12, %esp
pushl $.LC0
call printf
addl $16, %esp
movl $0, %eax
leave
& %
ret
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 31
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 3.2 20020903
(Red Hat Linux 8.0 3.2-7)"
Size: 399 Bytes
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 32
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 33
& %
26: c3 ret
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 34
08048328 <main>:
8048328: 55 push %ebp
8048329: 89 e5 mov %esp,%ebp
.....
804833b: 68 98 83 04 08 push $0x8048398
8048340: e8 23 ff ff ff call 8048268 <_init+0x38>
8048345: 83 c4 10 add $0x10,%esp
804834d: c9 leave
804834e: c3 ret
804834f: 90 nop
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 35
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 36
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 37
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 39
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 40
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 41
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 42
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 43
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 44
If There is an Error
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 45
Compiler Message
bash$ cc first.c
first.c:4: parse error before ‘{’
bash$
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 46
What to DO?
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 47
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 48
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 49
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 50
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 51
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 52
A man Page
$ man ls
LS(1) User Commands LS(1)
NAME
ls list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current
directory by default). ....................
-a, --all
do not hide entries starting with .
& %
...........................................
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 53
File System
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 55
/ root
/pclab2 pclab2
/pclab/g07 g07
Working
Directory
assignment a.out practice first.c
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 56
Creating Directories
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 57
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 58
Example - II
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 59
Example - III
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 60
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 61
Example - IV
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 62
Example - V
#include <stdio.h>
int main() { // **** fifth.c
int first, second, max ;
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 63
Example - VI
#include <stdio.h>
int main() { // **** sixth.c
int n, i, sum = 0 ;
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 64
Example - VII
#include <stdio.h>
int main() {
int n ;
printf("Enter a non-negative integer:") ;
scanf("%d", &n) ;
printf("\nSum of (0+...+%d)=%d\n",n,sum(n));
return 0 ; }
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 65
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 66
& %
' $
PDS Tut. & Lab.: I (CS 11001/19001): Section 9 Dept. of CS&Engg., IIT Kharagpur 67
#include <stdio.h>
int what(int n, int m)
{
if(m == 0) return n ;
else return what(m, n%m) ;
}
int main() {
int m, n ;
printf("Enter two non-negative integers:") ;
scanf("%d%d", &n, &m) ;
printf("\nwhat(%d, %d) = %d\n",m,n,what(m,n));
return 0 ;
}
& %