111CMSC 212 – F05 (lect 28)AnnouncementsProgram #6– due TODAYFinal– Saturday, May 12 4-6 in PHY 14122CMSC 212 – F05 (lect 28)Review LectureFinal covers all material from the class– Comprehensive– relatively equally weighted between 1st, 2ndand after 2ndexamToday's lecture is set of highlights– Be aware that there are things not in today's review which will likely also be on the final!223CMSC 212 – F05 (lect 28)Why Study Low-level Programming?and Why Study the C language?Provides Understanding of How Things Work– Compilers, Processors, Data StructuresAllows access to Hardware when requiredAllows efficiency and control– Writing device drivers and operating systems– If you are careful and good it will be faster– BUT could also be Dangerous or SlowerWidely Used for System Programming“Middle Aged” Language – created in late 1970’sC’s design goals GoalsMajor Differences from JAVA– Explicit Memory Allocation and Deallocation– Procedural rather than object oriented– Compiles directly to machine code (rather than byte codes)4CMSC 212 – F05 (lect 28)Phases of Compilation Preprocessor– Not everything is in one file• Definitions of routines are in separate files– Function Prototypes• Define name, parameters, and return types– #include, #if, #endif, #ifndef– macros, constants, conditional compilationtranslation (compilation)– source to object code– of individual files– making sure prototypes match definitions and callslinking– combining object files– making sure all parts are there335CMSC 212 – F05 (lect 28)Readability, Reliability and Efficiency of Your programReadability– Comments• Should be surrounded by /* and */• May span multiple lines– White Space• vertical and horizontalTesting– Write Tests as you write software– When the software is “done”, tests continue as part of it– Types of Testing• line coverage, white box and black boxEfficiency– Code motion– Loop Unrolling– Dead-Code Elimination– human changes vs -O option on compiler6CMSC 212 – F05 (lect 28)Make: A Tool to Compile ProgramsProblem: Compiling programs is tediousSolution: Automate it!Done in Unix by the make command– Uses a file called Makefile• A makefile is a file (script) containing :– Project structure (files, dependencies)– Instructions for files creationMake is not limited to C programsAdapted from: www.cs.tau.ac.il/~dcor/Software1/Makefile.ppt447CMSC 212 – F05 (lect 28)Stored Program ComputerA Program:– Consists of a sequence of operations – Is loaded into a computer’s memory to executeStandardize set of allowed operations, called Instructions– each instruction performs a specific operation– Instructions can • read or change memory• compute a value• read or write output– What instruction to execute next:• normally it is the next instruction• provide control to select alternate next instructionProgram and data are both stored in memoryCompleteness of Instructions8CMSC 212 – F05 (lect 28)What do Instructions Look Like?For Project #1, instructions:– are 32 bits long– contain three partsExtension Later – Maintaining the Stack– Calling Functions• for calls to the same function from other places in the program• for recurrsion– parameter passing to functions– return values of functionsPurpose Opcode Register1Register2Register3MemorySize (bits) 4 4 4 4 16559CMSC 212 – F05 (lect 28)Function ArgumentsComma separated list of valuesAll parameters are passed by value– called function can modify values– arrays are passed by the address of their 0th element• modifying elements of array seen by calling functionCan pass address of variable to change valuesint foo(int *a) {*a = 3;}int x;foo(&x);10CMSC 212 – F05 (lect 28)struct, union, and typedefOften useful to use typedef and struct together:typdef struct {int a;char b;} Simple;Declarations then look like:Simple x;Simple y[20], z;Header Files– If structure used by more than one file, put it in a header file.– Use #include to include definition in each file where used. Unions– Rather than storing each field, only stores one of the fields– Syntax: union tag { member-list } variable-list6611CMSC 212 – F05 (lect 28)Bit FieldsPart of the Power of C is control over data layout– If you need a field with exactly 13 bits, you can define it– Useful for:– defining fields that interact with hardware– managing pre-defined file formats– saving every last bit of space– Syntax: type variable:size;Examples:int foo:13;unsigned int foo:4;typedef struct { /* from project #1 */unsigned int opCode:4;unsigned int r1:4;unsigned int r2:4;unsigned int r3:4;unsigned int address:16;} instruction;12CMSC 212 – F05 (lect 28)Bit Shift OperatorC has operators to bit shift numbers– number of bits changed depends on size of variableLeft Shift (number << xxx)– Move each bit of number to the left by xxx bit positions– Leftmost xxx bits discarded– Rightmost xxx bits gets 0Right Shift (number >> xxx)– Move each bit of number to the right by xxx bit positions– Rightmost xxx bits discarded– Leftmost xxx bits gets 0 or replicate sign bit• for unsigned gets 0• for signed, its implementation dependent7713CMSC 212 – F05 (lect 28)Type ConversionPromotion of char and short– in expressions, char and short are promoted to intchar a, b, c;a = b + c;• b and c are converted to int, then the sum is truncatedArithmetic Conversions– can't be performed on different types• converted to "higher" typelong doubledoublefloatunsigned long intlong intunsigned intintHighest Type14CMSC 212 – F05 (lect 28)PrecedenceThere is a set of rules about precedence of operator– Most important precedence rule:• When in doubt, put in () to ensure correct order– Order (highest to lowest):• ()• Function call, subscript, postfix increment/decrement• rest of unary operators• type conversion• Arithmetic operators• Relational operators• Bit operators• Assignment operators• , (comma operator)8815CMSC 212 – F05 (lect 28)Standard I/O Library#include <stdio.h>– includes prototypes for standard I/O routinesMost I/O is stream based– buffered to allow efficient operations • mostly to disks or networks– interactive I/O is normally flushed at useful points• printf("foo\n"); /* \n causes a flush
View Full Document