Preparing and Using a Program [ Micro Focus COBOL for UNIX, Getting Started ] MPE/iX 5.0 Documentation
Micro Focus COBOL for UNIX, Getting Started
Preparing and Using a Program
This sample session illustrates all the essential steps in preparing an
application for use:
* compiling - checking its syntax and producing intermediate code
* animating - testing and debugging it
* generating - producing an object program in native machine code
* running it
* linking - producing a single executable file containing the object
program, plus the support modules it needs from the Micro Focus
Run-Time Environment
Compiling
The tutorial programs supplied with the COBOL system are in subdirectory
demo in the directory pointed to by the $COBDIR environment variable.
Before you use any of them, we recommend you copy them to your own work
directory to avoid the danger of other users working with them at the
same time.
1. Copy the source file tictac.cbl from $COBDIR\demo into one of your
work directories.
2. Enter:
cob -av tictac.cbl
The explanation of this command line is:
cob tells the system to check the syntax and compile
your source code.
- indicates the start of the flags that you set to
control compilation.
a is a flag that tells cob that you will be using
Animator to test the program.
v is a flag that sets "verbose" mode, in which cob
displays messages to show progress.
tictac.cbl is the name of the program source file to compile.
The Compiler looks for the file tictac with the
extension .cbl or .cob.
You could in fact omit the -a flag, as it is the default; we have
included it here for clarity.
The Compiler banner appears, including the version number of the COBOL
system. The Compiler then checks the source code for syntax errors and
creates intermediate code, displaying a number of messages indicating its
progress. The system then outputs the intermediate code in the .int
file, and extra information needed by the Animator in an .idy file. When
compiling is complete, the following message appears:
Data=xxxxxxxxx Code=yyyyyyyyy
This means the compilation produced an object program needing xxxxxxxxx
bytes of data storage and yyyyyyyyy bytes of storage for Procedure
Division code.
You could run the .int program now, using cobrun. However, there is a
very powerful tool available for testing and debugging the logic of your
program, and it's a good idea to use this tool next.
Animating
Now use Animator to watch the source code executing.
Note: Animator animates .int files; if it finds a .gnt
file, it runs it without animation. If you run
through this sample session a second time, make
sure you have no tictac.gnt file left from the
previous time.
Enter:
anim tictac
The system displays the Animator banner, including the version number and
copyright information, and then displays the Animator main menu screen,
shown in Figure 3-1
Figure 3-1. The Animator Screen
This screen consists of four parts:
* the text area
* the information line
* the menu
* the message/input area
The text area means the main body of the screen.
The menu, at the bottom of the screen, shows the functions available at
this point. Each function has a function key (shown as Fnn= ) associated
with it; pressing the key activates the function.
Above the menu is the third part of the screen, the information line.
This line contains a mixture of fixed and variable information. For
example, the information line can contain the menu name, the name of the
program you are working with, the status of the Insert, Caps, Num Lock
and Scroll Lock keys, and toggle settings. A toggle is a key that
selects between options.
The message/input area is below the menu, on the bottom line of your
display. This is where the system displays messages and you enter
information such as file-names.
The source code is displayed with the cursor positioned at the first
executable statement:
perform with test after
The Animate main menu appears at the bottom of the screen. It displays
an information line containing the program name Tictac, together with
level, speed, Ins, Caps, Num and Scroll indicators. The next two lines
contain the function and letter key options you use to control the way
the program is animated.
Controlling the Speed.
You can execute your program in one of three modes: Step, Go and Zoom.
When you execute your program in Step mode, you manually control the
execution of each statement. In Go mode, you initially set the speed of
execution, then watch as each statement is automatically executed. Zoom
mode executes the entire program without displaying the source until it
reaches the end of the program (you can interrupt it, or set a breakpoint
where it will stop). These modes of execution are described in the
following sections.
Stepping Through the Program.
One of the letter commands on the Animate menu is S tep.
1. Press S to begin stepping through your program.
The cursor is now positioned under the next executable statement:
call clear-screen
2. Press S tep twice.
This positions the cursor on the DISPLAY statement and then
executes the statement.
The user screen is the screen displayed by the program itself,
that is, the screen that would be seen by the user if you were
running the program without Animator. The screen showing the
source is called the Animator screen.
The cursor then moves to the beginning of the statement:
perform init
3. Now press F2=view .
The user screen appears showing the text displayed by the executed
DISPLAY statement:
To select a square type a number between 1 and 9
You can use the view facility to view the user screen at any time
during animation.
4. Press any key to return to the Animator screen.
5. Now execute the next three statements in the program by pressing S
tep three times.
Each time you press S tep another statement is executed, and you
can see what is actually happening in the program.
Executing in Go Mode.
In Go mode, your program is automatically executed at the speed you
select, and you can watch exactly what is happening in the program.
1. To start Go mode, press G .
The Go menu shown in Figure 3-2 appears at the bottom of the
screen as execution begins. The program is currently executing at
a speed of 5, the default setting.
Figure 3-2. The Go Menu
2. Press 7 to increase the speed.
This change in speed is displayed on the information line. You
can set execution speed from either the Animate main menu or the
Go menu. The range of speeds is from 0 to 9, with 0 the slowest
speed. You can change this speed at any time during execution
(except during an ACCEPT statement) by pressing a key between 0
and 9.
Program execution halts when an ACCEPT statement is encountered.
The system now pauses on the user screen after executing the
ACCEPT statement on line 114 anticipating data entry.
3. Respond by typing N to indicate that you will be the one to start
the game.
The system continues animation in Go mode at a speed of 7 and
pauses after the next ACCEPT, displaying the user screen and
prompting again for data entry.
4. Select square 9 by pressing 9 on your keyboard.
This places an "X" in square 9 and resumes program execution in Go
mode. Remember, you can press F2=view at any time to see the user
program's screen.
Execution stops again when you are prompted to select another
square. Notice that the system has placed a 0 in square 5.
5. Press 8 to select the square and to resume execution.
You can temporarily halt execution at any time except while the
program is executing an ACCEPT statement.
6. Press Esc.
The message:
Keyboard interrupt
appears at the bottom of the screen. The cursor is positioned on
the next executable statement.
Executing in Zoom Mode.
1. Now press Z to begin execution in Zoom mode.
The program resumes execution at full speed and displays only the
user screen. Continue playing until the game is complete. Then
the message:
Play again?
appears.
2. Respond by typing N (No).
The source code returns to the screen with the cursor positioned
under STOP RUN, indicating that the program has completed
execution. The message:
STOP RUN encountered with RETURN-CODE = +00000: use Escape
to terminate
appears at the bottom of the screen.
Exiting Animator.
1. Press Esc to terminate your Animator session.
The message:
Exit from animator? Y/N
appears at the bottom of the screen.
2. Press Y (Yes) to return to the operating system.
Windows On Windows, a message box appears saying
"Application terminated" and you click on Ok to
finish.
Generating Native Code
Having animated the program, you will now want to convert it to optimized
native code in a .gnt file.
Enter:
cob -uv tictac.cbl
In this command line, all is as before, except:
u is a flag that tells cob to process the program
through to producing dynamically loadable generated
code.
This time, cob invokes the Compiler and the Generator. The last message
displayed comes from the Generator, and is similar to the Compiler's
message giving data and code sizes, but also shows space taken by
literals.
Since you had already produced a tictac.int in your previous call of cob
(see the section Compiling, above), you could have omitted the Compiler
step by specifying .int instead of .cbl .
Running
To run Tictac, enter:
cobrun tictac
You need not specify an extension, as cobrun will automatically look for
the .gnt file.
The system runs the program, which starts off by displaying the first
message of the game on your screen. Have fun playing it; it can be
beaten!
You could run the intermediate code instead by specifying an extension of
.int (also, if cobrun does not find a .gnt file it will run the .int
file). The above call of cob to produce generated code would then be
unnecessary. However, although the difference is not apparent in a small
program such as Tictac, generated code runs much faster than intermediate
code.
Linking
As an alternative to using cobrun, you can create an executable file.
NOTE You can link your program only if you have a "C"/Operating System
Development System installed on your machine. This provides the
system tools that cob needs, like ld, the system linker.
Enter:
cob -xv tictac.cbl
In this command line, all is as before, except:
x is a flag that tells cob to process the program all
the way through to producing an executable file.
This time cob invokes the Compiler, Generator, and UNIX system linker,
one after the other. The last thing displayed is a list headed "Entry
points defined in module: tictac.o ", after which there may be a long
pause before the operating system prompt appears.
Since you had already produced a tictac.int in your previous call of cob
(see the section Compiling, above), you could have omitted the Compiler
step here; if you had specified .int instead of .cbl , cob would have
used the existing .int file and just invoked the Generator and Linker.
The end effect would have been the same.
Running the Linked Program
To run Tictac, simply enter:
./tictac
The system runs the program.
MPE/iX 5.0 Documentation