Chapter 1. Basic Techniques

Table of Contents
1.1. Hello World
1.2. How to Recompile a Program
1.3. The Generated Code
1.4. What's in My Program?
1.5. Restrictions

To start with we'll write a small program and run it on the simulator. This will give you a general idea of how things work. Later we will describe how to run a program on the real target computer.

1.1. Hello World

The subject of this chapter is a small program called "hello". Using library functions and simulated input-output to do the printing, it simply prints the message "Hello World" on your terminal. You will find the source code in the directory examples on the M68K Ada CD-ROM.

Three steps are needed to create an executable file from an Ada source file:

  1. The source file(s) must first be compiled.

  2. The file(s) then must be bound using the M68K Ada binder.

  3. All appropriate object files must be linked to produce an executable.

All three steps are best handled using the gnatmake program. Given the name of the main program, gnatmake automatically performs the necessary compilation, binding and linking steps. See Section 1.2.

1.1.1. How to Prepare an Ada Program

Any editor may be used to prepare an Ada program. If emacs is used, the optional Ada mode may be helpful in laying out the program. The program text is a normal text file. We will suppose in our initial example that you have used your editor to prepare the following standard format text file:

Example 1-1. The Source File

with Text_IO;
procedure Hello is
begin
   Text_IO.Put_Line ("Hello World");
end Hello;

This file should be named hello.adb. Using the default file naming conventions, M68K Ada requires that each file contain a single compilation unit whose file name corresponds to the unit name with periods replaced by hyphens and whose extension is .ads for a spec and .adb for a body.

1.1.2. How to Compile

You can compile the program using the following command:

Example 1-2. How to Compile hello.adb

$ m68k-coff-gcc -c hello.adb

The command m68k-coff-gcc is used to access the compiler. This command is capable of compiling programs in several languages including Ada 95, C, assembly language and object code. It determines you have given it an Ada program by the filename extension (.ads or .adb), and will call the Ada compiler to compile the specified file.

The -c switch is always required. It tells gcc to stop after compilation. (For C programs, gcc can also do linking, but this capability is not used directly for Ada programs, so the -c switch must always be present.)

This compile command generates the file hello.o which is the object file corresponding to the source file hello.adb. It also generates a file hello.ali, which contains additional information used to check that an Ada program is consistent. To get an executable file, we then use gnatbind to bind the program and gnatlink to link the program.

Example 1-3. Binding and Linking

$ m68k-coff-gnatbind hello.ali
$ m68k-coff-gnatlink hello.ali

The result is an executable file named hello.

You may use the option -v to get more information about which version of the tool was used and which files were read.

A simpler method of carrying out these steps is to use the gnatmake command. gnatmake is a master program that invokes all of the required compilation, binding and linking tools in the correct order. In particular, it automatically recompiles any modified sources, or sources that depend on modified sources, so that a consistent compilation is ensured.

The following example shows how to use gnatmake to build the program hello.

Example 1-4. Using gnatmake

$ m68k-coff-gnatmake hello
m68k-coff-gcc -c hello.adb
m68k-coff-gnatbind -x hello.ali
m68k-coff-gnatlink hello.ali
$ 

Again the result is an executable file named hello.

1.1.3. How to Run a Program on the Simulator

The program that we just built can be run on the simulator using the following command. If all has gone well, you will see the message "Hello World".

Example 1-5. Running on the Simulator

$ m68k-coff-run hello
Hello World