Re: A83:'ret'


[Prev][Next][Index][Thread]

Re: A83:'ret'




As several people suggested, you can save the stack pointer at the beginning
of your program and restore it at the end as a sort of "safety net" in case
you screw things up.  However, this is generally not good practice and
should be avoided if at all possible.  A well written program will never
need to manually restore the stack.  Usually having a corrupt stack is a
sign of a bug.  Think about it: why are you randomly pushing values that you
aren't going to pop off?  Or why are you calling a routine that you are not
going to return from?  When writing complex routines that use a lot of
registers that also involve some complex logic, it sometimes be tricky to
make sure you don't mess up the stack, but that doesn't mean you should let
it become corrupted.  It's still a bug.

About the only time you would need to clear the stack is for error handling.
But on a calculator, you don't really need to be bailing out of routines due
to random errors, simply because they don't exist.  In general programming
(such as games), there are only a couple things that I can think of that
would force you to abort the program.  The first would be not finding a
required variable, such as graphics or levels.  In this case, you would
probably do the detection at the start of the program, and it would be
trivial to exit without corrupting the stack.  The other instance would be
needing to allocate variables, either for more memory or to save a high
score.  Again, these are going to happen at some fixed point in the program,
and not just at any random time.  For something like C++'s exception
handling, saving and restoring the stack could be genuinely useful.  But as
I said before, you don't really need that on a calculator.  Using it for any
other purpose is just going to make it harder on yourself, because you're
using very poor coding methods and introducing very difficult to find bugs
into your program.

> If you use al lot of 'push' and 'pop' during a program, your calculator
can
> easily crash when trying to exit the program with the usual 'ret'(i'm
> writing for ION btw, but i don't think that has an influence)
> Is there a way of avoiding crashes or do you just have count how many
times
> you pushed a register and then pop it as many times? (and that's not so
> easy! So i hope there is another way...)





Follow-Ups: References: