Saturday, August 11, 2012

AdaTutor - Outside Assignment 1

Preparing to Run Ada on Your Computer

Since these screens disappear, you'll probably want to refer to your printed course notes when doing the Outside Assignments.  The first assignment appears on page 8.  In this assignment, we'll compile, link, and run our first two programs, Hello and Add.

First compile HELLO.ADA, then link, and then execute the program.  The exact steps will depend on the Ada compiler you're using.  You'll probably have to refer to the documentation that came with your compiler.  Note that some implementations of Ada use the term "binding" instead of "linking."  When you run the program, it should display Hello! on your screen.

Then compile ADD.ADA, link, and run the program.

When we compile, we specify the name of the source file being compiled.  Some, but not all, implementations of Ada assume the extension to be .ADA if it's not specified.  When we link, we specify the name of the main program.  In these two programs, the program name agrees with the name of the source file, without the extension.  The linking step produces a file which we can execute.

With some Ada compilers, we must type a command to create an Ada library before compiling.  For example, with one compiler we tried, we had to type the command NEWLIB before we could compile programs.  However, after typing that command only once, we could compile any number of Ada programs.  Again, you'll have to consult your compiler documentation to see if such a command is necessary.

If you haven't done so already, please stop reading this AdaTutor temporarily, read page 8 of your printed course notes, and try the first Outside Assignment.

When you finish Outside Assignment 1, we'll go on to discuss the Ada compilation environment.

We know this first assignment wasn't very interesting, because the programs HELLO.ADA and ADD.ADA were supplied for you.  But we had to be sure we can compile, link, and execute Ada programs, so we can do the remaining assignments.

The rest of the Outside Assignments should be more challenging!

Let's go on to discuss the Ada compilation environment.

The Ada Compilation Environment

The programs Hello and Add were complete in themselves, and we were able to link them as main programs.  Of course, most Ada programs are more complex than that, involving calls to subprograms (procedures and functions).

When we compiled HELLO.ADA and ADD.ADA, the Ada compiler didn't "know" that we were going to link them as main programs.  After compiling them, we could have compiled another program that calls Hello, Add, or both, and link that other program as the main program.  The Ada system doesn't know which compilation unit is the main program until we link.

For example, we could create a file TEST.ADA containing the following:

with Hello, Add;
procedure Test is
begin
   Hello;
   Add;
end Test;

This new program does nothing but call procedures Hello and Add.

After compiling HELLO.ADA and ADD.ADA in either order, we could compile TEST.ADA, which withs Hello and Add.  Then we could link with the name of the main program, Test, to make an executable file.  Running Test would display "Hello!" on one line and "4" (with some leading spaces) on the next line.

Ada comes with several packages, such as Ada.Text_IO, that are already compiled.  That's why our programs can say with Ada.Text_IO; and call its procedures and functions.  We can also with previously compiled procedures and functions that are not in packages, as we did with procedures Hello and Add in the program Test above.  However, use applies only to packages.

When we compile a call to a subprogram, Ada always checks that the number and types of parameters in the call match those of the subprogram.  In the example above, Hello and Add had no parameters, and Ada checked that the calls to them in Test also had no parameters.

With some other languages, this checking isn't done.  For example, a Fortran main program might say CALL SUB(I, J, K), while the subroutine might say SUBROUTINE SUB(I, J).  Fortran will compile both of these correctly, without giving any error messages or warnings.  When the program is run, however, the results are unpredictable.  The program might simply give wrong answers with no warning.  An Ada compiler always catches that mistake!

In our example Test, we wrote and compiled the subprograms before the calling program.  Ada also lets us write and compile the calling program first, because we can compile a subprogram specification separately from its body.  (We'll learn more about that later.)  The specification contains the name of the subprogram, and the names, number, and types of any parameters.  The specification also tells which parameters are inputs, which are outputs, and which are both inputs and outputs.  That's all the information the compiler needs to compile calls to the subprogram; the body can be supplied later.  When the subprogram body is compiled, the compiler will make sure it's consistent with the specification.  For example, we could compile just these two lines:

    procedure Hello;

    procedure Add;
and then compile TEST.ADA.  We could later compile HELLO.ADA and ADD.ADA as the procedure bodies, and then link with the name of the main program, Test.

An Ada 95 compiler may defer generating code until the all the required bodies are present; GNAT is an example of such a compiler.  Most compilers, however, generate code when each unit is compiled.  Naturally, in any system all bodies must be present before we can link.

Question

True or False?  A procedure specification must be written before calls to the procedure can be compiled.

< prev   next >

No comments: