Docsity

Introduction to Low Level Programming - Lecture Slides | CMSC 212, Study Guides, Projects, Research of Computer Science

Toggle sidebar
bg1
1
1
1
CMSC 212 โ€“ S07 (lect 1)
Introduction to Low-Level Programming
๎˜
Review Syllabus
๎˜
Class Submit Server & Grades Servers
โ€“ submit.cs.umd.edu
โ€“ grades.cs.umd.edu
๎˜
Program #1 on the web page on Monday
โ€“ not a long project
โ€“ to help you get used to the language and environment
๎˜
Discussion Sections
โ€“ new material (outside of lecture)
โ€“ hands-on practice and projects
โ€“ quizzes
๎˜
Reading
โ€“ Chapters (C) 1 & 2 (by Tuesday)
โ€ข Skip 1.1.4
2
CMSC 212 โ€“ S07 (lect 1)
What is โ€œlow-levelโ€ Programming
๎˜
Possible Definitions
โ€“ Low-level languages are closer to the hardware than are high-
level programming languages, which are closer to human
languages. (www.webopedia.com)
โ€“ Computer languages are classed a low-level if each instruction
specifies only one operation of the computer, or high-level if
each instruction may specify a complex combination of
operations. (onlinedictionary.datasegment.com)
โ€“ Programming where details and intricacies of the hardware are
visible
๎˜
The C Language
โ€“ lower than Java (need to manage memory)
โ€“ higher level than assembly
pf3
pf4
pf5

Partial preview of the text

Download Introduction to Low Level Programming - Lecture Slides | CMSC 212 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CMSC 212 โ€“ S07 (lect 1)^1

Introduction to Low-Level Programming

 Review Syllabus  Class Submit Server & Grades Servers

  • submit.cs.umd.edu
  • grades.cs.umd.edu  Program #1 on the web page on Monday
  • not a long project
  • to help you get used to the language and environment  Discussion Sections
  • new material (outside of lecture)
  • hands-on practice and projects
  • quizzes  Reading
  • Chapters (C) 1 & 2 (by Tuesday)
  • Skip 1.1.

CMSC 212 โ€“ S07 (lect 1)^2

What is โ€œlow-levelโ€ Programming

 Possible Definitions

  • Low-level languages are closer to the hardware than are high- level programming languages, which are closer to human languages. (www.webopedia.com)
  • Computer languages are classed a low-level if each instruction specifies only one operation of the computer, or high-level if each instruction may specify a complex combination of operations. (onlinedictionary.datasegment.com)
  • Programming where details and intricacies of the hardware are visible

 The C Language

  • lower than Java (need to manage memory)
  • higher level than assembly

CMSC 212 โ€“ S07 (lect 1)^3

Why Study Low-level Programming?

 Provides Understanding of How Things Work

  • Compilers, Processors, Data Structures

 Allows access to Hardware when required

  • Writing device drivers
  • Creating Operating Systems

 You are in Control

  • Can be faster
    • If you are careful and good
  • Can be slower
    • If you rewrite low level routines where faster ones exist
  • Can be Dangerous โ€“ easier to have software that
    • Crashes program (or OS)
    • Fails in odd ways (memory corruption)

CMSC 212 โ€“ S07 (lect 1)^4

The โ€œCโ€ Programming Language

 Widely Used for System Programming

  • Writing Operating Systems and Compilers

 โ€œMiddle Agedโ€ Language โ€“ created in late 1970โ€™s

  • Reflects lessons learned from Fortran, COBOL

 Goals

  • Designed to allow low-level programming
  • Small language
    • Syntax is relatively simple
    • Libraries provided but optionally used
  • Assumes you know what you are doing

 Major Differences from JAVA

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

CMSC 212 โ€“ S07 (lect 1)^7

Preprocessor

 function prototypes

  • Define name, parameters, and return types
  • Sort of like how methods are defined within class definitions
  • example int Funct1(int x, int y);

 #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 individually to object code
    • And then linked together to make executable code

 Two ways to use

  • #include <stdio.h>
  • #include โ€œswap.hโ€

CMSC 212 โ€“ S07 (lect 1)^8

Formatting Your program

 Comments

  • Must be surrounded by /* and */
  • May span multiple lines
    • Common bug is to forget to close a comment
    • Can not be nested
  • // until end of line
    • is acceptable per ANSI C99 standard
    • May want to avoid using it, older compiler donโ€™t accept it

 Vertical White Space

  • Blank lines before a new function definition
  • Blank lines within a function to separate different tasks

 Horizontal White Space

  • Indentation
  • Generally corresponds to nesting level

CMSC 212 โ€“ S07 (lect 1)^9

Basic C Syntax

 Variable Declarations

  • Format:
    • type variableName1, variableName2;
  • Examples
    • int a;
    • float x, y;
    • int m = 8;
  • Must be before any executable code
  • May be global (whole file) or specific to a function

 Looping and Conditionals

  • while, do, for
  • if, switch

 Function Calls

  • FunctName(argument1, argument2, โ€ฆ)

 Block of code

CMSC 212 โ€“ S07 (lect 1)^10

printf Function

 Definition

  • included with #include <stdio.h>
  • then just use it because it has already been defined for you

 Syntax:

  • printf(formatString);
  • printf(formatString, variable1, variable2, variabe3, ..);

 Within Format String:

  • Defines constants to print, or format for variables:
    • %d Print an integer in decimal
    • %x Print an integer in hex
    • %f Print a floating point value
    • %c Print a character
    • %s Print a character string
    • \n Print a newline

 Example assuming int a; and a has the value 23

  • code: printf(โ€œ%d is smaller than %d\nโ€, a, a+5);
  • output: 23 is smaller than 28

CMSC 212 โ€“ S07 (lect 1)^13

Compiling & Running a Program

 Programs must be compiled to execute

 Compiler is invoked as:

  • gcc
  • Common Options:
    • -g Enable Debugging
    • -Wall Warn about common errors
    • -o Filename for the executable
    • -c Only compile to object code
    • -fprofile-arcs Count how often statements run
    • -ftest-coverage Test program coverage
  • A simple command line:
    • gcc โ€“g โ€“Wall โ€“o prog1 prog1.c tools.c

 Make

  • covers this for you