Inside function Sys.init, it calls Sys.main 0; when it does, the value saved in the 'return address' is 6.
And inside Sys.main, it calls Sys.add12 1; when does, the value saved in the 'return address' is 22.
However, after running the simulation dozens of times I still can't understand the relevancy of these two numbers and how they're used. There doesn't seem to be anything significant stored in either RAM or RAM during this simulation.
If it's suppose to act as a program counter, then doesn't it make logical sense to start at 1 and increment each time a function returns?
First, let me describe how this works in assembly language programs, because that is a published specification. I will describe what I think the VM Emulator does later.
For an assembly language program, program counter means the ROM address of the currently executing instruction. When an ASM program calls a subroutine, the called subroutine needs to know what instruction in the calling program should be jumped to when the called subroutine returns.
For example the ASM code for Sys.init's call to Sys.main might look something like this:
ROM Addr Code
// call Sys.main 0
... [ASM code to push D, build the frame, and jump to Sys.main]
// pop temp 1
In this case, the return address pushed on the stack will be 157, and the return instruction at the end of Sys.main will get that number off the stack and jump to the next instruction in Sys.init.
The return addresses pushed on the stack by the VM Emulator are a bit confusing.
In the VM emulator, the program counter is a VM instruction number. Unfortunately, those numbers are not visible to users when the VM Emulator is running. They appear to be a running count of the instructions shown in the "Program" panel.