I defined some global and some local purebasic variables (local = inside a procedure). Then I went to ASM-mode and made a CALL to an assembly subroutine. Inside this subroutine, I checked the values of the purebasic variables - they were all wrong (shifted, see example below).
I know, if I do a CALL, the address of it is pushed on the stack, so that the subroutine can return to the call (after the call). So my next test was to get this address from the stack (POP). After that, I checked the values of the purebasic variables again - and guess what: They were all correct now!
I made the same test with global variables and outside a procedure, so i called my subroutine and checked the purebasic variables. They were all correct.
Now my question: Why is there a difference between inside and outside a procedure? Doesn't make sense for me.
Code for 64 bit OS (Windows 10):
Code: Select all
EnableExplicit
; Some really BAD code on Windows 10, 64 bit, don't blame me for this crap ;-)
; Assembly test: CALL within a procedure
Declare main()
Global *jumpBackTo
Global iAmGlobal1.l=5
Global iAmGlobal2.l=6
Global iAmGlobal3.l=7
Debug("Global test:")
EnableASM
CALL subroutineGlobal
JMP gameoverGlobal
!subroutineGlobal:
Debug("Values of iAmGlobal1-3 before POP")
Debug(iAmGlobal1)
Debug(iAmGlobal2)
Debug(iAmGlobal3)
POP rax
MOV *jumpBackTo,rax
Debug("Values of iAmGlobal1-3 after POP")
Debug(iAmGlobal1)
Debug(iAmGlobal2)
Debug(iAmGlobal3)
Debug(">>> IDENTICAL :-)")
JMP *jumpBackTo
!gameoverGlobal:
DisableASM
Debug("-----End of global test---------")
main()
End
; -----------Procedures---------------------------
Procedure main()
Define iAmLocal1.l=1
Define iAmLocal2.l=2
Define iAmLocal3.l=3
Debug("Local test")
EnableASM
CALL subroutineSub
JMP gameoverSub
!subroutineSub:
Debug("Values of iAmLocal1-3 and iAmGlobal1 before POP")
Debug(iAmLocal1)
Debug(iAmLocal2)
Debug(iAmLocal3)
Debug(iAmGlobal1)
Debug(">>> Huh?!?")
POP rax
MOV *jumpBackTo,rax
Debug("Values of iAmLocal1-3 and iAmGlobal1 after POP")
Debug(iAmLocal1)
Debug(iAmLocal2)
Debug(iAmLocal3)
Debug(iAmGlobal1)
Debug(">>> Muuuuuch better! But why?")
JMP *jumpBackTo
!gameoverSub:
DisableASM
Debug("-----End of local test---------")
EndProcedure
DEBUGGER OUTPUT:
Global test:
Values of iAmGlobal1-3 before POP
5
6
7
Values of iAmGlobal1-3 after POP
5
6
7
>>> IDENTICAL
-----End of global test---------
Local test
Values of iAmLocal1-3 and iAmGlobal1 before POP
0
1
2
5
>>> Huh?!?
Values of iAmLocal1-3 and iAmGlobal1 after POP
1
2
3
5
>>> Muuuuuch better! But why?
-----End of local test---------