Information Technology Topic

Steps in Implementing a Program

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. The steps in order are: create the source code, compile it to object code, link it with library code to make an executable file, execute it, and maintain it afterwards
  2. Errors send you back. Compile errors, and wrong results found when the program is executed, both mean editing the source code and repeating the steps from compile
  3. Know what goes in and what comes out of each step: source code into the compiler, object code into the linker, an executable file out, and results from execute. You edit the source and you run the executable

Visual notes

The steps in implementing a program
A top-to-bottom flow of five boxes joined by arrows: 1 Create source code, 2 Compile, 3 Link, 4 Execute, 5 Maintain. A second arrow, labelled Errors: edit the source, leads from Compile back to Create source code. A third arrow, labelled Wrong output: edit the source, leads from Execute back to Create source code.

The five steps in order. Compile errors and wrong output both send you back to edit the source code. Maintenance later repeats the same cycle.

Section 1The big picture

A program starts as an idea and ends as something people use. Getting it there takes five steps: create the source code, compile, link, execute and maintain. The diagram for this topic shows them in order. Here's the trick: each step takes something in and gives something out. Source code goes into the compiler and object code comes out. Object code goes into the linker and an executable file comes out. The executable file is what gets executed. We will follow this small Pascal-style program through the steps: program Hello; begin writeln('Good morning, Kingston'); end.

Section 2Step 1: Create the source code

The programmer types the program in a text editor and saves it as a source file. The program as typed is called the source code. Free Pascal expects a Pascal source file to end in .pas (or .pp). Source code is written for people to read. The computer cannot run it yet, because the processor only carries out machine language. The small program above is source code, and the name Hello at the top is just the program's heading.

Section 3Step 2: Compile

The compiler is a language translator. It reads the whole source file, checks it against the rules of the language and translates it into object code, which is machine language. If the source breaks the rules of the language, the compiler stops and reports error messages. No object code, and so no program, is produced until the errors are fixed. The programmer goes back to step 1, corrects the source code, and compiles again. Free Pascal writes the object code into an object file, which has the extension .o.

Section 4Step 3: Link

Few programs are made of only the code the programmer wrote. Common jobs, such as writing text to the screen, come as ready-made library code that is already translated. A Pascal program can also use units, and the object code of a unit is linked into the programs that use it. The linker joins the program's object code with the library code it needs, and the result is one executable file. Easy to mix up: compiling translates your code and linking joins the translated pieces. Free Pascal's command does both one after the other, so it looks like one step, but they are still two jobs in this order.

Section 5Step 4: Execute

To execute a program means to run it. The executable file is loaded into memory and the processor carries out its instructions. The program takes in any input, processes it and gives output. Because the executable is already in machine language, it can be run again and again without the compiler or the source code. You only need to compile and link again if the source code changes. If the program runs but the results are wrong, the fault is in the steps of the program itself. Fixing it means going back to the source code, not editing the executable.

Section 6Step 5: Maintain

A program is not finished when it first works. Maintenance is everything done to keep it working and useful once people are using it. That includes repairing errors found later, and changing the program when needs change, for example a new subject on a school report, or a new rate on a shop's bills. Maintenance is a smaller repeat of the same cycle: edit the source code, then compile, link and execute again. Clear notes and documentation written while the program was created make maintenance much easier.

Section 7Going back: the error loops

The steps are not always a straight line. Two loops send you back to the source code. Compile errors: the compiler reports that the source breaks the rules of the language. Fix the source, then compile again. Wrong output: the program runs but gives wrong results. Fix the source, then compile, link and execute again and compare with the results you expected. Watch out: never fix a mistake by changing the object code or the executable. The source code is the master copy, and the next compile would simply bring the mistake back.

Examples

A student is told the steps to implement a program, but they are jumbled. A: Execute the program B: Link the object code with library code C: Write and save the source code D: Maintain the program E: Compile the source code (a) Put the steps in the correct order. (b) State what is produced by steps C, E and B. (c) State which step the student must return to if the compiler reports an error, and why.

(a) The order is C, E, B, A, D: create the source code, compile, link, execute, maintain. (b) Step C produces the source file. Step E produces object code, or error messages if the source breaks the rules of the language. Step B produces the executable file. (c) She returns to step C, writing and editing the source code. The compiler cannot make object code from a program that breaks the rules, so nothing can be linked or run until the source is corrected. Then she compiles again.

A student in Portmore writes a Pascal-style program to find the average of two marks. This is her first version. program MarkAverage; var first : integer; second : integer; average : real; begin readln(first); readln(second) average := first + second / 2; writeln(average:0:1); end. Her test data is 40 and 80, so the expected output is 60.0. Attempt 1: she compiles it and the compiler reports an error. She adds the missing semicolon after readln(second). Attempt 2: it compiles and links, and she executes it. The output is 80.0. She changes the formula to average := (first + second) / 2; Attempt 3: it compiles, links and executes. The output is 60.0. (a) Describe what happened in each attempt, naming the steps carried out. (b) State how many times the compile, link and execute steps were each carried out. (c) Explain why the link step was not carried out in attempt 1. (d) Explain why attempt 2 gave 80.0, and state which step she returned to and why.

(a) Attempt 1: create source code, then compile. The compiler reported an error, so there was no object code and she returned to the source. Attempt 2: edit the source, compile (successful), link (successful), execute, which gave 80.0 where 60.0 was expected, so she returned to the source again. Attempt 3: edit the source, compile, link and execute, which gave 60.0, matching the expected output. (b) Compile was carried out 3 times, link 2 times and execute 2 times. (c) The linker needs object code to join. The failed compile in attempt 1 produced none, so there was nothing to link. (d) Division is done before addition, so 80 / 2 = 40 and 40 + 40 = 80. The program was valid, so it compiled, linked and ran, but its steps were wrong. The fault is in the source code, so she returned to step 1 to edit the formula, and then compiled, linked and executed again. Changing the executable would not have worked, because the source would still contain the mistake.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from