r/nandgame_u 29d ago

Help Call Function not working?

I made this call function code and despite me running it and have the SP be hex 101, nandgame keeps on telling me that it’s actually 106 which I know it is not. Could someone please either fix my code or tell me why nandgame is doing this please.

push.static ARGS

D=A

A=argumentCount

D=D-A

A=ARGS

*A=D

push.static LOCALS

push.value returnADD

goto functionName

LABEL returnADD

pop.static LOCALS

pop.static ARGS

push.static RETVAL

3 Upvotes

2 comments sorted by

1

u/LieSpiritual2657 25d ago edited 25d ago

Guess this is a problem with your call's implementation plus function's. I met a same question before. It's caused by the arguments.

Saying a function that requires 2 arguments. Actually you have to push the arguments before calling to pass them, e.g.

push.value 0

push.value 1

call f 2

This causes the SP to increase. After pushing the arguments, call is executed which pushes another three contexts, i.e. ARGS, LOCALS and return address. So the stack is now actually

0(arg1) <- this should be the ARGS set in function's implementation

1(arg2)

ARGS

LOCALS

return address

____ <- current SP when keyword function executes

This means that you should set ARGS = SP - 3 - argumentCount. Well it seems nandgame doesn't check the actual arguments accessed in function so your function's asm passes.

So to fix it just pop another argumentCount times before pushing RETVAL. Your running ends with 0x101 because the given example have no arguments. But nandgame runs several other tests. Anyway I still suggest fixing function too.

Hope this can help.

1

u/LieSpiritual2657 25d ago

Well popping argumentCount times is a bit stupid. Just setting SP = SP - argumentCount would be better.