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.
Please help me fix this.
P.S My program passes the test created by "cadet1620" from this post.
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.
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 261," into the test script to make it work. Right now my code passes the test on my computer but fails the Autograder.
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:
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:
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.
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.
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.