Docsity

Why Study Low level Programming - Lecture Slides | CMSC 212, Study notes of Computer Science

Toggle sidebar
bg1
1
1
1
CMSC 212 –F05 (lect 26)
Announcements
●Program #6
– is due Today
●Final
–Thur sday, Dec. 15 4-6 in H.J. Patterson 0226
2
CMSC 212 –F05 (lect 26)
Review Lecture
●Final covers all material from the class
– Comprehensive
– relatively equally weighted between 1st, 2nd and after 2nd
exam
●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!
3
CMSC 212 –F05 (lect 26)
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)
4
CMSC 212 –F05 (lect 26)
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 there
5
CMSC 212 –F05 (lect 26)
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 compiler
6
CMSC 212 –F05 (lect 26)
Make: A Tool to Compile Programs
●Problem: Compiling programs is tedious
●
●   
  
   !
"# $
% 
● &! 
Adapted from: www.cs.tau.ac.il/~dcor/Software1/Makefile.ppt
pf3
pf4
pf5

Partial preview of the text

Download Why Study Low level Programming - Lecture Slides | CMSC 212 and more Study notes Computer Science in PDF only on Docsity!

CMSC 212 – F05 (lect 26) 1

Announcements

l Program

  • is due Today

l Final

  • Thur sday, Dec. 15 4-6 in H.J. Patterson 0226

CMSC 212 – F05 (lect 26)^2

Review Lecture

l Final covers all material from the class

  • Comprehensive
  • relatively equally weighted between 1st, 2nd^ and after 2nd exam

l 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!

CMSC 212 – F05 (lect 26)^3

Why Study Low-level Programming?

and Why Study the C language?

l Provides Understanding of How Things Work

  • Compilers, Processors, Data Structures

l Allows access to Hardware when required

l 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

l Widely Used for System Programming

l “Middle Aged” Language – created in late 1970’s

l C’s design goals Goals

l Major Differences from JAVA

  • Explicit Memory Allocation and Deallocation
  • Procedural rather than object oriented
  • Compiles directly to machine code (rather than byte codes)

CMSC 212 – F05 (lect 26) 4

Phases of Compilation

l 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

l translation (compilation)

  • source to object code
  • of individual files
  • making sure prototypes match definitions and calls

l linking

  • combining object files
  • making sure all parts are there

CMSC 212 – F05 (lect 26)^5

Readability, Reliability and Efficiency

of Your program

l Readability

  • Comments
    • Should be surrounded by /* and */
    • May span multiple lines
  • White Space
    • vertical and horizontal

l 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

l Efficiency

  • Code motion
  • Loop Unrolling
  • Dead-Code Elimination
  • human changes vs -O option on compiler

CMSC 212 – F05 (lect 26)^6

Make: A Tool to Compile Programs

l Problem: Compiling programs is tedious

l    

l          

l         & ! 

Adapted from: www.cs.tau.ac.il/~dcor/Software1/Makefile. ppt

CMSC 212 – F05 (lect 26) 7

Stored Program Computer

l A Program:

  • Consists of a sequence of operations
  • Is loaded into a computer’s memory to execute

l 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

l Program and data are both stored in memory

l Completeness of Instructions

CMSC 212 – F05 (lect 26)^8

What do Instructions Look Like?

l For Project #1, instructions:

  • are 32 bits long
  • contain three parts

l 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 functions

Purpose Opcode Register 1 Register 2 Register 3 Memory

Size (bits) 4 4 4 4 16

CMSC 212 – F05 (lect 26)^9

Function Arguments

l Comma separated list of values

l 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

l Can pass address of variable to change values

int foo(int *a) { *a = 3; }

int x; foo(&x);

CMSC 212 – F05 (lect 26) 10

struct, union, and typedef

l Often useful to use typedef and struct together:

typdef struct { int a; char b; } Simple;

l Declarations then look like:

Simple x; Simple y[20], z;

l 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.

l Unions

  • Rather than storing each field, only stores one of the fields
  • Syntax: union tag { member-list } variable-list

CMSC 212 – F05 (lect 26)^11

Bit Fields

l 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;

l 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;

CMSC 212 – F05 (lect 26)^12

Bit Shift Operator

l C has operators to bit shift numbers

  • number of bits changed depends on size of variable

l 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

l 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 dependent

CMSC 212 – F05 (lect 26) 19

Pointers

l Can declare any variable to be of type pointer

  • are similar to references in Java
  • use * before variable name to create a pointer to the type
    • int *foo;
    • myTypeDef *myPtr;
    • float *bar;
  • The type determines both:
    • what is pointed to
    • the size of the object being pointed to

l Initialization

  • creating a pointer doesn't create the space it points to
  • int *foo;
  • foo = 3; / error: haven't defined what it points to */

CMSC 212 – F05 (lect 26)^20

Type Conversion

l C lets you assign variables of different types

  • called type conversion
  • useful when dealing with
    • external devices
      • int *fooDev = (int *) 100;
      • we know the foo device is at location 100
    • when type information read from a file is based on file contents.

l WARNING: This can be very dangerous

l (type *) variable:

  • int *x;
  • float *y;
  • x = (int *) y;

CMSC 212 – F05 (lect 26)^21

Pointer Aliases

l Sometimes two pointers point to the same thing\

  • This can be useful
  • This can cause problems: int p, q; int a= 89; p = &a; q = p; free(p); if (q) { … } / error: *q points to freed space */

89

p q

a

CMSC 212 – F05 (lect 26) 22

Heap Memory Management

l Allocating memory:

  • void *malloc(size_t requestedSize);
    • allocate requestedSize bytes of memory
  • void *calloc(size_t requestCount, size_t objectSize);
    • allocate requestCount objects of size objectSize bytes each
    • set the requested memory to zero
  • Type Information
    • malloc/calloc return void *
    • need to convert to desired type using cast operator

l free(void *p);

  • returns space pointed to by p
  • does not change p
    • good idea p=NULL; right after deallocation.

CMSC 212 – F05 (lect 26)^23

Linked Data Structures

l Singly Linked Lists

l Doubly Linked Lists

l Trees

l Hash

  • simple
  • linked

l Graphs

  • Directed vs. undirected
  • Cyclic vs Non-Cyclic
  • DAGS – directed acyclic graphs

l How they are stored

l Algorithms for traversal

CMSC 212 – F05 (lect 26)^24

Graphs - Directed

l Examples

  • List of web pages that link to each other

l Nodes can

  • have an arbitrary number of out arcs
  • have an arbitrary number of inbound arcs

l Declarations

typedef struct ARC { struct NODE *nodePtr; struct ARC *next; } Arc; typedef struct NODE { Arc *outArcs; int value; } Node;

CMSC 212 – F05 (lect 26) 25

Number Representation

l Integers - 2's complement representation

  • To compute a negative value:
    • flip all the bits of the positive value, add 1
  • Allows addition of signed and unsigned numbers
  • Valid range of numbers
    • -2n-1^ to 2n-1-1 for n bits
    • Example: 16 bit number -32,768 to 32,

l Real numbers

  • Each number has two parts
    • mantissa (represents a number between -1 and 1)
    • exponent (designates the position of the decimal point)
    • number = m r e^ where r is the radix
  • Normalization
    • convert to number between -1 and 1
    • if the most significant digit of mantissa is non-zero.

CMSC 212 – F05 (lect 26)^26

Floating Point Continued

l Computers normally use a radix of 2

l Examples of floating point numbers

  • 1001.11 = .1001110 x 2^4
  • 10.5 = 1010.1 or 10101 x 2^4
  • 17.451 = 111.011100 = 1110111 x 2^3

l IEEE Floating point standard

  • 32 bit floating point (float)
    • 1 sign bit, 8 bits exponent, 23 bits mantissa
  • 64 bit floating point (double)
    • 1 sign bit, 11 bits exponent, 52 bits mantissa
    • most common for real applications
  • 128 bit floating point (quad)
    • 1 sign bit, 15 bits exponent, 112 bits of mantissa

CMSC 212 – F05 (lect 26)^27

Callbacks

typedef int (*compareFunc)(item *a, item * b);

int searchTree(Node *root, Item target, compareFunc cmp) { if (!root) return -1; ret = (cmp)(target, root->data); if (ret == 0) { / found it */ return 1; } else if (ret < 0) { return searchTree(root->left, target, cmp); } else { return searchTree(root->right, target, cmp); } }

CMSC 212 – F05 (lect 26) 28

Programming Command Line Arguments

l Generally, want arguments to be accepted in any order

  • ./foo -file myfile -help
  • ./foo -help -file myfile

l Solution:

  • Table driven command line parsing
    • Example of a common pattern, table drive parsing
  • Declare a structure
    • fields describe each valid option typedef enum { Flag, StrParam, IntParam }; typedef struct { char *name; optionTypes options; void *parameter; } option;

CMSC 212 – F05 (lect 26)^29

Simple Data Structure for Heap

l Divide heap into blocks

l Each Block has a 32 bite header

  • 29 bits define size of block (in 8 byte chunks)
  • 1 bit is for free/allocated (1 indicates allocated)
  • 2 bits are un-used

l Can find next block if know the current block

  • add address of current block header to size of block

8/0 16/1 24/0 8/1 24/1 0/

CMSC 212 – F05 (lect 26)^30

Garbage Collection

l What is Garbage?

  • memory the program can't get to anymore
  • Example:
    • ptr = malloc(40);
    • ptr = NULL;

l How to detect Garbage?

  • Need to know what is a pointer
    • know what everything is (Java)
    • guess anything that looks like a pointer is one (C)
      • any 32-bit quantity in globals, on stack, is assumed to be a pointer.
      • anything in a memory region pointed to
  • Compute reachability graph
    • Follow all pointers