Jump anywhere: Difference between revisions

m
→‎{{header|TXR}}: Fixed unfinished </code> tag
No edit summary
m (→‎{{header|TXR}}: Fixed unfinished </code> tag)
 
(20 intermediate revisions by 11 users not shown)
Line 23:
You may also defer to more specific tasks, like [[Exceptions]] or [[Generator]].
<br><br>
 
=={{header|360 Assembly}}==
The unconditionnal branch instruction <b>B</b> is the jump anywhere of the S/360 assembler.
<syntaxhighlight lang="360asm"> ...
B ANYWHERE branch
...
ANYWHERE EQU * label
...</syntaxhighlight>
 
=={{header|6502 Assembly}}==
Line 193 ⟶ 201:
 
Although <code>JMP</code> was referred to as "unconditional" before, it does ''not'' have access to the entire address space of the CPU, unlike the 6502, Z80, and 68000. The 8086 uses a segmented memory model, where it uses 20-bit addresses despite being a 16-bit CPU. The other 4 bits are the "segment," and a <code>JMP</code> or <code>CALL</code> cannot exit that segment.
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program julpanaywhere64.s */
 
/************************************/
/* Constantes */
/************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessResult: .asciz "loop indice : "
szMessage1: .asciz "Display to routine call by register\n"
szMessage2: .asciz "Equal to zero.\n"
szMessage3: .asciz "Not equal to zero.\n"
szMessage4: .asciz "loop start\n"
szMessage5: .asciz "No executed.\n"
szMessResult1: .asciz ","
szMessResult2: .asciz "]\n"
szMessStart: .asciz "Program 64 bits start.\n"
szCarriageReturn: .asciz "\n"
 
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrszMessStart
bl affichageMess // branch and link to routine
// return here after routine execution
b label1 // branch unconditional to label
ldr x0,qAdrszMessage5 // this instruction is never executed
bl affichageMess // and this
label1:
ldr x0,qAdrszMessage4
bl affichageMess
mov x20,0
1:
mov x0,x20
ldr x1,qAdrsZoneConv
bl conversion10 // decimal conversion
strb wzr,[x1,x0]
mov x0,#3 // number string to display
ldr x1,qAdrszMessResult
ldr x2,qAdrsZoneConv // insert conversion in message
ldr x3,qAdrszCarriageReturn
bl displayStrings // display message
add x20,x20,1 // increment indice
cmp x20,5
blt 1b // branch for loop if lower
mov x0,0
cbz x0,2f // jump to label 2 if x0 = 0
2:
adr x1,affichageMess // load routine address in register
ldr x0,qAdrszMessage1
blr x1
mov x4,4
cmp x4,10
bgt 3f // branch if higter
mov x0,x4
3:
mov x0,0b100 // 1 -> bit 2
tbz x0,2,labzero // if bit 2 equal 0 jump to label
ldr x0,qAdrszMessage3 // bit 2 <> 0
bl affichageMess
b endtest // jump end if else
labzero: // display if bit equal to 0
ldr x0,qAdrszMessage2
bl affichageMess
endtest:
mov x0,0b000 // 0 -> bit 2
tbnz x0,2,4f // if bit 2 <> 0 jump to label
ldr x0,qAdrszMessage2 // bit 2 = 0
bl affichageMess
b 5f // jump end test
4: // display if bit equal to 1
ldr x0,qAdrszMessage3
bl affichageMess
5:
 
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsZoneConv: .quad sZoneConv
qAdrszMessResult: .quad szMessResult
qAdrszMessage1: .quad szMessage1
qAdrszMessage2: .quad szMessage2
qAdrszMessage3: .quad szMessage3
qAdrszMessage4: .quad szMessage4
qAdrszMessage5: .quad szMessage5
qAdrszMessStart: .quad szMessStart
 
/***************************************************/
/* display multi strings */
/***************************************************/
/* x0 contains number strings address */
/* x1 address string1 */
/* x2 address string2 */
/* x3 address string3 */
/* other address on the stack */
/* thinck to add number other address * 4 to add to the stack */
displayStrings: // INFO: displayStrings
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
add fp,sp,#48 // save paraméters address (6 registers saved * 8 bytes)
mov x4,x0 // save strings number
cmp x4,#0 // 0 string -> end
ble 100f // branch to equal or smaller
mov x0,x1 // string 1
bl affichageMess
cmp x4,#1 // number > 1
ble 100f
mov x0,x2
bl affichageMess
cmp x4,#2
ble 100f
mov x0,x3
bl affichageMess
cmp x4,#3
ble 100f
mov x3,#3
sub x2,x4,#4
1: // loop extract address string on stack
ldr x0,[fp,x2,lsl #3]
bl affichageMess
subs x2,x2,#1
bge 1b
100:
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret // return to addtress stored in lr
 
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{Out}}
<pre>
Program 64 bits start.
loop start
loop indice : 0
loop indice : 1
loop indice : 2
loop indice : 3
loop indice : 4
Display to routine call by register
Not equal to zero.
Equal to zero.
</pre>
 
=={{header|Ada}}==
Line 354 ⟶ 534:
<syntaxhighlight lang="applesoftbasic">CONT : REM CONTINUE, JUMP BACK TO WHERE THE PROGRAM STOPPED</syntaxhighlight>
 
==={{header|IS-BASICBASIC256}}===
<syntaxhighlight lang="is-basic">10 GOTO 100 ! jump to a specific line
20 RUN 200 ! start the program running from a specific line</syntaxhighlight>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">for i = 1 to 10
if i = 5 then goto [label5]
next i
end
 
[label5]
print i
while i < 10
if i = 6 then goto [label6]
i = i + 1
wend
end
 
[label6]
print i
if i = 6 then goto [finish]
print "Why am I here"
 
[finish]
print "done"</syntaxhighlight>
 
=={{header|BASIC256}}==
BASIC256 supports both <code>goto</code> and <code>gosub</code>.
{{works with|QBasic}}
Line 408 ⟶ 562:
{{out}}
<pre>Igual que la entrada de FutureBasic.</pre>
 
==={{header|Chipmunk Basic}}===
Chipmunk Basic supports both <code>goto</code> and <code>gosub</code>.
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 PRINT "First line."
120 GOSUB sub1
130 PRINT "Fifth line."
140 GOTO Ending
150 sub1:
160 PRINT "Second line."
170 GOSUB sub2
180 PRINT "Fourth line."
190 RETURN
200 Ending:
210 PRINT "We're just about done..."
220 GOTO Finished
230 sub2:
240 PRINT "Third line."
250 RETURN
260 Finished:
270 PRINT "... with goto and gosub, thankfully."
280 END</syntaxhighlight>
{{out}}
<pre>Same as FutureBasic entry.</pre>
 
==={{header|GW-BASIC}}===
GW-BASIC supports both <code>goto</code> and <code>gosub</code>.
{{works with|Applesoft BASIC}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|Minimal BASIC}}
{{works with|MSX BASIC|any}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
{{works with|Quite BASIC}}
<syntaxhighlight lang="qbasic">100 PRINT "First line."
110 GOSUB 140
120 PRINT "Fifth line."
130 GOTO 190
140 REM sub1:
150 PRINT "Second line."
160 GOSUB 220
170 PRINT "Fourth line."
180 RETURN
190 REM Ending:
200 PRINT "We're just about done..."
210 GOTO 250
220 REM sub2:
230 PRINT "Third line."
240 RETURN
250 REM Finished:
260 PRINT "... with goto and gosub, thankfully."
270 END</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">10 GOTO 100 ! jump to a specific line
20 RUN 200 ! start the program running from a specific line</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|MSX Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|Quite BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">for i = 1 to 10
if i = 5 then goto [label5]
next i
end
 
[label5]
print i
while i < 10
if i = 6 then goto [label6]
i = i + 1
wend
end
 
[label6]
print i
if i = 6 then goto [finish]
print "Why am I here"
 
[finish]
print "done"</syntaxhighlight>
 
==={{header|True BASIC}}===
True BASIC supports both <code>goto</code> and <code>gosub</code>.
<syntaxhighlight lang="qbasic">100 ! Jump anywhere
110 CLEAR
120 PRINT "First line."
130 GOSUB 160
140 PRINT "Fifth line."
150 GOTO 210
160 ! sub1:
170 PRINT "Second line."
180 GOSUB 250
190 PRINT "Fourth line."
200 RETURN
210 ! Ending:
220 PRINT "We're just about done..."
230 GOTO 270
240 ! sub2:
250 PRINT "Third line."
260 RETURN
270 ! Finished:
280 PRINT "... with goto and gosub, thankfully."
290 END</syntaxhighlight>
{{out}}
<pre>Same as FutureBasic entry.</pre>
 
==={{Header|Tiny BASIC}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
=={{header|BQN}}==
Line 639 ⟶ 911:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobolcobolfree"> IDENTIFICATION DIVISION.
PROGRAM-ID. JUMPSjumps-PROGRAMprogram.
* Nobody writes like this, of course; but...
 
PROCEDURE DIVISION.
PROCEDURE DIVISION.
 
* You can jump anywhere you like.
start-paragraph.
START-PARAGRAPH.
GO TO ANan-ARBITRARYarbitrary-PARAGRAPHparagraph.
 
YET-ANOTHER-PARAGRAPH.
yet-another-paragraph.
ALTER START-PARAGRAPH TO PROCEED TO A-PARAGRAPH-SOMEWHERE.
ALTER start-paragraph TO PROCEED TO a-paragraph-somewhere.
* That's right, folks: we don't just have GO TOs, we have GO TOs whose
* destinations can be changed at will, from anywhere in the program,
* at run time.
GO TO STARTstart-PARAGRAPHparagraph.
* But bear in mind: once you get there, the GO TO no longer goes to
* where it says it goes to.
 
A-PARAGRAPH-SOMEWHERE.
a-paragraph-somewhere.
DISPLAY 'Never heard of him.'
DISPLAY 'Never heard of him.'
STOP RUN.
STOP RUN.
SOME-OTHER-PARAGRAPH.
 
some-other-paragraph.
* You think that's bad? You ain't seen nothing.
GO TO YETyet-ANOTHERanother-PARAGRAPHparagraph.
 
AN-ARBITRARY-PARAGRAPH.
an-arbitrary-paragraph.
DISPLAY 'Edsger who now?'
DISPLAY 'Edsger who now?'
GO TO SOME-OTHER-PARAGRAPH.</syntaxhighlight>
GO TO some-other-paragraph.
 
END PROGRAM jumps-program.</syntaxhighlight>
{{out}}
<pre>Edsger who now?
Line 669 ⟶ 949:
'''COBOL''' also supports computed go to phrases, given a list of labels (paragraph names) and an integer index into that list.
 
<syntaxhighlight lang="cobolcobolfree">01 provinceDATA pic 99 value 2DIVISION.
WORKING-STORAGE SECTION.
GO TO quebec, ontario, manitoba DEPENDING ON province</syntaxhighlight>
01 province PICTURE IS 99 VALUE IS 2.
 
PROCEDURE DIVISION.
GO TO quebec, ontario, manitoba DEPENDING ON province.
* Jumps to section or paragraph named 'ontario'.</syntaxhighlight>
 
=={{header|Common Lisp}}==
Line 981 ⟶ 1,266:
end.
</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
^|EMal has no goto statement.
|The closes statements are break, continue, return, exit
|and exceptions management.
|^
fun sample = void by block
for int i = 1; i < 10; ++i
if i == 1 do continue end # jumps to next iteration when 'i' equals 1
writeLine("i = " + i)
if i > 4 do break end # exits the loop when 'i' exceeds 4
end
for int j = 1; j < 10; ++j
writeLine("j = " + j)
if j == 3 do return end # returns from the function when 'j' exceeds 3
end
end
sample()
type StateMachine
^|this code shows how to selectevely jump to specific code
|to simulate a state machine as decribed here:
|https://wiki.tcl-lang.org/page/A+tiny+state+machine
|Functions return the next state.
|^
int n = -1
Map stateMachine = int%fun[
0 => int by block
if Runtime.args.length == 1
n = when(n == -1, int!Runtime.args[0], 0)
else
n = ask(int, "hello - how often? ")
end
return when(n == 0, 2, 1)
end,
1 => int by block
if n == 0 do return 0 end
writeLine(n + " Hello")
n--
return 1
end,
2 => int by block
writeLine("Thank you, bye")
return -1
end]
int next = 0
for ever
next = stateMachine[next]()
if next == -1 do break end
end
</syntaxhighlight>
{{out}}
<pre>
emal.exe Org\RosettaCode\JumpAnywhere.emal 2
i = 2
i = 3
i = 4
i = 5
j = 1
j = 2
j = 3
2 Hello
1 Hello
Thank you, bye
</pre>
 
=={{header|Erlang}}==
Jumps are limited to [[Exceptions]].
Line 2,855 ⟶ 3,206:
This code uses the Continuation object <code>c</code> to jump to the top of the loop. For the first iteration, <code>callcc</code> creates <code>c</code> and returns <code>[c, 1]</code>. For later iterations, <code>callcc</code> returns <code>[c, i + 1]</code>. For the last iteration, <code>callcc</code> returns <code>nil</code> to break the loop.
 
=={{header|ScalaSNOBOL4}}==
 
Goto's are in the European programmer community [http://en.wikipedia.org/wiki/Considered_harmful considered harmful]. They are error-prune and not essential. A good programmer would stay away from that. Scala is not equipped with goto's.
Branches are the only kind of control structure in SNOBOL4. They come in three flavours (and one compound one):
 
* <code>:(LABEL_UNCONDITIONAL)</code>
* <code>:S(LABEL_ON_SUCCESS)</code>
* <code>:F(LABEL_ON_FAILURE)</code>
* <code>:S(LABEL_ON_SUCCESS)F(LABEL_ON_FAILURE)</code> or <code>:F(LABEL_ON_FAILURE)S(LABEL_ON_SUCCESS)</code>
 
<syntaxhighlight lang="snobol4">
* Demonstrate an unconditional branch.
OUTPUT = "Unconditional branch." :(SKIP1.START)
OUTPUT = "This will not display."
 
* Demonstrate a branch on success.
SKIP1.START v = 1
SKIP1.LOOP gt(v, 5) :S(SKIP2.START)
OUTPUT = "Iteration A" v
v = v + 1 :(SKIP1.LOOP)
 
* Demonstrate a branch on failure.
SKIP2.START v = 1
SKIP2.LOOP le(v, 5) :F(SKIP3.START)
OUTPUT = "Iteration B" v
v = v + 1 :(SKIP2.LOOP)
 
* Demonstrate a combined branch.
* Demonstrate also an indirect branch.
SKIP3.START v = 0
label = "SKIP3.LOOP"
SKIP3.LOOP v = v + 1
le(v, 5) :S(SKIP3.PRINT)F(EXIT)
SKIP3.PRINT OUTPUT = "Iteration C" v :($label)
 
EXIT OUTPUT = "Goodbye"
 
END
</syntaxhighlight>
 
{{Out}}
 
<pre>
Unconditional branch.
Iteration A1
Iteration A2
Iteration A3
Iteration A4
Iteration A5
Iteration B1
Iteration B2
Iteration B3
Iteration B4
Iteration B5
Iteration C1
Iteration C2
Iteration C3
Iteration C4
Iteration C5
Goodbye
</pre>
 
=={{header|SPL}}==
Line 2,893 ⟶ 3,302:
=={{header|Tcl}}==
Tcl has both [[Exceptions#Tcl|exceptions]] and (from 8.6 onwards) [[Generator#Tcl|generators/coroutines]] but no unstructured goto ability. However, the main case where it might be desired, coding a general state machine, can be handled through metaprogramming (as discussed at some length on [http://wiki.tcl.tk/8363 the Tcler's Wiki]) so the absence is not strongly felt in practice.
 
=={{header|TXR}}==
{{trans|Common Lisp}}
 
TXR Lisp has a <code>tagbody</code> similar to Common Lisp. Like the Common Lisp one, it establishes an area of the program with forms labeled by symbols or numbers. The forms can branch to these symbols or numbers using <code>go</code>.
 
When a form initiates a branch, it is gracefully abandoned, which means that unwinding takes place: <code>unwind-protect</code> clean-up forms are called. Once the form is abandoned, control then transfers to the target form.
 
A <code>go</code> transfer may be used to jump out of a lexical closure, if the <code>tagbody</code> is still active. If a closure is captured in a <code>tagbody</code> which then terminates, and that closure is invoked, and tries to use <code>go</code> to jump to adjacent forms in that terminated tagbody, it is an error. An example of this follows, from an interactive session:
 
<pre>1> (let (fun)
(tagbody
again
(set fun (lambda () (go again))))
[fun])
** expr-1:4: return*: no block named #:tb-dyn-id-0028 is visible
** during evaluation of form (return* #:tb-id-0024
0)
** ... an expansion of (go again)
** which is located at expr-1:4</pre>
 
The above error messages reveal that TXR Lisp's <code>tagbody</code> is implemented by macros, and relies on a dynamic block return. It is provided mainly for compatibility; Common Lisp users using TXR Lisp may find it handy.
 
If the <code>tagbody</code> is still active when the lambda tries to perform a <code>go</code>, it works:
 
<pre>2> (let (fun)
(tagbody
(set fun (lambda () (go out)))
[fun]
(put-line "this is skipped")
out
(put-line "going out")))
going out
nil</pre>
 
 
The translated Common Lisp example follows:
 
<syntaxhighlight lang="txrlisp">(tagbody
beginning
(put-line "I am in the beginning")
(usleep 1000000)
(go end)
middle
(put-line "I am in the middle")
(usleep 1000000)
(go beginning)
end
(put-line "I am in the end")
(usleep 1000000)
(go middle))</syntaxhighlight>
{{out}}
<pre>
I am in the beginning
I am in the end
I am in the middle
I am in the beginning
I am in the end
I am in the middle
I am in the beginning
...</pre>
 
=={{header|VBA}}==
Line 2,926 ⟶ 3,396:
In VBScript, there is no <code>goto</code> statement. It is a good thing for structured programming.
 
=={{header|V (Vlang)}}==
V (Vlang) and 'goto':
 
1) 'Goto' used only with 'unsafe' statements.
 
2) 'Goto', only allowed unconditionally jumping to a label within the function it belongs to.
 
3) Labelled 'break' and 'continue' statements, are among the preferred alternatives to 'unsafe goto' usage.
 
4) Labelled 'break' and 'continue' allow easy breaking out of a nested loop or continuing within a containing loop.
<syntaxhighlight lang="v (vlang)">
// Unsafe 'goto' pseudo example:
 
Line 3,006 ⟶ 3,476:
 
The Fiber class also has a ''try'' method for catching errors (which won't be described here) and a static ''abort'' method which (unless caught) exits the script altogether if an error occurs.
<syntaxhighlight lang="ecmascriptwren">var func = Fn.new {
for (i in 1..10) {
if (i == 1) continue // jumps to next iteration when 'i' equals 1
Line 3,044 ⟶ 3,514:
resuming
aborting
[./jumpJump_anywhere line 17] in new(_) block argument
</pre>
 
Line 3,175 ⟶ 3,645:
{{omit from|Oberon-2}}
{{omit from|UNIX Shell|Does not have goto or line labels, but can break from nested loops}}
{{omit from|Insitux}}
9,476

edits