Jump instruction
A jump instruction or a jump instruction is an instruction in a programming language . In computer programs , it is used to continue processing not with the following command, but at any point.
The best-known jump instruction is the so-called Goto (from English go to go to). This is in programming a command in the program code that a jump to another location in the program causes.
Branching to subroutines (also: Subroutine and Function ) by means of Call executed or similar delivery commands (depending on the programming language and sub-program type).
A return jump is the return to the point at which the (main) program is to be continued after such a call and the execution of a subroutine - often caused by a return statement in the subroutine.
However, the jump back to a point in the program code that has already been visited, i.e. the repetition of a program section in a loop , is referred to as a jump back. Instead of Goto instructions, so-called control structures are used in modern programming methods; Details see below .
Jump instructions at machine level
In contrast to the high-level languages, one speaks here more often of jump instructions than of jump instructions .
Processors usually know several different jump instructions. These can be divided into:
- conditionally or unconditionally
- The jump is either always executed or only if a condition is met.
- absolute, relative, register or memory
- The program will continue at the specified address.
- The jump is made relative to the current position. This means that the code can be moved (relocated).
- You can calculate the jump destination
JMP reg
. - The address of the jump destination is somewhere in memory
JMP [reg]
orJMP [reg+offs]
.
A special form of jump instructions are subroutine calls , i. H. Jumps with the intention of returning. When they are executed, the address of the subsequent command is first saved (on the stack or in a register), and only then is it jumped to the target address. About return instructions , the subroutine can return to the original position and the program will continue that way. The return address is taken from the stack or is in a register.
Jump instructions at high-level language level
In high-level programming languages , a jump instruction is an instruction of the form
goto Markenname
or
if Bedingung goto Markenname
In the first case it is called unconditional jump instruction , in the second it is called conditional jump instruction . Here stands Markenname
for the name of a jump label that is defined at another point in the program. When the program is processed, the program continues from the point at which the jump label is located.
In principle, you could program any algorithm with conditional jump instructions and some additional functions for data input and output . However, the resulting programs are almost impossible to maintain from a certain size . In current programming languages, explicitly formulated jump instructions are therefore rarely used. Instead, higher-value control structures , in particular loops, subroutine calls and conditionally executed program blocks, are used in which the logically associated branch and branch back instructions are implicit, i.e. H. can be inserted in the machine code by the translator without having to program a Goto.
GOTO variants
In machine-level programming, the Goto command is used to branch to another point in the program , either unconditionally or depending on a condition. In some assembly languages , the corresponding Goto command is called JMP ( English jump jump) or BR ( English branch branch).
In high-level programming languages (which may also allow lower case), either a code line number (for example in old BASIC dialects) or a defined jump label ( label , for example in C or Pascal ) is addressed by a Goto command .
Example of a simple program with Goto in Pascal:
PROGRAM beispiel;
LABEL 10;
BEGIN
10:
writeln('Endlosschleife');
GOTO 10;
END.
The following program example (from the Essay Coding Style by Linus Torvalds ) shows the use of a Goto command goto out
in C :
int fun(int a) {
int result = 0;
char *buffer = kmalloc(SIZE);
if (buffer == NULL)
return -ENOMEM;
if (condition1) {
while (loop1) {
// ...
}
result = 1;
goto out;
}
// ...
out:
kfree(buffer);
return result;
}
In early versions of older programming languages such as Fortran or BASIC , Goto commands were still the most important, sometimes even the only way to program branches; these were therefore also used to implement loops and conditional execution. This led to spaghetti code .
The programming language ALGOL led in 1960 own commands while
, for
, if-else
for programming of loops and conditional statements and branching one. These innovations were soon adopted in other programming languages. As while
a substitute for jumps back and if
as a substitute for jumps forward, with a suitable restructuring of every program, the Goto instruction is theoretically superfluous.
The structured programming and the GOTO programming, however, are equivalent from the perspective of theoretical computer science; all such programs fall under the category of GOTO programs .
In other respects the commands of structured programming with GOTO are equivalent: The instruction set of a processor usually contains no explicit support of constructs such as while
, repeat
, for
, if-else
etc. Therefore this when compiling a program by the compiler by unconditional and conditional jump instructions ( Mnemonics: JP, JMP, J cond , BR, B cc , ...) reproduced. The above-mentioned constructs do not offer any new possibilities, but they are useful to force human-understandable and therefore maintenance-friendly code.
Controversial use of GOTO
In 1968, Edsger W. Dijkstra spoke out in his essay Go To Statement Considered Harmful (originally: A Case against the GO TO Statement ; the later title goes back to Niklaus Wirth ), for an abolition of the Goto command in all higher programming languages. This is easily possible, since almost every Goto can be replaced by so-called control structure instructions such as do
, for
or while
(see also simulation GOTO by WHILE ). After initial resistance, this opinion soon became a dogma in programming training; In some languages such as Java , no Goto command was deliberately introduced at all, but instead a labeled break and later a labeled continue . However, it is goto
also reserved as a keyword in Java, the implementation may be reserved, but probably also for practical reasons; otherwise goto would be a valid identifier. On the other hand, numerous new imperative programming languages still support Goto, e.g. B. C ++ and even the C # developed in 2000 and the D published in 2007 . In 2009, Goto was subsequently introduced into the scripting language PHP from version 5.3 and was included in the new Go language .
A less criticized variant of the Goto command is to exit a subroutine prematurely return
, to abort a loop break
or to abort a specific loop pass and to start the next pass continue
. Nevertheless, in some programming languages, it is considered good programming style if a block has exactly one entry point and exactly one exit point. In languages that allow exception handling , on the other hand, the throw
entry / exit point rule is viewed as obsolete, since statements reduce the argumentation behind this rule to absurdity.
In practice, however, it has been shown that it is possible to dispense with Goto, but in some cases leads to very complex constructs. Donald E. Knuth thinks Goto is the optimal solution for some common programming problems. Especially in time-critical program parts, a Goto is significantly more efficient than performing an abort check at the end of several nested loops.
The frequent use of goto in Linux source code has been discussed by some developers on the Linux kernel mailing list . Linus Torvalds said that the use of Goto could in many cases significantly increase the readability of the source code.
See also
Web links
- Edsger W. Dijkstra: Go To Statement Considered Harmful. In: Communications of the ACM . Vol. 11, No. 3, March 1968, pp. 147–148 ( PDF )
- David R. Tribble: Go To Statement Considered Harmful: A Retrospective . November 27, 2005
Individual evidence
- ^ IBM System / 360 Principles of Operation. IBM , 1964, p. 64 , accessed on February 2, 2015 (English, command description for “Branch and Link”: BAL, BALR).
- ↑ Linux kernel coding style, Chapter 7: Centralized exiting of functions (English)
- ^ Edsger W. Dijkstra: Letters to the editor: Go To Statement Considered Harmful . In: Communications of the ACM . tape 11 , no. 3 , March 1968, p. 147-148 , doi : 10.1145 / 362929.362947 .
- ^ Frank Rubin: "GOTO Considered Harmful" Considered Harmful . In: ACM (Ed.): Communications of the ACM . tape 30 , no. 3 , March 1987, p. 195–196 , doi : 10.1145 / 214748.315722 ( ecn.purdue.edu (PDF) [accessed September 21, 2010]).
- ^ Donald E. Knuth: Structured Programming with Go to Statements . In: ACM (Ed.): Computing Surveys . tape 6 , no. 4 , 1974, p. 261–301 , doi : 10.1145 / 356635.356640 ( clifford.at (PDF) [accessed on January 5, 2019]).
- ↑ Linux: Using goto In Kernel Code. kerneltrap.org, archived from the original on November 28, 2005 ; accessed on September 21, 2010 (English).