5

FORTRAN had an 'assigned goto' statement. Firstly, a statement number is assigned to a variable:

ASSIGN 42 TO L

Subsequently, one can obey

GOTO L

which (given the preceding ASSIGN) would transfer control to the statement labelled 42.

But in actual fact, the assigned goto had to be given a list of statement numbers:

GOTO L, (17, 23, 42, 63, 99)

Why was that needed or chosen?

I would suppose that the affect of ASSIGN is to set the variable to the actual address corresponding to the labelled statement. That being so, the assigned goto is just an indirect jump through the variable. What purpose does the statement-label list serve?

Compare with the computed-goto, which is somewhat like the Algol switch statement: given an integer N, jump to the N'th label.

 GOTO (17, 23, 42, 63, 99), N

For this case it's clear the label list is needed. But not in the assigned-goto case.

  • Did the assigned label list restrict the values that could be used in L? Like in your example, could you ASSIGN 11 TO L and then execute the GOTO L statement with 11 not being in the list? That would at least give the poor reader some clue where the jump might go, rather than just anywhere. – Greg Hewgill Aug 25 at 3:35
  • With DEC's F77 compiler, a statement number not in the list causes fall-through. Ugly! – another-dave Aug 27 at 2:22
12

TL;DR:

That being so, the assigned goto is just an indirect jump through the variable.

Right.

But in actual fact, the assigned goto had to be given a list of statement numbers

No, it had not. The list was always an optional one. If not given, the GOTO was simply executed without any further check.

What purpose does the statement-label list serve?

It's a run time check for valid targets.

This is why literature often calls the variants as 'Unguarded' (without a list) or 'Guarded' (with a list) Assigned GOTO.

It's important to keep in mind that FORTRAN was missing next to all control structures we take for granted today. FORTRAN is the prototypical spaghetti code and Assigned GOTO was the way to create complex structures with jump targets hidden in variables.

Adding a list to check against was a way to make certain loop constructions spaghetti code manageable (kind of) as well as to catch pointer errors. Not really the best way for either, but these were the beginnings of HLL, one had to start somewhere.


In Detail:

Using assign basically turns an integer into a pointer and loads it with the address of the label, not its numeric value.

An Unguarded Assigned GOTO takes the pointer value of the integer and jumps. With a list added it checks if the Integer holds the address of any single label within the list and only jumps when it's among them.

Think of forming a loop with a various cases within, like a state machine. Remember, there was no switch/case like statement in FORTRAN, so it had to be done some other way. And assigned GOTO was the way to go. At the end of each state check the next state was loaded into an integer but control was returned (via unconditional GOTO) to the main loop, which picked the next data and switched accordingly.

All of this ends up with a vast number of targets. In addition storage, and thus variables, was limited, so variables get reused. Using a single variable (like IGO) for all/most Assigned GOTO in a program was quite common. So this variable might contain some value from outside the loop constructions one was in. Quite a good chance to ass programming errors ending up at a GOTO IGO with a leftover target from some prior construct.

Being able to name a list of all (at the point) valid targets seemed like a good idea to catch that and make sure all works as intended.

At that point it's once again important that we talk about a time when everything was barely invented, machines were small and compilers straightforward without much ability or even chance for checking. Not to mention that FORTRAN was on purpose kept simple to have users adopt it. Maybe hard to believe, but scientists were hardcore Assembly users at that time. So many concepts were tried, some of them might look strange from today's orderly landscape.

In addition, FORTRAN, as simple as it may seem today, was considered by many users as bloat. So making it work as straightforward as possible, so users can imagine the Assembly code while writing FORTRAN, was mandatory. There is a very nice interview with Frances Allen talking about this time.


Now, in a perfect world it could end here, but the real world also included implementation specific effects. The FORTRAN description did leave up a few grey areas:

For one it didn't define if and how an integer used to hold an integer is prevented against being used as target, nor if and how one holding a target is protected against being used as integer.

Using a Guarded Assigned GOTO one way to protect against such errors, by checking the values against legal values.

Second, it wasn't stated what happens if the integer is holding a target that is not within the list. Some implementation simply dropped to the next statement, while others threw an exception ending the program - which might be the most safe way.

Well, and some ignored the list at all.

Spaghetti code, implementation dependant behaviour, added, changed or missing instructions - everything we love and hate about BASIC was already present in FORTRAN, but on a much worse level.


With the introduction of additional loop control (WHILE, EXIT, etc.) in many FORTRAN-77 compilers (aka FORTRAN-78) the use of Assigned GOTO as well as computed GOTO or alternate returns became obsolete and finally removed in FORTRAN-90.

| improve this answer | |
  • 2
    My experiments show that even a 1973 BESM-6 FORTRAN compiler directly recoded from a (much older) CDC 1604 compiler does not require the list of potential labels, and when it is present but does not include the assigned label, the GOTO is still performed. In other words, not all compilers honored the list to perform the runtime check. – Leo B. Aug 25 at 4:00
  • 2
    I should perhaps add, I didn't acquire the IBSYS FORTRAN manual as an "historical document" :-) – another-dave Aug 25 at 22:25
  • 1
    @Raffzahn The first one is FORTRAN-BESM-6 (1973), transcoded from CDC 1604. The second one is FORTRAN-GDR (I'm not sure in what organization it was written), 1981. The third one is FOREX by Keldysh Institute of Applied Mathematics, 1985. If a scalar variable is used in the GOTO, none of the three require a list. FORTRAN-GDR wants to see a comma if it sees a parenthesis in a GOTO statement. – Leo B. Aug 26 at 7:42
  • 1
    The DEC F77 compiler (RSX-11M-PLUS) supports an optional label list. Without the list, an arbitrary ASSIGN stmt TO N; GOTO N will succeed. With a list not containing N, the GOTO simply falls through. That last seems to be a particularly ugly choice. – another-dave Aug 27 at 2:13
  • 1
    @WalterMitty in another question pointed us at the DEC PDP-10 FORTRAN IV manual. It explicitly describes fall-through for label not in list. – another-dave Aug 27 at 12:21
1

As an example, the PDP-11 FORTRAN (UNIX V5) had a runtime check, if the label list had been provided. An attempt to jump to a label not equivalent to those mentioned on the list would result in a runtime error.

Using an online emulator,

Paul Nankervis - paulnank@hotmail.com

Boot> boot rk0
@unix

login: root
# chdir /tmp
# cat > test.f
      assign 10 to l
      goto l,(20)
      stop
  10  print 1
   1  format(' label 10')
      stop
  20  print 2
   2  format(' label 20')
      stop
      end
Ctrl/D
# fc test.f
# ./a.out
Runtime error 14

Whereas the program with just goto l prints label 10, as expected.

However, this works:

      assign 10 to l
      goto l,(20)
   10 continue
   20 continue
      end

because both labels resolve to the same offset in the compiled executable, and the check succeeds.

| improve this answer | |
1

I'm posting my own answer as a summary, just prior to accepting someone else's.

  1. The statement list is not necessary. If present, it may be used for validation of the jump target. If absent, the GOTO will jump regardless.

  2. The statement list is optional in some implementations. We have not yet proven whether it was/wasn't optional in the original FORTRAN.

  3. If the statement list is supplied on the GOTO, and the variable is assigned a statement not in the list, the results vary between implementations.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.