The Language Hierarchy: electrons to web
CS 301:
Assembly Language Programming Lecture, Dr. Lawlor
You can think of a computer as running very simple low-level
operations like moving electrons, or as running very high-level
operations like looking at a web page. The appropriate level
depends on what you're trying to achieve, but it's useful to know at
least a little about all the levels.
- Web user interface, where you can click and type
- JavaScript code, which mostly moves around JavaScript Object
Notation (JSON) or Document Object Model (DOM) elements on a web
page or across the network
- JavaScript engine, like Google's v8, written in C++ to
interpret JavaScript fast by converting it to machine code
- C++, the main language used in our CS program
- C, the simpler predecessor of C++
- Assembly language, human-readable version of machine code
- Machine code, raw binary CPU control, as discussed here
- CPU transistors, as discussed in EE 341 (Digital Circuits),
and EE 443 or CS 441 (Architecture)
- Individual electrons, as discussed in Physics 212
- Quantum wavefunctions, as directly manipulated by a quantum
computer (currently either small, specialized, or theoretical!)
Field guide: C versus C++
Plain C is a
simple, low-level language, and it's nearly a subset of C++, which
was originally named "C with classes". If you look at the
size of the author's book on each language, you can immediately
see the big complexity difference!
Generally,
these features are shared between C and C++:
- Variables like "int" and "char"
- Arithmetic operations like + - * /
- Assignments like =, or += and such.
- Functions, curly braces {}
- if statements, switch statements
- for loops, while loops, do loops
- arrays
- pointers
These features
are only C++, not available in plain C:
- class, constructor, destructor, inheritance, or virtual
methods. Plain C uses functions or structs for similar
operations.
- new and delete. Plain C uses functions named malloc and
free instead.
- templates. Plain C uses macros for similar features.
- std::string. Plain C uses arrays of characters for
strings, which are more error-prone.
- std::cout and std::cin. Plain C uses printf and scanf,
which I actually prefer, but are harder to learn, and can't be
extended.
- std::vector. Plain C uses bare arrays, and a function
named realloc.
- std:: anything else!
In this class,
we'll be covering both the implementation of C++ features in plain
C and assembly, and plain C as a language on its own. Quite
a few large projects are built in plain C, including the linux
kernel, and many system libraries.
Field guide: assembly versus machine code
Machine code is the raw binary data that the CPU executes. The
individual bits of machine code are the actual inputs to the
transistors in the CPU's control system that make the CPU eventually
(many stages later!) compute stuff.
Assembly language is a human-readable version of machine code.
If you compile a simple function that just does "return 7;", you can
see assembly language instructions "mov" and "ret", and the binary
machine code that the CPU actually runs. (Hit the "Disassemble"
checkbox on NetRun to see this yourself; we'll be covering this
starting next week.)
0000000000000000 <foo>:
0: b8 07 00 00 00 mov eax,0x7
5: c3 ret
Machine Code (in hex) Assembly Language