Difference between revisions of "0051"
Jump to navigation
Jump to search
Junior Djjr (talk | contribs) (Created page with "{{OpCode | games = {{Icon|t}} | command = RETURN | description = Return from the current subroutine. | syntax1 = 0051: return | p1t = ''none'' }} After...") |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 7: | Line 7: | ||
}} | }} | ||
− | + | This opcode returns from the current subroutine. | |
+ | When the script encounters a [[0051]], it will return from where the last [[0050]] was called. | ||
The game will crash if: | The game will crash if: | ||
* [[0051]] was read before any [[0050]] was called. | * [[0051]] was read before any [[0050]] was called. | ||
+ | |||
+ | == Implementation == | ||
+ | See ''[[0050#Implementation|gosub]]'' for further details. | ||
+ | |||
+ | <syntaxhighlight lang="cpp"> | ||
+ | /* Note: This is not real code and will not compile. */ | ||
+ | /* Based on decompilation of SA v2.0 */ | ||
+ | |||
+ | /* | ||
+ | The game will crash if the call stack is empty | ||
+ | (i.e. script->callStackPosition == 0), because | ||
+ | script->callStackPosition - 1 would be -1, and | ||
+ | therefore an invalid index. | ||
+ | |||
+ | This can happen if "return" is used outside a | ||
+ | subroutine. | ||
+ | */ | ||
+ | |||
+ | // Move up to the previous stack frame. | ||
+ | script->callStackPosition = script->callStackPosition - 1; | ||
+ | |||
+ | // Set the current offset to the previous one. | ||
+ | // The previous offset will be the offset of the next instruction after the ''gosub''. | ||
+ | script->readPointer = (&script->callStack)[script->callStackPosition]; | ||
+ | </syntaxhighlight> | ||
== Keywords == | == Keywords == | ||
subroutine, gosub, go sub | subroutine, gosub, go sub |
Latest revision as of 15:07, 1 July 2021
- Description
- Return from the current subroutine.
- Syntax
- 0051: return
- Parameter
- none
This opcode returns from the current subroutine. When the script encounters a 0051, it will return from where the last 0050 was called.
The game will crash if:
Implementation
See gosub for further details.
/* Note: This is not real code and will not compile. */
/* Based on decompilation of SA v2.0 */
/*
The game will crash if the call stack is empty
(i.e. script->callStackPosition == 0), because
script->callStackPosition - 1 would be -1, and
therefore an invalid index.
This can happen if "return" is used outside a
subroutine.
*/
// Move up to the previous stack frame.
script->callStackPosition = script->callStackPosition - 1;
// Set the current offset to the previous one.
// The previous offset will be the offset of the next instruction after the ''gosub''.
script->readPointer = (&script->callStack)[script->callStackPosition];
Keywords
subroutine, gosub, go sub