ConvertToBin/Main.jack local variable initialization

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

ConvertToBin/Main.jack local variable initialization

Fatih Sarıkoç
Hi Everybody,

At Chapter-11 test file of ConvertToBin/Main.jack file, it seems that there is not an initial value assigment for local variables "position" and "mask". Is there a rule in the book meaning that default initial value for a local integer variable shall be zero. Here is the test code given below to let you check the initial values of "position" and "mask" parameters:



// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/11/ConvertToBin/Main.jack

/**
 * Unpacks a 16-bit number into its binary representation:
 * Takes the 16-bit number stored in RAM[8000] and stores its individual
 * bits in RAM[8001..8016] (each location will contain 0 or 1).
 * Before the conversion, RAM[8001]..RAM[8016] are initialized to -1.
 *
 * The program should be tested as follows:
 * 1) Load the program into the supplied VM Emulator
 * 2) Put some value in RAM[8000]
 * 3) Switch to "no animation"
 * 4) Run the program (give it enough time to run)
 * 5) Stop the program
 * 6) Check that RAM[8001]..RAM[8016] contains the correct binary result, and
 *    that none of these memory locations contain -1.
 */
class Main {
   
    /**
     * Initializes RAM[8001]..RAM[8016] to -1, and converts the value in
     * RAM[8000] to binary.
     */
    function void main() {
        var int result, value;
       
        do Main.fillMemory(8001, 16, -1); // sets RAM[8001]..RAM[8016] to -1
        let value = Memory.peek(8000);    // reads a value from RAM[8000]
        do Main.convert(value);           // perform the conversion
   
    return;
    }
   
    /** Converts the given decimal value to binary, and puts
     *  the resulting bits in RAM[8001]..RAM[8016]. */
    function void convert(int value) {
    var int mask, position;
    var boolean loop;
   
    let loop = true;
 
    while (loop) {
       let position = position + 1;
       let mask = Main.nextMask(mask);
            do Memory.poke(9000 + position, mask);
   
       if (~(position > 16)) {
   
           if (~((value & mask) = 0)) {
               do Memory.poke(8000 + position, 1);
               }
           else {
               do Memory.poke(8000 + position, 0);
             }    
       }
       else {
           let loop = false;
       }
    }
   
    return;
    }
 
    /** Returns the next mask (the mask that should follow the given mask). */
    function int nextMask(int mask) {
    if (mask = 0) {
       return 1;
    }
    else {
            return mask * 2;
    }
    }
Reply | Threaded
Open this post in threaded view
|

Re: ConvertToBin/Main.jack local variable initialization

cadet1620
Administrator
Fatih Sarıkoç wrote
Hi Everybody,

At Chapter-11 test file of ConvertToBin/Main.jack file, it seems that there is not an initial value assigment for local variables "position" and "mask". Is there a rule in the book meaning that default initial value for a local integer variable shall be zero.
In a quick scan of chapters 9-11 I don't see it explicitly stated that local variables ar initialized to zero.

It is explicitly stated in the VM Specification in chapter 8, that on entry to functions, the local segment is initialized to 0.  See section 8.2.3 and figure 8.5.

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

Re: ConvertToBin/Main.jack local variable initialization

Fatih Sarıkoç
Thank you Mark for pinpointing the relevant description in the book.