Implementing function, pushing nVars on stack

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

Implementing function, pushing nVars on stack

ronaldmaas
I'm probably getting confused.
Implementing a function call is defined as:

g:
  repeat nVars times:
  push 0

Does this mean that the assembler code loops round nVars times, generating commands to push 0 onto the stack, or does it mean the assembler needs to generate asm code to loop round nVars times?

i.e.
for i in 0..nVars
  write @0
  write D=A
  write @SP
  write A=M
  write M=D
  write @SP
  write M=M+1
end for

or use asm code to loop round, store nVars in a temp register, and loop round in the asm code until the temp register becomes 0, and the required number of push 0 has been performed.

Thanks,

Ronald
 
Reply | Threaded
Open this post in threaded view
|

Re: Implementing function, pushing nVars on stack

cadet1620
Administrator
ronaldmaas wrote
Implementing a function [header] is defined as:

g:
  repeat nVars times:
  push 0

Does this mean that the assembler code loops round nVars times, generating commands to push 0 onto the stack, or does it mean the assembler needs to generate asm code to loop round nVars times?
...
or use asm code to loop round, store nVars in a temp register, and loop round in the asm code until the temp register becomes 0, and the required number of push 0 has been performed.
You can do it either way, what matters is what the Hack machine state is at the end of the header code.

If your translator loops calling writePush("const", 0) you end up with longer code; if your translator writes code that loops, you end up with shorter code that runs a bit slower.

You can write shorter linear code by eliminating extra SP increments.  Write all the zeros, then add nVars to SP.  

My translator chooses between these options based on nVars so that it writes the shortest code.

--Mark