Clearing memory on stack

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Clearing memory on stack

63rrit
Hey there,
how do i clear the memory on my stack?
My approach would be like this:

@SP
M=M-1
A=M
M=0

or is there any asm command for 'null'?

btw:
does

M=M-1
A=M

and

AM=M-1

do the same?

Reply | Threaded
Open this post in threaded view
|

Re: Clearing memory on stack

silversilva
Well, you don't have to clear the stack. That's a speciality of stack. You only have to decrease the Stack Pointer if you need to remove it.
Reply | Threaded
Open this post in threaded view
|

Re: Clearing memory on stack

WBahn
Administrator
In reply to this post by 63rrit
Depends on what you mean by "clearing the stack".

The usual way is simply set the stack pointer to the bottom of the stack.

@256
D=A
@SP
M=D

Consider that, when you first start the machine, you don't go through as set the contents of all of the memory cells in the stack to zero -- so they start out with whatever random junk they had when the machine powered up (or would in the real world). So if your design can tolerate that, it can tolerate it when that "junk" happens to be the leftovers from prior computations.

Now, it is important to realize that leaving that junk there IS a potential security risk since it leaks information. Some software deals with this by specifically zeroing out (or writing random garbage) into cells whose values are no longer needed. But there's a significant performance penalty to pay for that added security.
Reply | Threaded
Open this post in threaded view
|

Re: Clearing memory on stack

WBahn
Administrator
In reply to this post by 63rrit
Oh, as for your second point.

M = M-1
A = M

and

AM = M-1

do do the same thing.

You should be able to verify that with pen and paper pretty easily.