Introduction to CS 301
CS 301 Lecture, Dr. Lawlor, 2005/09/02
Pass out course syllabus. Hit the high points.
The key question to answer at the start of this class is this:
Why would anybody ever want to learn assembly language?
I.e., why not just use a compiler?
- Assembly is a CS degree requirement. An honest answer! But this only begs the question: "Why is it a degree requirement?".
- Compiler generation. You need to know assembly to *write* a
compiler, or at least a compiler that generates code that will run on
the real machine.
- Help debugging. Being able to understand assembly lets you see
what's really going on when something goes wrong, which at least will
help you find the problem, andcan sometimes help you fix it.
- Help optimize code, both by directly writing assembly, and by
understanding what the machine does underneath. Almost nobody
writes lots of code in assembly anymore, but anybody--using any
language--can write faster code if they know what the machine has to do in order to run it.
- Run on tiny machines. How can you run your code in (I kid
you not) 128 bytes of RAM? Silly answer: very carefully! A
compiler may use more time and space than you have on the machine. Tiny
machines, like the PIC microcontroller, are used inside cell phones, calculators, microwaves, toasters, watches, toys, etc.
- To do things other languages won't let you you do, like access special instructions. Often this sort of assembly can be turned into small macros or inline
functions, and then called from other languages like C. Examples of
these "assembly wrapper" headers include Intel's <mmintrin.h>
To work around problems in existing *compiled* code, when you
don't even have source code, you've got to mess with the machine code,
often inside a hex editor. This is a good argument for using
open-source code instead of proprietary binary code, but there's a lot
of proprietary code out there being used, and there are times when you
really have to fix a bug now.
- Find first set bit in an integer is a single instruction on most machines, but can't be accessed from C.
- Overflow checking, used for multi-precision arithmetic, exists everywhere, but not in C.
- Memory atomicity and locking instructions, used for efficient shared-memory multiprocessing, are not in C.
To enable bizarre new functionality by generating code at runtime (e.g., Java Just-In-Time (JIT) compilation, ARB_fragment_program).