Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling Scripts with Parameters Uses Stack but Never Frees Stack Prior to Script Execution #1671

Open
MaverickLoneshark opened this issue Jan 13, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@MaverickLoneshark
Copy link

MaverickLoneshark commented Jan 13, 2025

Description

Calling a script with a parameter pushes the parameter on to the conventional stack but never pops the parameter from the stack prior to script execution.

If you try to reference the parameter itself after altering the stack in your script, you WILL alter the value of the parameter!

To Reproduce

Alter the stack prior to referencing the parameter.

For example:
Pushing onto the stack before selecting the parameter in a Switch-Case and only popping the stack after the Switch-Case

Without altering the stack, this behaves as expected
image

.module script_23

.include "vm.i"
.include "data/game_globals.i"

.area _CODE_255

.SCRIPT_ARG_INDIRECT_0_VARIABLE = -3

___bank_script_23 = 255
.globl ___bank_script_23

_script_23::
; Switch

        ; Switch Variable
        VM_PUSH_VALUE_IND       .SCRIPT_ARG_INDIRECT_0_VARIABLE
        VM_SWITCH               .ARG0, 2, 1
        .dw 1, 1$
        .dw 2, 2$

        VM_JUMP                 3$
        ; case 1:
1$:
; // Case 1

        VM_JUMP                 3$
        ; case 2:
2$:
; // Case 2

        VM_JUMP                 3$
3$:

        VM_RET_FAR_N            1

...but this does NOT!
image

.module script_23

.include "vm.i"
.include "data/game_globals.i"

.area _CODE_255

.SCRIPT_ARG_INDIRECT_0_VARIABLE = -3

___bank_script_23 = 255
.globl ___bank_script_23

_script_23::
; GBVM Script

        VM_RESERVE 1

; Switch

        ; Switch Variable
        VM_PUSH_VALUE_IND       .SCRIPT_ARG_INDIRECT_0_VARIABLE
        VM_SWITCH               .ARG0, 2, 1
        .dw 1, 1$
        .dw 2, 2$

        VM_JUMP                 3$
        ; case 1:
1$:
; // Case 1

        VM_JUMP                 3$
        ; case 2:
2$:
; // Case 2

        VM_JUMP                 3$
3$:

; GBVM Script

        VM_POP 1

        VM_RET_FAR_N            1

Expected behavior

Shouldn't parameters be relocated into special reserved memory (like a separate, dedicated stack) and the conventional stack popped prior to the script's execution?

Platform

Windows 10
image

@MaverickLoneshark MaverickLoneshark added the bug Something isn't working label Jan 13, 2025
@MaverickLoneshark MaverickLoneshark changed the title Calling Scripts with Parameters Uses Stack But Never Frees Stack Prior to Script Execution Calling Scripts with Parameters Uses Stack but Never Frees Stack Prior to Script Execution Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant