projects/07/VMtranslator/CodeWriter.php
/**
     * Pop the top of the stack & store it in segment[index]
     */
Pop the top of the stack & store it in segment[index]
Pop the top of the stack & store it in segment [ index ]
protected function writePopSegment($label, $index)
    {
        if ($index == 0) {
            $this->writePop();
            $this->writeLine('D=M   // store popped value in D');
            $this->writeLine("@$label   // set address to $label");
            $this->writeLine("A=M   // use the value stored in $label as the next address");
            $this->writeLine('M=D   // store the value at the address');
        } else {
            $this->writeLine('@' . $index);
            $this->writeLine('D=A    // Store the index value in D');
            $this->writeLine("@$label   // set address to $label");
            $this->writeLine("D=D+M  // store the address stored in $label + index in D");
            $this->writeLine('@R13  // temp store the address');
            $this->writeLine('M=D   // store the address in R13');
            $this->writePop();
            $this->writeLine('D=M   // store the value in D');
            $this->writeLine('@R13  // R13 address');
            $this->writeLine('A=M   // use the value stored in R13 as the next address');
            $this->writeLine('M=D   // store the value at the address');
        }
        $this->writeLine("");
    }
protected function writePopSegment ( $ label , $ index ) { if ( $ index == 0 ) { $ this -> writePop ( ) ; $ this -> writeLine ( 'D=M   // store popped value in D' ) ; $ this -> writeLine ( " @ $ label    // set address to  $ label " ) ; $ this -> writeLine ( " A=M   // use the value stored in  $ label  as the next address " ) ; $ this -> writeLine ( 'M=D   // store the value at the address' ) ; } else { $ this -> writeLine ( '@' . $ index ) ; $ this -> writeLine ( 'D=A    // Store the index value in D' ) ; $ this -> writeLine ( " @ $ label    // set address to  $ label " ) ; $ this -> writeLine ( " D=D+M  // store the address stored in  $ label  + index in D " ) ; $ this -> writeLine ( '@R13  // temp store the address' ) ; $ this -> writeLine ( 'M=D   // store the address in R13' ) ; $ this -> writePop ( ) ; $ this -> writeLine ( 'D=M   // store the value in D' ) ; $ this -> writeLine ( '@R13  // R13 address' ) ; $ this -> writeLine ( 'A=M   // use the value stored in R13 as the next address' ) ; $ this -> writeLine ( 'M=D   // store the value at the address' ) ; } $ this -> writeLine ( " " ) ; }
Pop the top of the stack & store it in segment[index]
Pop the top of the stack & store it in segment [ index ]
// No need to waste cycles looking for base + index address. // M now countains what was popped off the stack. // Find where to store the value. // M now countains what was popped off the stack.