Number of Args for String.appendChar in VM call

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

Number of Args for String.appendChar in VM call

peterxu422
push constant 'T'
call String.appendChar 2

Why is it that when you are building a string in the VM code and you make a call to String.appendChar, it requires 2 arguments even though the Jack specification says it takes 1 argument? I suspect the two arguments it deals with on the stack are the char constant and the pointer the string. But does that mean that within the appendChar subroutine, it re-pushes the pointer to the string at the end of the call?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Number of Args for String.appendChar in VM call

cadet1620
Administrator
peterxu422 wrote
push constant 'T'
call String.appendChar 2

Why is it that when you are building a string in the VM code and you make a call to String.appendChar, it requires 2 arguments even though the Jack specification says it takes 1 argument? I suspect the two arguments it deals with on the stack are the char constant and the pointer the string. But does that mean that within the appendChar subroutine, it re-pushes the pointer to the string at the end of the call?
You are correct.

String.appendChar is an object method, so there are two arguments: the object pointer and the character value to append. The object pointer has, presumably, already been pushed on the stack before your quoted code snippet.

String.appendChar's return type is String and it returns this (see 9.2.7), so the object pointer is on the stack when appendChar returns.

--Mark
Loading...