Chapters 1-‐3 Review
Total Page:16
File Type:pdf, Size:1020Kb
Chapters 1-3 Review C is a Procedural programming language (like Ada, BASIC, C++, C#, COBOL, Fortran, Java, Pascal, Perl, Python, Visual Basic). How long has C been around? Since late 1960s/early 1970s (1969-1972) Procedural This is a type of programming language that contains a series of computational steps to be carried out; procedures / routines / functions may be called at any point in the program, including by other functions or itself. Instruction Set This is the set of instructions that is defined on a processor. Different processors have different ones of these defined on them (within the circuitry of the processor). Program This is a collection of instructions that perform some purpose, solves a specific problem. Algorithm This refers to the approach or steps taken to solve the problem, to show the logic for the program. Compiler This is the process of analyzing & translating the program from the C program language to a form the computer can understand (machine language); creates an executable. Preprocessor The first sub-step when compiling where certain “administrative” things are taken care of before any compiling takes place (for example a #define, which we’ll talk about later). So, for now, think of this sub-step as “filling in the blanks” before compiling. (.i file) Assembler The second sub-step when compiling where the programming language instructions are broken down into assembly language instructions first (.s file) and then converts each assembly language instruction into binary code, or object code (.o file). Linker The last sub-step when compiling that links or adds to the object code any other code being included with the program (from the #includes or from the other program files that are all being compiled together) when then produces the executable program. Operating Systems Unix, Linux, Windows, MacOS are examples of these. These are the programs that control the entire operation of a computer system (such as I/O, system resources, execution of programs, etc). [Unix – developed at Bell Labs in 1969 in assembly language, re-written in C in 1972 (over 90% of it written in C; makes it much more easily ported to different systems with relatively small effort).] Steps to create a program: 1. Create a file (using vim/pico/nano/gedit) and code the program program_name.c 2. Compile (using gcc) gcc program_name.c a. Preprocessor i. Before any compiling takes place, the preprocessor takes care of a few “administrative” things, for example #define items ii. Can create .i file by doing the following: gcc –E program_name.c > ppf b. Assembler i. After it compiles, an assembly language version is created ii. The assembly statements are then translated into actual machine language instructions i.e. the assembler converts each assembly language statement into binary code (object code), producing a file that ends in .o program.o iii. Can create .s file by doing the following: gcc –S program_name.c iv. Can create .o file by doing the following: gcc –c program_name.c c. Linker i. After it compiles (with no errors), the linker links whatever else is needed all together so the program is ready to be run, e.g., if the program uses other programs that were previously processed by the compiler; also, anything from the library ii. Final linked file is the executable file, called a.out iii. Can capture all the above intermediate files by typing the following: gcc –save-temps program_name.c (will create .i, .s, & .o files) 3. Debug (fix erros) a. Syntax errors – language-specific omissions/errors, e.g., missing a semi-colon where one is required b. Semantic errors – logical erros, e.g. using a variable that hasn’t been declared yet 4. Repeat steps 2 & 3 until there are no errors and it works 5. To run your compiled program, just type ./a.out and your program called a.out will be loaded into your compter’s memory & execution will be initiated; when run, each statement of the program is sequentially executed in turn a. it might pause if it is waiting for input from the user or an event e.g. a mouse click b. results, or output, are displayed to the screen or written to a file. Machine instructions (Low-level) These are the (low-level) types of instructions that the computer (processor) understands. They are not human readable; are machine dependent - only worked on the machine that it was developed for (not portable). • The first computer programs, (1940s), were in terms of binary numbers (explain 1s & 0s). The set of binary numbers used in these programs corresponded directly to specific machine language instructions and locations in memory. • Instructions are simply a pattern of bits – different patterns correspond to different commands to the machine (CPU). • Example: 16-bit instruction from the 8086 microprocessor by Intel. (Show Intel’s processors here.) 0000001111010011 where the first 6 bits represent the opcode (the computer understands that 000000 means an ADD operation), the last 6 bits represent which registers the operands are in (3 bits for each register: 010 for Bx and 011 for Ax). • These days, instruction lengths are longer (e.g. 32 bits) and their lengths differ depending on the instruction. Assembly language (Low-level) These are also low-level types of instructions that, starting in the early 1950s, allowed the programmer to use a somewhat more English-like way of writing instructions by using abbreviations, or mnemonic codes, to refer to the bit-patterns. • A special program called an assembler translates the assembly language statements into the specific machine instructions of the computer system. • The programmer still had to learn the instruction set of the particular computer system because there is a one-to-one correspondence between the assembly language instructions and the specific machine instructions. • The resulting programs were not portable to other systems because they corresponded directly to the specific instruction set that the programmer was using. (i.e. machine dependent – only worked on the machine that it was developed for) Higher-level languages These languages, like C, use even more English-like statements than assembly language. • It was Grace Hopper’s idea that programs could be written in more English-like statements than Assembly and Machine language. (Look at the people posted in the Module.) • FORTRAN was one of the first (FORmula TRANslation), developed in 1954 by IBM in California, for scientific and engineering applications. • Programmers no longer had to concern themselves with the architecture of the particular computer system; the FORTRAN instructions (statements) were far removed from the instruction set of the particular machine. • A compiler is used to accomplish this. The compiler is a program that translates the statements from the higher-level language into a form that the computer understands (machine language instructions of the computer). • This meant that programs could be written in the language to be machine independent – i.e. the programs could be run on any machine that supported the language with few or no changes. • Grace Hopper created the first compiler; and it was from her work that the COBOL programming language was developed (1959). She is given credit for coining the phrase “debugging the computer” when an actual moth was found in the Mark II computer which stalled its operation. (Pgs. 11-20, Chapter 3 – Compiling and Running Your First Program) 101/Modules/Intro/FirstProgram/prog3_1.c /* this is the very first program in the book Program 3.1, on page 11 which is explained in detail in Chapter 3 */ #include <stdio.h> int main (void) { printf("\nProgramming is fun. \n"); return 0; } /* this is the very first program in the book with an added line of output and comments Program 3.1, on page 11 which is explained in detail in Chapter 3 */ // we need stdio.h because this is where printf is defined #include <stdio.h> // main() is the entry point where the program starts execution int main (void) { /* printf is used to print "Programming is fun." to the screen, followed by a new line denoted by \n */ printf("Programming is fun. \n"); /* same line with extra "new lines" note the difference when you run the program */ printf("\I’m going to love this class! \n\n"); // return 0 to the system to indicate successful execution return 0; } /* Program 3.4, on page 15 with an added line or two of code Displaying Variables */ // we need stdio.h because this is where printf is defined #include <stdio.h> // main() is the entry point where the program starts execution int main (void) { /* declaring a variable called "sum", which will hold the value of two numbers added together */ /* a variable needs to be declared somewhere before it is used; otherwise, it would cause a semantic error */ /* this variable is of type "int", which means it is an integer and not a decimal number */ int sum; sum = 50 + 25; /* printf is used to print 2 known values and 1 unkown value, "sum" */ printf ("\nThe sum of 50 and 25 is %i. \n\n", sum); sum = 483 + 379; printf ("The sum of 483 and 379 is %i. \n\n", sum); // return 0 to the system to indicate successful execution return 0; } .