projects/07/VMtranslator/CodeWriter.php
/**
     * NB this code is slightly different to writePushSegment
     * in that it directly using the temp registries
     * rather than the registries pointing to the addresses.
     */
NB this code is slightly different to writePushSegment
in that it directly using the temp registries
rather than the registries pointing to the addresses.
NB this code is slightly different to writePushSegment in that it directly using the temp registries rather than the registries pointing to the addresses .
protected function writePushPopSegmentDirect($label, $command, $index)
    {
        $this->writeLine('// segment direct');
        if ($command == 'C_PUSH') {
            $this->writeLine('@' . $index);
            $this->writeLine('D=A       // Store the index value in D');
            $this->writeLine("@$label   // set address to $label");
            $this->writeLine("A=D+A     // set the address to be address of $label + index");
            $this->writeLine('D=M       // store the push value in D');
            $this->writePush();
        } else {
            if ($index == 0) {
                $this->writePop();
                $this->writeLine('D=M   // store popped value in D');
                $this->writeLine("@$label   // set address to $label");
                $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+A  // store the address of $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');
            }
        }
    }
protected function writePushPopSegmentDirect ( $ label , $ command , $ index ) { $ this -> writeLine ( '// segment direct' ) ; if ( $ command == 'C_PUSH' ) { $ this -> writeLine ( '@' . $ index ) ; $ this -> writeLine ( 'D=A       // Store the index value in D' ) ; $ this -> writeLine ( " @ $ label    // set address to  $ label " ) ; $ this -> writeLine ( " A=D+A     // set the address to be address of  $ label  + index " ) ; $ this -> writeLine ( 'D=M       // store the push value in D' ) ; $ this -> writePush ( ) ; } else { if ( $ index == 0 ) { $ this -> writePop ( ) ; $ this -> writeLine ( 'D=M   // store popped value in D' ) ; $ this -> writeLine ( " @ $ label    // set address to  $ label " ) ; $ 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+A  // store the address of  $ 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' ) ; } } }
NB this code is slightly different to writePushSegment
 in that it directly using the temp registries
 rather than the registries pointing to the addresses.
NB this code is slightly different to writePushSegment in that it directly using the temp registries rather than the registries pointing to the addresses .
// Find where the value is stored. // M now countains what was popped off the stack. // Find where to store the value. // M now countains what was popped off the stack.