Difference between revisions of "0051"
Jump to navigation
Jump to search
Junior Djjr (talk | contribs) |
m (Added implementation and linked to gosub) |
||
Line 12: | Line 12: | ||
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 |
Revision as of 12:01, 24 August 2020
- Description
- Return from the current subroutine.
- Syntax
- 0051: return
- Parameter
- none
This opcode returns from the current subroutine. When the thread 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