Unformatted text preview:

111CMSC 212 – F05 (lect 28)AnnouncementsProgram #6– due TODAYFinal– Saturday, May 12 4-6 in PHY 14122CMSC 212 – F05 (lect 28)Review LectureFinal covers all material from the class– Comprehensive– relatively equally weighted between 1st, 2ndand after 2ndexamToday'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 StructuresAllows access to Hardware when requiredAllows 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 SlowerWidely Used for System Programming“Middle Aged” Language – created in late 1970’sC’s design goals GoalsMajor 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 compilationtranslation (compilation)– source to object code– of individual files– making sure prototypes match definitions and callslinking– combining object files– making sure all parts are there335CMSC 212 – F05 (lect 28)Readability, Reliability and Efficiency of Your programReadability– Comments• Should be surrounded by /* and */• May span multiple lines– White Space• vertical and horizontalTesting– 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 boxEfficiency– Code motion– Loop Unrolling– Dead-Code Elimination– human changes vs -O option on compiler6CMSC 212 – F05 (lect 28)Make: A Tool to Compile ProgramsProblem: Compiling programs is tediousSolution: 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 creationMake is not limited to C programsAdapted from: www.cs.tau.ac.il/~dcor/Software1/Makefile.ppt447CMSC 212 – F05 (lect 28)Stored Program ComputerA Program:– Consists of a sequence of operations – Is loaded into a computer’s memory to executeStandardize 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 instructionProgram and data are both stored in memoryCompleteness of Instructions8CMSC 212 – F05 (lect 28)What do Instructions Look Like?For Project #1, instructions:– are 32 bits long– contain three partsExtension 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 ArgumentsComma separated list of valuesAll 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 functionCan pass address of variable to change valuesint foo(int *a) {*a = 3;}int x;foo(&x);10CMSC 212 – F05 (lect 28)struct, union, and typedefOften 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 FieldsPart 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 OperatorC has operators to bit shift numbers– number of bits changed depends on size of variableLeft Shift (number << xxx)– Move each bit of number to the left by xxx bit positions– Leftmost xxx bits discarded– Rightmost xxx bits gets 0Right 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 ConversionPromotion 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 truncatedArithmetic Conversions– can't be performed on different types• converted to "higher" typelong doubledoublefloatunsigned long intlong intunsigned intintHighest Type14CMSC 212 – F05 (lect 28)PrecedenceThere 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 routinesMost 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

UMD CMSC 212 - Lecture Slides

Download Lecture Slides
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Slides and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Slides 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?