Out of segment space, please explain

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

Out of segment space, please explain

pikachu
I am making a basic "Guess the number" game before going into a full game.
I am getting an "out of segment space" problem, specifically:

Out of segment space in Guess_Number.main.2

And here's my code:

class Main {
        function void main () {
                var n, number, tries;
                let number = 64; // This is where the error is.  In the VM code, this is pop local 1
                let tries = 7;
                while (tries > 0) & ((n > number) | (n < number)) {
                        let n = Keyboard.readInt("Guess the number:");
                        if (n > number) {
                                Output.printString("Lower!");
                                let tries = tries - 1;
                        else {
                                if (n < number) {
                                        Output.printString("Higher!");
                                        let tries = tries - 1;
                                }
                                else {
                                        Output.printString("Correct!");
                                }

                        }
                }
                if (tries = 0) {
                        Output.printString("Out of tries!");
                }
                return;
        }
}

What is this "out of segment space" telling me?  And what's the proper way of wording this assignment?

Pikachu
Reply | Threaded
Open this post in threaded view
|

Re: Out of segment space, please explain

cadet1620
Administrator
pikachu wrote
I am making a basic "Guess the number" game before going into a full game.
I am getting an "out of segment space" problem, specifically:

Out of segment space in Guess_Number.main.2

And here's my code:

class Main {
        function void main () {
                var n, number, tries;
                let number = 64; // This is where the error is.  In the VM code, this is pop local 1
                let tries = 7;
                while (tries > 0) & ((n > number) | (n < number)) {
                        let n = Keyboard.readInt("Guess the number:");
...

What is this "out of segment space" telling me?  And what's the proper way of wording this assignment?

Pikachu
"out of segment space" means that you are trying to reference beyond the end of on eof the VM's virtual segments, for instance trying to use arg 2 on a function that was called with only one argument. The ".2" in the message means the second VM command in the function.

What compiler did you use? Your code has multiple syntax problems.  When I run it through JackCompiler, I get:
In Main.jack (line 3): In subroutine main: Expected a type followed by comma-seperated variable names
In Main.jack (line 6): In subroutine main: Expected {
In Main.jack (line 6): In subroutine main: Expected statement(do, let, while, return or if)
The line 3 problem is that you need to add the variable type:
    var int n, number, tries;


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

Re: Out of segment space, please explain

pikachu
I must have put my buggy version in.
How do you fix the "Out of segment space" problem?

class Guess_Number {
        function void main () {
                var int n, number, tries;
                let number = 64;
                let tries = 3;
  let n = 0;
                while ((tries > 0) & (n > number)) {
                        let n = Keyboard.readInt("Guess the number:");
                        if (n > number) {
                                do Output.printString("Lower!");
                                let tries = tries - 1;
                        }
                        else {
                                if (n < number) {
                                        do Output.printString("Higher!");
                                        let tries = tries - 1;
                                }
                                else {
                                        do Output.printString("Correct!");
                                }

                        }
                }
                if (tries = 0) {
                        do Output.printString("Out of tries!");
                }
                return;
        }
}

Pikachu
Reply | Threaded
Open this post in threaded view
|

Re: Out of segment space, please explain

cadet1620
Administrator
Since your program is a single file, it must be named "Main.jack" and it must be class "Main".

If I change your first line to read
class Main /*was Guess_Number*/ {
I can compile and run your code with no crash.  It immediately exits due to errors in you program's logic.

Make sure that there are no other .jack files and no other .vm files in the directory with your game.

If there are other .jack files, the JackCompiler will compile all of them to .vm files. If there are other .vm files, the VMEmulator will load them all depending on how you use it. This can cause you problems with unexpected code running.

----------

A simple way to debug is to put printString calls in the code to tell you where it gets to.
class Main /*was Guess_Number*/ {
    function void main () {
        var int n, number, tries;
        let number = 64;
        let tries = 3;
  let n = 0;
do Output.printString("*** before the while"); do Output.println();     /* DEBUG */
        while ((tries > 0) & (n > number)) {
do Output.printString("*** top of the while"); do Output.println();     /* DEBUG */
            let n = Keyboard.readInt("Guess the number:");
            if (n > number) {
                do Output.printString("Lower!");
                let tries = tries - 1;
            }
            else {
                if (n < number) {
                    do Output.printString("Higher!");
                    let tries = tries - 1;
                }
                else {
                    do Output.printString("Correct!");
                }

            }
        }
do Output.printString("*** after the while"); do Output.println();      /* DEBUG */
        if (tries = 0) {
            do Output.printString("Out of tries!");
        }
        return;
    }
} 
Running this version prints
*** before the while
*** after the while
and puts up the "Program Halted: Main.main finished execution" message box.

--Mark