CS270 Colorado State University ==================== High-Level Languages ==================== We talk about the meaning of a program fragments as if they are executed in their source form. This is useful and practical, but not true. x = x+1; - in source form is a piece of text sitting in a .c file. - when executed some data container (a memory location or a register) gets incremented, but there is no variable x to speak of. In order to see why we can talk "symbolically", we must understand compilation, linking, loading and executing a program. C is a symbolic language, i.e.: entities (variables (primitive, array, string, user defined structures, functions) in a program are referred to by their name (an identifier) It is up for discussion whether C is a High Level Language, if it is, it is the lowest high level language around (higher than assembly language). High level languages were invented for one reason: to simplify programming. Without them we would have to write in machine or assembly language. Compilation versus Interpretation ============== Compiling C ============== Most of the time a program consists of a number of source files which refer to each other, and which call system functions. A C source gets compiled into machine language, replacing symbolic names for addresses and replacing loops and conditionals by tests and gotos in three major steps: from SOURCE PROGRAM to ASSEMBLY PROGRAM from assembly program to OBJECT FILE from a number of object files to an EXECUTABLE LOAD file A C, or any other, program on its own cannot execute: it uses all kinds of library functions (e.g. to read command line arguments, to get the time of day in milliseconds, or to do input / output it calls functions in other C programs Every source file gets compiled into an object file, then a number of object files and a number of library object files are LINKED together into an executable load file. source 1 \ / object 1 \ + lib object 1 \ source 2 > COMPILE < object 2 \ + lib object 2 > LINK into load file source 3 / \ object 3 / + lib object 3 / The compiler create object files out of souce code files. All these are compiled independently, e.g.they all start at memory location 0, and they refer (still symbolically) to library functions and data and / or functions in other source files. Here is a breakdown of the different steps in the compilation process: Preprocessor (cpp, gcc -E) #include #define See for yourself % gcc -E LC3assem.c > LC3assem.i Compiler (gcc -S) Assembler (as, gcc -c) gcc -c takes as input C code and generates object code, or object module Linker (ld) Loader ============== C Memory Model ============== --------- Variables name of a location in which to store a value types symbol table entry ---------- Scope Where is the variable accessible within the C program itself? Two main scopes: global and local Example with block scoping -------------- LC3 memory map System space Program text Global data section Heap Stack System Space --------------- Bone-headed C overloading of keyword static ====================== Literals, or Constants ====================== int, float, and char in C Constants in assembly ------------------------ mstrout@cs.colostate.edu, 10/29/08 Sources for these notes include Wim's notes, CS 30 notes, and slides that go with book.