FibonacciElement

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

FibonacciElement

kingofbuffs
This post was updated on .
Hello everyone. I have been stuck at this problem for a few days now, and I still have no idea what exactly is the problem. I have gone through the .asm file dozens of times, and it seems like the return function does not work properly.



As soon as the program hits the "return" command, everything goes astray. It jumps to the register 273 and pushes the argument 0. And then instead of pushing constant 1, it pushes constant 2. I think this is because the return command jumps to a different point.




(deleted)

(deleted)

(deleted)

Please help me fix this.

P.S My program passes the test created by "cadet1620" from this post.


Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

Eytan Lifshitz
Hi, You shouldn't share your code here, not even segments of it, and because the links you sent are broken, you actually didn't share it.

If you already pinpointed the problem, all you need to do is to start go backwards: Why it jumps to 279 instead of 273? Is it doing what it supposed to do?
If not, your code is wrong.
If it does what it supposed to do, and the problem is in wrong value in some memory register, then maybe the problem is not really here, and you should go back to the place where this wrong value was stored in memory.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

kingofbuffs
I shared the code (tried to anyway..) because I thought it would be impossible to figure out the root cause without seeing it or recreating the problem.

I have tested each function (call, function, return) individually and they seem to be working fine, at least they pass the tests that come with the course.

And I have gone back and checked it dozens of times.

Nonetheless, I will go back and try again. Probably I am missing something obvious.

P.S I deleted the code.

Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

Stormtrooper2001
You have to make sure you create a unique label at the end of the call function so you can jump back.
For my code, I used a variable that incremented itself whenever the line contained call and used($RA$+" the variable") as my return address
I can't really comment on the problem unless I see your code more in detail
Another problem I faced was that the test script didn't initialise the value of the ram so I had to add the line "set RAM[0] 261," into the test script to make it work. Right now my code passes the test on my computer but fails the Autograder.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

kingofbuffs
Did that too.

(CALL{})".format(str(callLabel))

"callLabel" is a variable that increments everytime. Although, I should give it a shot with a more random string than "CALL".


Have you implemented the bootstrap code? After that, your SP should be set at 261.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

Stormtrooper2001
Did you go through your assembly language? Also how did you get ram set to 261 please elaborate?
Which language are you coding in? I could send you my logic I wrote it in python.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

kingofbuffs
As far as I understand, there are different ways to do this, but I just pushed 5 zeros to the stack, it works since at the beginning no variable is initialized, everything is zero. (Although, I figure there is a better way of doing this.)

I coded it in python. Upon further inspection, I am absolutely sure I am messing up the return address part. So, here's the call function pushing the return address:

// push return-address
RIP$$"+str(callLabel)
D=A
@SP
A=M
M=D
@SP
M=M+1

and at the end, right after we jump to the function with @+[function name here] 0;JMP I create the label:
(RIP$${})".format(str(callLabel))

P.S please don't send me the correct code, I'd like to figure it out myself without cheating.

Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

Stormtrooper2001
In reply to this post by Stormtrooper2001
I made a mistake in my bootstrap code i only wrote
set SP 256
I forgot to implement call sys.init
after i call i get 256+5=261
thanks a lot man..!!
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

kingofbuffs
You are welcome and good luck with the rest of the chapter!

Now I need to somehow figure it out too.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

Stormtrooper2001
In reply to this post by kingofbuffs

@SP
A=M
M=D
why are u changing ur sp here? I think this might be the error.
I don't get what are u trying to do after creating the label. The label of the return address should be the last line of the call statement. The .asm file should look like this:

@Main.fibonacci
0; JMP
//label RA$2
(RA$2)
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

kingofbuffs
Because I need to push the return address to the stack. Since, SP always points to the top of the stack, I need to increment it after pushing a value to the stack.

That's exactly how the end of my call function looks too! Odd.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

Stormtrooper2001
Okay then how did you end your return statement?
does it look like this?
@R15"
A=M
0;JMP
here I used R15 to store my return address u could use anything outside the temp variable segment
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

kingofbuffs
Yes! That's actually the exact way I did.

Maybe problem is with the "RET=FRAME-5"?

I did:

@FRAME
D=M
@15
M=D
@5
D=A
@15
M=M-D

The problem is, even though my return code passes the test from "SimpleFunction", as far as I can tell, in FibonacciElement, it works incorrectly when it hits the return command, and jumps to a wrong point in the program.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

Stormtrooper2001
This also looks correct if you are passing the simple test then you might want to look at your code for "goto" and "if-goto" functions as if that isn't correct it might not return:
One common fix could be:
in week 7 for the if-goto jump, all we had to do was to ensure the value is greater than 0,
But there we have an lt statement above the if-goto and the value is -1 for true, so incase u didnt use the JNE function it will fail.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

kingofbuffs
That's exactly what I did?

@SP
AM=M-1
D=M
"@"+label
D;JNE
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

Stormtrooper2001
Damn. Send me your generated Asm file. Ill look through it
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

kingofbuffs
Just did, see if you can spot the error.

I have a feeling it's going to be something ridiculously trivial but we'll see.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

Stormtrooper2001
Okay give me some time...Ill revert back if I find the error
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

WBahn
Administrator
In reply to this post by kingofbuffs
The reason that the SP is equal to 261 is because, after initializing the SP to 256, the bootstrap code needs to call the function Sys.init. You need to set up the stack frame properly for this function, not just push a bunch of zeros onto the stack (which may work, but your translator is then making assumptions and imposing constraints on how the higher level code works).

Since you already have the code to execute a call, this should be easy to do.
Reply | Threaded
Open this post in threaded view
|

Re: FibonacciElement

kingofbuffs
This maybe a silly question, but what are asterisks meant to convey?

This one:

RET = *(FRAME-5)

or this one:

*ARG = pop()

Maybe I am missing something here?
12