Week 01 Lectures
Course Introduction |
CP1521 Computer Systems Fundamentals | 2/78 |
CP1521 on the Web | 3/78 |
Primary entry point is Webcms3
http://webcms3.cse.unsw.edu.au/DPST1092/19T2/
Most of the content lives under
/home/dp1092/public_html/19T2/... (e.g.lecs,labs,tutes,...)
Most content is web-accessible via
http://cgi.cse.unsw.edu.au/~dp1092/19T2/index.php
... CP1521 on the Web | 4/78 |
Most material on Webcms3 is publically readable.
Login to Webcms3 is via zID/zPass, and is needed for
give
Check marks via sturec
For questions: use Webcms3 forum or email A.Finlayson@unswglobal.unsw.edu.au
Course Goals | 5/78 |
CP1511
CP1511 vs CP1521 | 6/78 |
CP1511
... CP1511 vs CP1521 | 7/78 |
CP1521 ...
Themes | 8/78 |
Major themes ...
Textbook | 9/78 |
There is no prescribed textbook
Recommended reference ...
|
Acknowledgements | 10/78 |
Material and resources have been mainly drawn from:
Systems and Tools | 11/78 |
Prac work based on Linux tools
gcc
dcc
Assembly language: MIPS on QtSpim
Use your own favourite text editor
Other tools: man
gdb
valgrind
make
bc
Learn to love the shell and command-line ... very useful
Classes | 12/78 |
Lectures
Assessments | 13/78 |
Lab exercises contribute 10% to overall mark.
Ideally, the lab exercise for Week X must be
Exceptionally good submissions can get bonus (A+)
Total mark for labs can be > 10 (capped at 11).
... Assessments | 14/78 |
Two assignments ...
Good time management avoids late penalties
Quizzes | 15/78 |
Five small online quizzes ...
A Practice quiz in week 1 ... C revision quiz. This will help you work out any areas you need to revise.
Then in weeks 3, 6, 8, 10, 12
Each quiz released on the wednesday at 9am, due before Sunday 11:59pm at end of week. A quiz in week X could contain material covered in lectures up to week X-1 and covered in tutorials and labs in week X.
Final Exam | 16/78 |
3-hour on-line exam during the exam period.
Held in CSE labs (must know lab environment)
On-line documentation available in exam:
How to pass? Practice, practice, practice, ...
Course Assessment | 17/78 |
CourseWorkMark = QuizMark + LabMark + Ass1Mark + Ass2Mark (out of 40) ExamPracMark = marks for prac questions on final exam (out of 30) ExamTheoryMark = marks for written questions on final exam (out of 30) ExamMark = ExamPracMark + ExamTheoryMark (out of 60) ExamOK = ExamMark ≥ 24/60 (true/false) FinalMark = CourseWorkMark + ExamMark (out of 100) FinalGrade = UF, if !ExamOK && FinalMark ≥ 50 = FL, if FinalMark < 50/100 = PS, if 50/100 ≤ FinalMark < 65/100 = CR, if 65/100 ≤ FinalMark < 75/100 = DN, if 75/100 ≤ FinalMark < 85/100 = HD, if FinalMark ≥ 85/100
Computer Systems |
What they used to look like | 19/78 |
ENIAC ( Electronic Numeric Integrator and Computer) : 1945
Computer Systems | 20/78 |
Component view of typical modern computer system
Processor | 21/78 |
Modern processors provide
Storage | 22/78 |
Memory (main memory) consists of
... Storage | 23/78 |
Run-time memory usage depends on language processor.
How typical C compiler uses the memory:
Computer System Layers | 24/78 |
View of software layers in typical computer system
C Program Life-cycle | 25/78 |
From source code to machine code ...
Exercise 1: C Compiler Stages | 26/78 |
Using a small program
#include <stdio.h> #include "x.h" #define NUM 10 int main(void) { int x = NUM; printf("Hello\n"); return 0; }
investigate intermediate stages of compilation using flags -E, -S, -c
The Linux Manual |
man | 28/78 |
The linux manual (man
ls
cp
strcpy
scanf
You can find the full table by using the command man man
You can get more information about individual sections by using man 1 intro
man 2 intro
Advice: man
Exercise 2: Using the linux manual | 29/78 |
Use man
o
Searching the manual | 30/78 |
You can search the manual for a keyword using man -k
For example, I want to find a command to view a pdf I could type
$ man -k pdf
Linux Streams and Pipes |
Standard Streams | 32/78 |
A stream is a sequence of bytes
stdio.h
stdin
stdout
stderr
IO Redirection | 33/78 |
a.out
data.txt
./a.out < data.txt
a.out
output.txt
./a.out > output.txt
a.out
output.txt
./a.out >> output.txt
a.out
errors.txt
./a.out 2> errors.txt
a.out
allOut.txt
./a.out &> allOut.txt
Using stderr for error Messages | 34/78 |
fprintf
fprintf(stderr, "error: can not open %s\n", argv[1]);
printf
fprintf
fprintf(stdout, ...);
Unix Pipes | 35/78 |
a.out
wc
./a.out | wc -l
Makefiles |
Multi-module C Programs and Makefiles | 37/78 |
All large systems written in C ...
.c
$ gcc -c Stack.c# produces Stack.o $ gcc -c main.c# produces main.o $ gcc -o main main.o Stack.o# links Stack.o + main.o
... Multi-module C Programs and Makefiles | 38/78 |
While developing a large system ...
.c
gedit Stack.c
.c
gcc -c Stack.c
.o
gcc *.o
.c
.o
... Multi-module C Programs and Makefiles | 39/78 |
Within a large software system ...
main.o
main.c
Stack.h
main.o
main.c
Stack.h
gcc
main.c
Stack.h
main.o
gcc
Makefile
The make
Makefile
... Multi-module C Programs and Makefiles | 40/78 |
Example dependencies and actions
... Multi-module C Programs and Makefiles | 41/78 |
Example Makefile
main : main.o Stack.o gcc -o main main.o Stack.o main.o : main.c Stack.h gcc -c -Wall -Werror main.c Stack.o : Stack.c Stack.h gcc -c -Wall -Werror Stack.c
Legend: target, source, action
Note: The lines with actions MUST begin with a tab (not spaces).
... Multi-module C Programs and Makefiles | 42/78 |
Example Makefile
CC = gcc CFLAGS = -Wall -Werror main : main.o Stack.o main.o : main.c Stack.h Stack.o : Stack.c Stack.h
... Multi-module C Programs and Makefiles | 43/78 |
Example Makefile
CC = gcc CFLAGS = -Wall -Werror main : main.o Stack.o main.o : main.c Stack.h Stack.o : Stack.c Stack.h clean : rm -f *.o core main
Debugging Tools |
Debugging Tools | 45/78 |
... Debugging Tools | 46/78 |
Debugging is like detective work ...
Detecting | Debugging |
Examine the crime scence | Examine the output |
Form a hypothesis (what happened? whodunnit?) |
Form a hypothesis (what might have caused this behaviour?) |
Look for clues (to strengthen hypothesis) |
Look at code (to strengthen hypothesis) |
Gather evidence | Observe program behaviour** |
If hypothesis supported, arrest the perpetrator |
If hypothesis supported, change code to fix problem |
** A debugging tool like gdb
qtspim
GDB: The Gnu Debugger | 47/78 |
gdb
gdb
ddd
For initial debugging, gdb is very useful.
Using GDB | 48/78 |
Program must be compiled using -g
gdb
... Using GDB | 49/78 |
Executing program under gdb control:
$ gdb myProg (gdb) run < dataFile... crashes, displaying line of code (gdb) where... stack trace (gdb) list... show code around current location (gdb) print expr... display value of expression (gdb) help... documentation (gdb) quit
Basic GDB Commands | 50/78 |
quit
help
[CMD]
CMD
run
ARGS
ARGS
$ xyz < data
is acheived by
(gdb) run < data
GDB Status Commands | 51/78 |
where
up
[N]
list
[LINE]
print
EXPR
EXPR
a@1
a
GDB Execution Commands | 52/78 |
break
[FUNC|LINE]
next
step
continue
gdb
Exercise 3: Monitoring Program Execution | 53/78 |
Use GDB to examine the execution of the following:
fac0.c
fac.c
List.c
valgrind | 54/78 |
valgrind
-g
Can be run like:
$ valgrind ./a.out
Or for more information about memory leaks:
$ valgrind --leak-check=full ./a.out
Exercise 4: Finding Memory Leaks | 55/78 |
Use valgrind to examine the execution of the following:
testList.c and List.c
C Revisited |
What (I assume) You Know | 57/78 |
Given a problem specification ...
==
!
<=
&&
if
while
for
break
scanf()
printf()
return
*.h
*.c
malloc()
free()
Fine Control | 58/78 |
Good programming style often dictates ...
if
return
return
main()
break
continue
return
exit
assert
... Fine Control | 59/78 |
Example: scan a[N]
for (i = 0; i < N; i++) { if (a[i] == 0) break; if (a[i] < 0) continue; sum += a[i]; }/* vs */ for (i = 0; i < N && a[i] != 0; i++) { if (a[i] > 0) { sum += a[i]; } }
Type Definitions | 60/78 |
Reminder: you can give a name to a C type definition e.g.
typedef int Integer; typedef long long int BigInt; typedef unsigned char Byte; typedef struct { int x; int y; } Coord;
and then use the name instead of the type definition e.g.
Byte byte;// one 8-bit variable Coord here, there;// two Coord variables
Assignment as Expression | 61/78 |
Assignments can be treated as expressions returning a value
x = 5;// returns 5
Can be useful
x = y = z = 0;// equiv to x = (y = (z = 0))
Can be dangerous
if (x = 0)// "test" always fails
... Assignment as Expression | 62/78 |
Assignment-as-expression often used to simplify loops, e.g.
// read stdin one char at-a-time while ((ch = getchar()) != EOF) {// do something with ch }
rather than
ch = getchar(); while (ch != EOF) {// do something with ch ch = getchar(); }
Exercise 5: Assignment as Expression | 63/78 |
In the example above, we wrote:
// read stdin one char at-a-time while ((ch = getchar()) != EOF) {// do something with ch }
which reads a char into ch
EOF
What would the following code do?
// read stdin one char at-a-time while ( ch = getchar() != EOF) {// do something with ch }
Hint: Check precedence table in the C Reference Card or by typing the command
man 7 operator
Ignoring Expression Results | 64/78 |
We usually ignore the value returned by an assignment.
Some C functions return a result which is usually ignored. These are usually
main()
Ignored return error statuses should be
... Ignoring Expression Results | 65/78 |
printf()
man 3 printf
int x = 123, y = 42; printf("%d %d\n", x, y);
Exercise 6: printf | 65/78 |
What value does the above printf()
Switch-statements | 66/78 |
switch
if (v == C1) { S1; } else if (v == C2) { S2; } ... else if (v == Cn) { Sn; } else { Sn+1; }
|
switch (v) { case C1: S1; break; case C2: S2; break; ... case Cn: Sn; break; default: Sn+1; }
|
break
... Switch-statements | 67/78 |
if (colour == 'r') { printf("Red"); } else if (colour == 'g') { printf("Green"); } else if (colour == 'b') { printf("Blue"); } else { printf("Invalid Colour"); }
|
switch (colour) { case 'r': printf("Red"); break; case 'g': printf("Green"); break; case 'b': printf("Blue"); break; default: printf("Invalid Colour"); }
|
Exercise 7: Displaying Months | 68/78 |
Write a function monthName(int)
switch
Conditional Expressions | 69/78 |
Encapsulates a common usage for an if
if (cond) { v = Expr1; } else { v = Expr2; }
can be written as
v = cond ? Expr1 : Expr2;
Examples:
x = (y > 0) ? z+1 : z-1;or x = z + ((y > 0) ? 1 : -1);
Exercise 8: Conditionals | 70/78 |
For each of the following:
// (a) if (x > 0) y = x - 1; else y = x + 1; // (b) if (x > 0) y = x - 1; else z = x + 1; // (c) if (x > 0) { y = x - 1; z = x + 1; } else { y = x + 1; z = x - 1; }
Pointer arithmetic | 71/78 |
Pointers can "move" from object to object by pointer arithmetic
For any pointer T *p;
p++
p
sizeof(
)
Examples (assuming 16-bit pointers):
char *p = 0x6060; p++; assert(p == 0x6061) int *q = 0x6060; q++; assert(q == 0x6064) double *r = 0x6060; r++; assert(r == 0x6068)
A common (efficient) paradigm for scanning a string
char *s = "a string"; char *c;// print a string, char-by-char for (c = s; *c != '\0'; c++) { printf("%c", *c); }
Exercise 9: Sum an array of int | 72/78 |
Write a function
int sumOf(int *a, int n) { ... }
to sum the elements of array a[]
n
Implement it two ways:
Function Pointers | 73/78 |
In C you may point to anything in memory.
... Function Pointers | 74/78 |
Syntax of declaring a function pointer:
return_t (*var) (arg_t, ...)
Examples of declaring a function pointer:
// variable fp is a pointer to a function with // one int parameter and an int return value int (*fp) (int); // variable fp2 is a pointer to a function with // a char and an int parameters and a void return value void (*fp2) (char, int);
... Function Pointers | 75/78 |
Examples of use:
int square (int x) { return x*x; } int timesTwo (int x) { return x*2; } int (*fp) (int); //Point to the square function and use it fp = □ int n = (*fp)(10); //It also works without the '&' fp = timesTwo; n = (*fp)(2); //Normal function notation also works n = fp(2);
... Function Pointers | 76/78 |
Can traverse a collection such as an array, applying the function to all values
void traverse(int len, int a[], int (*f)(int)){ for(int i = 0; i < len; i++){ a[i] = f(a[i]); } } int main(void){ int a[3] = {1,2,3}; traverse(3,a,square); traverse(3,a,timesTwo); return 0; }
Exercise 10: Understanding Function Pointers | 77/78 |
What will the following code do?
int mystery(int a, int b, int (*fn)(int,int)) { return (fn(a,b)); } int gcd(int a, int b) { int c; while ( a != 0 ) { c = a; a = b%a; b = c; } return b; } int sumofsquares(int x, int y) { return (x*x + y*y); } int sum(int x, int y) { return (x + y); } int main(){ int n = mystery(8,12,sum); printf("%d \n", n); printf("%d \n", mystery(8,12,gcd)); printf("%d \n", mystery(8,12,sumofsquares)); return 0; }
Exercise 11: Using Function Pointers | 78/78 |
Write a function that:
int decrement(int)
int doublify(int)
int factorial(int)
int fibonacci(int)
Produced: 20 Aug 2019