Passed the first 3 tests but got stuck in the NestedCall.vm

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

Passed the first 3 tests but got stuck in the NestedCall.vm

ajksdf
I have write the following code for the ```call``` section. I wonder if I have done sth wrong since I have re-read it a couple of times and they make sense to me.

I have passed the first 3 tests. Can I assume that the ```return```, ```function``` commands don't need modification?

Any observations are welcomed. Thank you very much.

```
//call Sys.add12 1
//1. push address label                    
@returnAddr
D=A
@SP
A=M
M=D
@SP
M=M+1

//2.push LCL
@LCL
D=M
@SP
A=M
M=D
@SP
M=M+1

//3.push ARG
@ARG
D=M
@SP
A=M
M=D
@SP
M=M+1

//4.push THIS
@THIS
D=M
@SP
A=M
M=D
@SP
M=M+1

//5.push THAT
@THAT
D=M
@SP
A=M
M=D
@SP
M=M+1

//6.reposition ARG(SP-(5+n arguments))
@5
D=A
@1
D=D+A
@SP
D=M-D
@ARG
M=D

//7.reposition *LCL = *SP
@SP
D=M
@LCL
M=D
@Sys.add12
0;JMP
(returnAddr)
```
Reply | Threaded
Open this post in threaded view
|

Re: Passed the first 3 tests but got stuck in the NestedCall.vm

WBahn
Administrator
Don't just look it over -- humans tend to see what we expect/want to see and not what is actually there.

Take out a sheet of paper and walk through the execution of a call and a return manually, step by step, in writing (don't do any of it in your head, which will force you to work at the level of what's there and not what you think is there). See if what your memory ends up like at the end is really what you expect it to be.

The NestedCall tests are looking at what happens when one function gets called and then calls another in turn. If you are not having problems with a single call/return, then the problem is usually either the return address is not being handled properly or the return value is not being handled properly. These are things that can behave as expected with a single call/return, but get messed up when multiple nested calls are made.
Reply | Threaded
Open this post in threaded view
|

Re: Passed the first 3 tests but got stuck in the NestedCall.vm

ajksdf
Making my reply to point form may be easier to read

1)I have tried the pen and paper method and the code is make sense to me.
2) For the return problem, I use the same variable name(returnAddr) for the return address which may be problematic. Not sure this will cause problems. Below is my reference code.
3)I have read the book and I see it mentioned bootstrap code. If I am using the .tst file for testing. I can choose not to add the bootstrap code right?


Reference code

```
//'return' asm code
            @5
            D=A

            @LCL
            D=M-D
            A=D    
            D=M

            @returnAddr 
            M=D

            @LCL
            D=M

            @endFrame //should equal to where LCL is pointing at now
            M=D


            @SP
            AM=M-1
            D=M

            @ARG
            A=M
            M=D

            @ARG
            D=M

            @SP
            M=D+1

            //reset those saved frame previously
            @4
            D=A
            @endFrame
            D=M-D
            A=D
            D=M
           
            @LCL
            M=D


            @3
            D=A
            @endFrame
            D=M-D
            A=D
            D=M

            @ARG
            M=D


            @2
            D=A
            @endFrame
            D=M-D
            A=D
            D=M

            @THIS
            M=D


            @1
            D=A
            @endFrame
            D=M-D
            A=D
            D=M
           
            @THAT
            M=D

     (returnAddr)
            A=M
            0;JMP
```