Hey everyone! I have written a simple calculator program in Jack just to get my hands dirty. I was testing it on the VM emulator and was curious if I could get it to run on the CPU emulator.
I took the Main.vm that was the result of running my jack file through the supplied compiler and put it into a folder with all the .vm files that represent the OS. I then ran that folder through my VM translator that I built in the previous chapter. When I try to load the result into a CPU emulator however I get an error telling me that the program is too large!
Is it possible that that the VM translator I built is not working properly, even though it passed all the tests from the previous chapter? Or is there something else that I am missing?
side note: It is painfully slow to test a program on the VM emulator. Don't get me wrong it is very cool to see how it all works. Sometimes however I want it to just run the jack code as fast as possible so that I may test my code!
calc.asmI have uploaded the resulting .asm file. the code for my basic calculator is as follows:
class Main{
        function void main(){
                var int selection;
                while (true){
                        let selection = Main.mainMenu();
                        if (selection = 1){
                                do Main.addition();
                        }
                        
                        if (selection = 2){
                                do Main.subtraction();
                        }
                        
                        if (selection = 3){
                                do Main.multiplication();
                        }
                        
                        if(selection = 4){
                                do Main.division();
                        }
                        
                        if (selection = 5){
                                return;
                        }
                }
                return;
        }
        
        function int mainMenu(){
                do Output.println();
                do Output.printString("-----Main menu-----");
                do Output.println();
                do Output.printString("1. Addition");
                do Output.println();
                do Output.printString("2. Subtraction");
                do Output.println();
                do Output.printString("3. Multiplication");
                do Output.println();
                do Output.printString("4. Division");
                do Output.println();
                do Output.printString("5. Quit");
                do Output.println();
                return Keyboard.readInt("Enter selection: ");
        }
        
        function void addition(){
                var int firstNum, secondNum, answer;
                do Output.println();
                let firstNum = Keyboard.readInt("Please enter the first number: ");
                do Output.println();
                let secondNum = Keyboard.readInt("Please enter a number to add to the first: ");
                do Output.println();
                let answer = firstNum + secondNum;
                do Output.printInt(answer);
                do Output.println();
                return;
        }
        
        function void subtraction(){
                var int firstNum, secondNum, answer;
                do Output.println();
                let firstNum = Keyboard.readInt("Please enter the first number: ");
                do Output.println();
                let secondNum = Keyboard.readInt("Please enter a number to subtract from the first: ");
                do Output.println();
                let answer = firstNum - secondNum;
                do Output.printInt(answer);
                do Output.println();
                return;
        }
        
        function void multiplication(){
                var int firstNum, secondNum, answer;
                do Output.println();
                let firstNum = Keyboard.readInt("Please enter the first number: ");
                do Output.println();
                let secondNum = Keyboard.readInt("Please enter a number to multiply the first by: ");
                do Output.println();
                let answer = firstNum * secondNum;
                do Output.printInt(answer);
                do Output.println();
                return;
        }
        
        function void division(){
                var int firstNum, secondNum, answer;
                do Output.println();
                let firstNum = Keyboard.readInt("Please enter the first number: ");
                do Output.println();
                let secondNum = Keyboard.readInt("Please enter a number to divide the first by: ");
                do Output.println();
                let answer = firstNum / secondNum;
                do Output.printInt(answer);
                do Output.println();
                return;
        }
}