Jack program that the VM Emulator can not run.

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

Jack program that the VM Emulator can not run.

Patrick Clot
The following jack program induces an error in the VM emulator:

class m3 {
        function void main() {
                do.Output.printString("I")
        }
}

The Jack compiler translates this into:

function m3.main 0
push constant 1
call String.new 1
push constant 73
call String.appendChar 2
call Output.printString 1
pop temp 0
push constant 0
return

In the vmEmulator, single stepping through the program, I get to the line
"push constant 73" and before executing it I get the error message "A built-in
function tried to access memory outside the Heap or Screen range".

Anyone know why?

The state of RAM at that point is:
 SP: 0 268
LCL: 1 268
ARG: 2 262
...
The rest are 0.

The global stack is:
256 1
257 3
258 0
259 0
260 0
261 0
262 3
263 -1
264 262
265 256
266 0
...
The rest are 0.
Reply | Threaded
Open this post in threaded view
|

Re: Jack program that the VM Emulator can not run.

cadet1620
Administrator
Because you are not loading a class "Main" the VM emulator thinks that you are loading test code, not an application, so it is not calling the built in Sys.init() so none of the OS classes have been initialized.

The crash you are seeing is when String.new calls Memory.alloc() to create the string storage and the heap hasn't been initialized yet.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: Jack program that the VM Emulator can not run.

Patrick Clot
Wow. Ok, I can accept that. :)
Thanks for the quick response Mark.