Is JACK language/runtime has specification about memory initialize?

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

Is JACK language/runtime has specification about memory initialize?

bupjae
It seems that provided VMEmulator writes all RAM to zero (except RAM[0] = 256) when it first runs or I clicked RESET button.

However, in reality, memory would be full of garbage value if someone didn't do memory initialization.

Question:

1. Is content of static variable is zero when program is started, according to JACK language specification?
If the answer is "YES", I think I have to write code to initialize static variable region (RAM[16]~RAM[255]) to zero. Maybe Memory.init()?

2. Is content of memory block returned by Memory.alloc() is zero-initialized, according to JACK OS specification?
If the answer is "YES", I think I have to write code to initialize memory block to zero on Memory.alloc().


My current guess is "NO" for both question, because provided OS VM doesn't seem to have such initialize code.
Reply | Threaded
Open this post in threaded view
|

Re: Is JACK language/runtime has specification about memory initialize?

dolomiti7
You are right, there is no guaranteed initialization of the RAM (neither on the Hack platform, nor from the Hack OS API, nor from the Jack Language). The only thing that is guaranteed are the values of local variables which are reset to 0 when a function is entered (as part of the VM specification of the FUNCTION command). This is somehow a bit counter-intuitive, since in many languages and platforms this is the opposite (i.e. in Java static class variables and instance variables are automatically initialized to 0 whereas local variables are not initialized - for a good reason).

Your conclusion on how to resolve this is not correct though. You just have to take care of the initialization of variables yourself in the respective Jack code. Your proposal would move the responsibility of the initialization to the Operating system. If you rely on Memory.init or Memory.alloc to take care of that, your code might run correctly with your own OS implementation, but fail with other ones, including the stock OS provided with nand2tetris.

The standard way of handling this in Jack is:
1. for static variables a static function has to handle the initialization. Typically it is called init() (not a fixed convention though).  It is then the programmer's responsibility to call that function before using the class. For the OS this is handled by Sys.init which calls all required init routines of the other static OS classes. For own classes, the required init routines are typically called from Main.main.
2. for fields (instance variables) the constructor of the class should initialize the fields.
3. for arrays, code to initialize the array has to be placed where and if required.
Reply | Threaded
Open this post in threaded view
|

Re: Is JACK language/runtime has specification about memory initialize?

bupjae
This post was updated on .
The answer was "NO" for both question, so I don't have to write such code in OS.

Moreover, your answer seems not "don't have to", but "should not".

I was used to languages which initializes static variable and/or heap-allocated memory to zero but not local variables, so specification of JACK (+OS) is somewhat unfamiliar.

Anyway, I know specification now and I understand that these initialization should be done by end-user code, not OS nor VM.

Thanks for the answer.