HiSoft Pascal Primer cover: a square, a triangle and a circle drawn and filled by a HiSoft Pascal program on the Tatung Einstein
← Back to Modules
module
5

When A Program Stops

Introduction

In the Turbo course a program that went wrong while it ran printed Run-time error and a number, and Turbo took you to the place. HiSoft Pascal prints a message in words and an address, and leaves the finding to you. And the Turbo course taught you that a runaway loop could only be stopped if you had compiled it with {$U+}. In HiSoft Pascal it is the other way round - and that turns out to matter more than you would think.

Run-Time Errors

When something goes wrong in a running program, it stops with one line and returns to 0::

MessageWhat happened
Overflow at PC=0E98An integer sum went past 32767 (S6)
/ by Zero at PC=0EC2DIV or MOD by zero - an error here, unlike Turbo
Index too High at PC=0F36An array index past the top of its range
Out of RAM at PC=0E7DThe program ran out of memory - usually a procedure calling itself without end
Number expected at PC=1016A READ of a number met something else (S9)
Halt at PC=0EA5You pressed CTRL-C

Finding The Line

The address after PC= is where in the program it stopped. It is the same kind of address the compiler's listing gives at the start of each line - so the listing tells you the line. Find the last line whose address is not more than the one in the message.

For example, with this in the listing:

0EA8     70 WRITELN(I DIV (I-5))
0ECB     80 END

/ by Zero at PC=0EC2 is in line 70: 0EC2 comes after 0EA8 and before 0ECB. The addresses are hexadecimal, but they only need comparing, digit by digit from the left.

If the listing has scrolled away, compile again (;Y) and watch for the line.

CTRL-C

CTRL-C stops a running HiSoft program, even one sitting in an empty loop. It prints Halt at PC= - with the address of where it was, which tells you which loop it was stuck in.

That is because the compiler, unless told otherwise, puts a check for CTRL-C into every loop. It is the Turbo course's {$U+} - but on from the start.

The Price

The check is not free. It runs every time round every loop, and it is slow: an empty FOR loop that runs about 44,000 times a second without it runs about 1,440 times a second with it. Turbo, with its check off, did about 39,000.

So you have the choice Turbo gave you, the other way about. A comment starting with $ gives the compiler an option; (*$C-*) turns the check off for the lines after it:

(*$C-*)
PROGRAM COUNT;

Now the loops run thirty times faster - and CTRL-C does nothing. A program under (*$C-*) that never ends can only be stopped by restarting the Einstein, or by a way out you write into it yourself (S15).

The sensible habit: write and test with the check on. When the program works, add (*$C-*) at the top and compile it again.

Other Options

Options take the same form, a letter and + or -: (*$O-*) switches off the overflow check on sums (S6), and (*$L-*) makes the compiler list only the lines with errors - handy once your programs are too long to watch go by.

The Code

10 PROGRAM COUNT;
20 VAR I,J:INTEGER;
30 BEGIN
40 FOR I:=1 TO 10 DO
50 BEGIN
60 FOR J:=1 TO 3000 DO;
70 WRITELN(I)
80 END
90 END.

Line 60 is an empty loop - it does nothing 3,000 times, just to take time.

Starting from

COUNT.ASC saved from XBAS; HPEIN COUNT.

What you should see

The numbers 1 to 10, one about every two seconds - about 21 seconds in all. Run it again, and press CTRL-C after a few seconds:

1
Halt at PC=0EA5
0:

Line 60 starts at 0E88 in the listing and line 70 at 0EA8, so 0EA5 is in the empty loop - where it spends nearly all its time.

Change One Thing

  • Put (*$C-*) in as a new line 5, save and compile. How long do the ten numbers take now? What does CTRL-C do?
  • Take line 5 out again, and change line 70 to WRITELN(I DIV (I-5)). What does it print before it stops, which message does it stop with, and which line does the address point to?
  • In the same program, what do you predict 3 DIV (3-5) prints - -1 or -2?

Exercises

5.1 Write a program with a REPEAT ... UNTIL FALSE loop that prints a count. Run it and stop it with CTRL-C. Which line does the PC= address point to?

5.2 Time COUNT with and without (*$C-*), and work out how many times a second each runs the empty loop.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
CTRL-C does nothingThe program has (*$C-*). Restart the Einstein - and put a key test in the loop next time (S15).
A loop is much slower than in TurboThe CTRL-C check is on. (*$C-*) once it works.
/ by Zero at PC=...A DIV or MOD by zero. In Turbo this was not an error; here it stops the program.
The PC= address matches no lineIt is between two line addresses: it belongs to the earlier line.

Summary

A run-time error prints its message and at PC= an address; the compiler's listing shows which line holds that address. CTRL-C stops a program - Halt at PC= - because the compiler checks for it in every loop by default. That check makes loops about thirty times slower; (*$C-*) removes it, and CTRL-C with it.

Next

S6, Numbers - integers and reals: mostly as in Turbo, with a space after every number and some differences at the edges.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.