|
12
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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..!!
|
|
You are welcome and good luck with the rest of the chapter!
Now I need to somehow figure it out too.
|
|
@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)
|
|
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.
|
|
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
|
|
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.
|
|
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.
|
|
That's exactly what I did?
@SP
AM=M-1
D=M
"@"+label
D;JNE
|
|
Damn. Send me your generated Asm file. Ill look through it
|
|
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.
|
|
Okay give me some time...Ill revert back if I find the error
|
Administrator
|
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.
|
|
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
|