The use of the extra register to store value while 'pop pointer'

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

The use of the extra register to store value while 'pop pointer'

ajksdf
For 'pop pointer 1'. I perceive it as popping the the value on the top of the stack and writing it to the THAT pointer. Therefore, I write the following code but the test just keep failing. Am I missing sth?

```
                    @SP
                    AM=M-1
                    D=M

                    @THAT
                    A=M
                    M=D
```
Reply | Threaded
Open this post in threaded view
|

Re: The use of the extra register to store value while 'pop pointer'

WBahn
Administrator
That will store the value in D at the address that THAT is pointing to. You want to change the value of the THAT pointer.
Reply | Threaded
Open this post in threaded view
|

Re: The use of the extra register to store value while 'pop pointer'

ajksdf
This post was updated on .
Oic. My previous implementation is modifying the value. I have below 2 questions

1) if my interpretation of 'pop pointer 1' is correct?
2) The following code has passed the test but it is far less commands than those I take reference online. Is the following code logically correct?

//assuming the RAM contains some made up value
 ____
|_256_|  RAM 0
|_967_|  RAM 1
|_101_|  RAM 2
|_123_|  RAM 3  THIS
|_456_|  RAM 4  THAT
|_....._|  


//my code
@SP
AM=M-1
D=M  //store what is in RAM[256] to D register

@THAT  //RAM[4]
M=D     // change the location of THAT pointer pointing at

============
//other ppl's
@THAT
D=A
@R13
M=D

@SP
AM=M-1
D=M

@R13
A=M
M=D
Reply | Threaded
Open this post in threaded view
|

Re: The use of the extra register to store value while 'pop pointer'

WBahn
Administrator
Most people implement the pointer segment like the other fixed-base segments, so their code works for

push/pop pointer n

where n is any integer.

You are taking an approach that requires that you treat push/pop pointer 0 differently than push/pop pointer 1 and that you can't handle anything other than 0 or 1. There's nothing intrinsically wrong with either approach.

The "other people's" code you show look like the code for "pop that 0"
Reply | Threaded
Open this post in threaded view
|

Re: The use of the extra register to store value while 'pop pointer'

ajksdf
Glad that both of the methods will work.  

I have checked that reference. It shows 'pop pointer 1' in the comment. If doing 'pop that 6', seems that we need more step as we need to go to @THAT + 6. Assigning the SP value to @THAT + 6. Then assign the original address THAT is pointing to. Those steps I copied from the reference just seems not proceeding this operation?

Reply | Threaded
Open this post in threaded view
|

Re: The use of the extra register to store value while 'pop pointer'

WBahn
Administrator
My bad. I thought the second line was D=M and not D=A.

If someone is going to hard code the @THAT, then there is no point in using one of the temp registers. I think that's what tripped me up -- it only makes sense to store the value in the THAT register in R13 and so I was seeing what made sense and not what was actually there.
Reply | Threaded
Open this post in threaded view
|

Re: The use of the extra register to store value while 'pop pointer'

ajksdf
Indeed. I take quite a lot of time re-read the code and trying to figure out what is the reason of using the temp register(R13). It seems to use some extra command for producing the same result.

However, there are also beneficial to write more command for the sake of reviewing those code after a few weeks later. Too hard to debug in such lower level.