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

Material Type: Notes; Professor: Hollingsworth; Class: INTRO TO LOW-LEVEL PROG; Subject: Computer Science; University: University of Maryland; Term: Spring 2005;

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-od4
koofers-user-od4 🇺🇸

10 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 212 – S05 (lect 27)
Announcements
zProgram #5
is due Today
zFinal
Monday 4-6 in J. M. Patterson 3201
zPickup old assignments in my office
Midterms (including re-grades)
Programming assignments
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

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!

Announcements

z^

Program #5– is due Today

z^

Final– Monday 4-6 in J. M. Patterson 3201

z^

Pickup old assignments in my office– Midterms (including re-grades)– Programming assignments

Review Lecture

z^

Final covers all material from the class

z^

Today's lecture is set of highlights– Also, things not in today's review will likely be on the final!

The “C” Programming Language

z^

Widely Used for System Programming– Writing Operating Systems and Compilers

z^

“Middle Aged” Language – created in late 1970’s– Reflects lessons learned from Fortran, COBOL

z^

Goals– Designed to allow low-level programming– Small language

  • Syntax is relatively simple

z^

Major Differences from JAVA– Explicit Memory Allocation and Deallocation– Procedural rather than object oriented–

Compiles

directly to machine code (rather than byte codes)

Preprocessor

z^

Not everything is in one file– Definitions of routines are in separate files

z^

Function

Prototypes

  • Define name, parameters, and return types– Sort of like how methods are defined in class definitions– Example: int myFunc(int a, int b); z^

#include– provides ability to include definitions from other files– Generally only include prototypes from external files

  • Actual code is kept in a separate file• Files are generally compiled seperately

Stored Program Computer

z^

A Program:– Consists of a sequence of operations– Is loaded into a computer’s memory to execute

z^

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

z^

Program and data are both stored in memory

Completeness of Instructions

z^

Need to ensure that the computer can execute anycomputable function.– more instructions may make the machine faster

  • but not more powerful

z^

Arithmetic– add, complement (negate)– AND and NOT (provides NAND which is enough)

z^

Data movement instructions– Load from memory, Store to memory

z^

Control instructions– check values of memory or other locations– provide a way to change the next instruction to execute

z^

Input/Output Instructions

Make: A Tool to Compile Programs

z^

Problem: Compiling programs is tedious

z^

Solution: Automate it!

z^

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

z^

Make is not limited to C programs

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

ppt

Function Arguments

z^

Comma separated list of values

z^

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

z^

Can pass address of variable to change valuesint foo(int *a) {

*a = 3; } int x;foo(&x);

Bit Fields

z^

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; z^

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;

Bit Shift Operator

z^

C has operators to bit shift numbers– number of bits changed depends on size of variable

z^

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

z^

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

Precedence

z^

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• ,

Standard I/O Library

z^

#include <stdio.h>– includes prototypes for standard I/O routines

z^

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 */

z^

FILE type– used to describe an active I/O connection– three default ones supplied on entry to main:

  • stdin - an input stream for default input (often keybaord)• stdout - default output stream (often terminal window)• stderr - default output stream for errors

Strings

z^

Zero or more characters followed by NUL– not counted as part of string– string.h defines prototypes for string routines

z^

Copying Strings– size_t strlen(char const *str);

  • returns count of characters in str
    • char *strncpy(char *dst, char const *src, size_t len);
      • copy src to dst• copy until NUL in src or at most len characters• pad extra characters will NUL• Safety tip:

dst[len-1] = '\0'; to force termination

  • char *strncat(char *dst, char const *src, size_t len);
    • append src onto the end of dst• always appends NUL ('\0') to end of string

Arrays of Pointers

z^

int *api[10];– An array of 10 pointers to integers;

char *keyword[] = {

"Load","Store","Negate",…