Stack practice

By thegeps

Paladin (736)

thegeps의 아바타

15-03-2021, 18:23

I was wondering... is it correct (and safe) use something like this?

	ld	(stack),sp

at the beginning of the code (before pushing/popping anything) to have a "clean" stack address and restore it using

	ld	sp,(stack)

when you need to exit from some subroutine so you can "end" it anytime without caring about pop all the pushed regs?

Login or 등록 to post comments

By ro

Scribe (4363)

ro의 아바타

15-03-2021, 18:50

If you're a clean coder, you won't need such escapes.
However, it is a possibility, sure. I even read in the MSXDOS2 specs that it's a preferred exit code. No my idea of perfect, but heck. On the other hand, you can also implement your own stack and drop that during exit code.

Whatever works, not?

By jltursan

Prophet (2476)

jltursan의 아바타

15-03-2021, 18:50

Sure, it's a must if between both instructions you use "stack blasting" techniques that usually badly messes up the stack.

By Grauw

Ascended (9739)

Grauw의 아바타

15-03-2021, 19:12

thegeps wrote:

I was wondering... is it correct (and safe) use something like this?

	ld	(stack),sp

at the beginning of the code (before pushing/popping anything) to have a "clean" stack address and restore it using

	ld	sp,(stack)

when you need to exit from some subroutine so you can "end" it anytime without caring about pop all the pushed regs?

This is what is done in compiled C code to clear the function stack frame in one go before returning. Although that actually adjusts it with add (it knows the stack frame size at any point in the program). Storing it in a global is a problem if you want the function to be recursive or reentrant.

By thegeps

Paladin (736)

thegeps의 아바타

15-03-2021, 20:16

Thank you all, as always. I feel I'm improving everytime I ask you something Wink