Nand2tetris Project
Nand2tetris Project
ASSIGNMENT-5
SIMPLEADD
Loading
BasicTest from
MemoryAccess
from projects 07
Load text script
PointerTestVME
from the given
options
End of script Comparision ended successfully
STATICTEST
• The CodeWriter class is responsible for translating VM commands into assembly code. It has the following methods:
• __init__(self, output_file): Initializes the CodeWriter object by opening the output file for writing.
• write_arithmetic(self, command): Writes the assembly code for the given arithmetic command.
• write_push(self, segment, index): Writes the assembly code for the push command with the specified segment and index.
• write_pop(self, segment, index): Writes the assembly code for the pop command with the specified segment and index.
• write_label(self, label, function): Writes the assembly code for the label command with the specified label and function.
• write_goto(self, label, function_name): Writes the assembly code for the goto command with the specified label and function.
• write_if_goto(self, label, condition): Writes the assembly code for the if-goto command with the specified label and condition.
• write_function(self, function_name, num_locals): Writes the assembly code for the function command with the specified function name and
number of locals.
• write_return(self): Writes the assembly code for the return command.
• The main() function is responsible for reading a VM file, parsing its commands, and
generating equivalent assembly code. It initializes a Parser object with the input file path and
a line index variable. It then enters a loop to process each command in the file. For each
command, it determines the command type and calls the appropriate method in a CodeWriter
object to generate the corresponding assembly code. The function also includes print
statements to display information about each command being processed. After each
command, it writes a specific sequence of assembly code to the output file. Finally, it closes
the output file.
Q2)
HERE GLOBAL
STACK IS
EMPTY
GLOBAL STACK
Value Passed to
Global Stack as
3
GLOBAL STACK
3
saved Main frame
3 Becomes argument 0
GLOBAL STACK
3
saved Main frame
3
saved Main frame
F(3):3
F(2):2
3
saved Main frame
F(3):3
F(2):2
saved main frame
3
saved Main frame
F(3):3
F(2):2
saved main frame
3
saved Main frame
argument 0 F(2):2
F(2):1
GLOBAL STACK
3
saved Main frame
F(3):3
F(2):2
saved main frame
F(2):2
F(2):1
saved main frame
}
saved Main frame
F(3):
3
F(2):
2
saved main frame
F(2):2
F(2):1
saved main frame
3
saved Main frame
F(3):3
F(2):2
saved main frame
F(2):2
F(2):1
saved main frame
F(1):1
3
saved Main frame
F(3):3
F(2):2
saved main frame
F(2):2
F(1):1
3
saved Main frame
F(3):3
F(2):2
saved main frame
F(2):2
F(1):1
As the mult 2 function is not available we can't have the flow of the
program we can only predict the output and the oputput has been
over written as argument o successfully after few steps the program
has returned to the written address and now the program will
execute the call mult 2 comand and we consider it as a function
that multiplies 2 agruments and gives an output.
GLOBAL STACK
3
saved Main frame
F(3):3
F(2):2
saved main frame
F(2):2
F(1):1
As the mult 2 function is not available we can't have the flow of the
program we can only predict the output and the oputput has been
over written as argument o successfully after few steps the program
has returned to the written address and now the program will
execute the call mult 2 comand and we consider it as a function
that multiplies 2 agruments and gives an output.
GLOBAL STACK
• 6
• Therefore we obtain
the Result
3.)
WRITE A VM FUNCTION THAT PUSHES THE VALUE OF THE INPUT ARG ONTO
THE STACK N TIMES, WHERE N IS THE VALUE OF THE SECOND INPUT ARG.
CODE:
As I given 6 times
to push 10
Q.4
1.The virtual machine first reads the value of the "this" register. The "this" register
typically points to the current object instance that the virtual machine is operating
on. 2.The virtual machine adds the value 0 to the value of the "this" register. This
creates a memory address offset that is 0 bytes away from the start of the current
object instance.
3.The virtual machine then reads the value stored in memory at the address pointed
to by the calculated offset. This value is typically a primitive type or a reference to
another object.
4.The virtual machine pushes the value that was read from memory onto the top
of the stack. This value is now the topmost value on the stack and can be used by
subsequent instructions in the virtual machine's program.
'POINTER
S'
In a stack machine, a pointer is a value that represents a memory address. Pointers are
often used to reference data structures and objects that are stored in memory.
In stack-based virtual machines, pointers are typically implemented as integer values that
represent memory addresses. When a pointer value is pushed onto the stack, it typically
represents a reference to an object or data structure that is stored in memory.
Pointers are commonly used in stack-based virtual machines to implement complex data
structures like arrays, linked lists, and trees. By using pointer values to reference data
structures in memory, virtual machines can perform efficient memory management and
data manipulation operations.
In addition to referencing data structures, pointers are also used to implement dynamic
memory allocation and deallocation. Virtual machines can allocate memory dynamically
using the "heap", a region of memory that is managed by the virtual machine. When
memory is no longer needed, virtual machines can deallocate it using pointer values that
reference the allocated memory.
Q.5