[go: up one dir, main page]

0% found this document useful (0 votes)
109 views72 pages

Nand2tetris Project

The code snippet shows the execution of a recursive factorial function on the stack. It takes the argument 3 and recursively calls the factorial function, pushing frames and arguments onto the stack at each call. When the base case of 1 is reached, it begins returning and multiplying the results, popping frames off the stack. It finally returns the factorial of 3, which is 6.

Uploaded by

Ãyûsh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views72 pages

Nand2tetris Project

The code snippet shows the execution of a recursive factorial function on the stack. It takes the argument 3 and recursively calls the factorial function, pushing frames and arguments onto the stack at each call. When the base case of 1 is reached, it begins returning and multiplying the results, popping frames off the stack. It finally returns the factorial of 3, which is 6.

Uploaded by

Ãyûsh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 72

ELEMENTS OF COMPUTING 2

ASSIGNMENT-5

CB.EN.U4AIE22151 Sudheer Kumar


CB.EN.U4AIE22130 MC Dhanush
CB.EN.U4AIE22111 Ayush Kumar Rai
CB.EN.U4AIE22110 Anerud
1. LOCATE THE FOLLOWING VM PROGRAMS IN THE NAND2TERIS FOLDER
 SIMPLEADD
 STACKTEST
 BASICTEST
 POINTERTEST
 STATICTEST
A.) CHECK THE CORRECTNESS OF THE PROGRAMS USING THE VM EMULATOR AND
SUPPLIED TEST SCRIPT.

 SIMPLEADD

Loading simpleAdd from


stackArithmetic in project 07
LOAD TEXT
SCRIPT
SIMPLEADDVME
FROM THE
GIVEN OPTIONS
END OF SCRIPT-COMPARISION ENDED SUCCESSFULLY
 StackTest

Loading StackTest from Stack


arithmetic from projects 07
Load text script
StackTestVME
from the given options
End of script Comparision ended successfully
 BASICTEST
Loading BasicTest
from
MemoryAccess
from projects 07
Load text script
BasicTestVME
from the given options
End of script Comparision ended successfully

POINTERTEST

Loading
BasicTest from
MemoryAccess
from projects 07
Load text script
PointerTestVME
from the given
options
End of script Comparision ended successfully
 STATICTEST

Loading StaticTest from


MemoryAccess from projects 07
Load text script
StaticTestVME
from the given
options
End of script Comparision ended successfully
B.) COMPREHEND THE LINES OF CODES. (YOU MAY BE
ASKED TO EXPLAIN THE LINES OF CODES).
 BASICTEST
 POINTERTEST
 STATICTEST
 STACKTEST
 SIMPLEADD
VM TRANSLATOR
CODE
CODE SUMMARY
• The Parser class is responsible for parsing the input file and extracting commands. It has the
following methods:
• __init__(self, file_path): Initializes the Parser object by opening the input file for reading.
• has_more_commands(self): Checks if there are more commands to read from the input file.
• advance(self): Reads the next command from the input file, ignoring empty lines and comments.
• command_type(self): Returns the type of the current command. It can be one of the following:
'C_PUSH', 'C_POP', 'C_ARITHMETIC', 'C_LABEL', 'C_GOTO', 'C_IF', 'C_FUNCTION',
'C_RETURN', 'C_CALL', or 'C_UNKNOWN'.
• arg1(self): Returns the first argument of the current command. For arithmetic commands, it returns
the command itself. For other command types, it returns the segment name or label.
• arg2(self): Returns the second argument of the current command. It returns the index of the segment
or the number of arguments/locals.
CODE SUMMARY

• 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)

Explain with the help of a stack


diagram, argument how the
followingsnippet of code works.
GLOBAL
STACK

HERE GLOBAL
STACK IS
EMPTY
GLOBAL STACK

Value Passed to
Global Stack as
3
GLOBAL STACK

3
saved Main frame

Call the factorial


function with 1
argument

End the execution


of the main
function

3 Becomes argument 0
GLOBAL STACK

3
saved Main frame

Here it will jump to the


fractorial function ,Local
variable is 0
GLOBAL STACK

3
saved Main frame

push argument 0 which


is constant to value
3
push constant value 1
checking
equality
operation
if satisfies the
program will jump
to the BASECASE
GLOBAL
STACK
3
saved Main frame

As 3 not equal to 1 the result is flse ,


so the values pushed are over written with null
values. so operations are not shown in the global
stack
GLOBAL
STACK
3
saved Main frame

F(3):3
F(2):2

} Here we take argument 0 which is


3 and push constant 1 and
perform subtraction, then we get
2 over written.
GLOBAL STACK

3
saved Main frame

F(3):3
F(2):2
saved main frame

} Call the factorial function with 1


argument, but now we have argument
0 value is 2 , so we save the frames
and then return address.
GLOBAL STACK

3
saved Main frame

F(3):3
F(2):2
saved main frame

Here we push th value 2 and 1 then


perform equality check and if it is true
the program jumps to base case.
GLOBAL
STACK

3
saved Main frame

As it is false moves F(3):3


to next step to F(2):2
perform subtraction saved main
, here we get 1 in 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

Call the factorial function with 1


argument, but now we have argument
0 value is 1 , so we save the frames
and then return address.
GLOBAL
STACK

}
saved Main frame

F(3):
3
F(2):
2
saved main frame

F(2):2
F(2):1
saved main frame

Here we push the argument 0 value


that is 1 here and push the constant 1
value which is 1 and perform equality
check and if true it jumps to
BASECASE.
GLOBAL STACK

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

As the condition satisfied it jumps

} to BASECASE and pushes the


constant value 1.
program will get the return
address from the stack .
GLOBAL STACK

3
saved Main frame

F(3):3

F(2):2
saved main frame

F(2):2
F(1):1

Frames and return address are


removed . After returning it will execute
multi function
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

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

Explain in detail ‘THIS’, ‘THAT’ and


‘POINTERS..’
'THI
S'
Add a little bit of body text•"This" refers to the base address of
the current object instance. When "this" is used in an arithmetic
operation, it typically refers to the memory location of an instance
variable or a method of the current object.
•In stack-based arithmetic, "this" and "that" are typically used as
pointers to memory locations in the current object instance.
•For example, "push this 0" would push the value of the first
instance variable of the current object onto the stack.
The code push this 0 is a hypothetical instruction that is used in a stack-based virtual
machine to push the value of the first instance variable of the current object onto
the top of the stack.
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.
'THA
T'

"That" refers to a temporary variable that is used to store a


memory address in the current object instance. "That" is often
used as a pointer to an object's instance variables or other data
structures.
In stack-based arithmetic, "this" and "that" are typically used
as pointers to memory locations in the current object
instance.
For example, "pop that 0" would pop the topmost value from the
stack and store it in the first instance variable of the current
object.
The code push this 0 is a hypothetical instruction that might be used in a stack-based
virtual machine to push the value of the first instance variable of the current object
onto the top of the stack.

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

Explain the following code


function getIndex
// store array and target value in memory
argument 0 // array pointer -> here we are soring the base address of our array
argument 1 // target value -> this is the value ,we want to search in array
// initialize counter and result variables
push argument 0 -> we are pushing the address in global stack
call Array.length -> we are finding the length of array using array length function
Push constant 1 -> we are pushing constant 1 and subtracting from array length
sub
pop local 0 ->here we are storing it in local 0
// loop through array and search for target value
label loop Start -> here we are putting label
push argument 0 -> we are pushing the base address in global stack
push local 0 -> we are also pushing local zero value and adding
Add in order to get the relative address
pop
pointer 1 -> then we are the relative address in pointer in order to access
push that 0 the value in that address using that zero
push argument 1 -> then we are checking whether it is equal to the target value or not
Eq
if-goto found -> if it’s equal we are jumping to found label
// decrement remaining length and check if zero
push local 0 -> if the target value is not equal to the targeted value of array
push constant 1 then we are decrementing the local zero by 1
Sub
pop local 0
push local 0 ->if the local zero value is not less then zero then we are again
jumping
push constant 0 to the loopstart label
lt
not
if-goto loopStart
Push constant -1
Return -> if the targeted value is not founded in array then we are returning -1
// return the index of the first occurrence of the target value in the array
label found -> if we got the value then we are returning the local zero value
push local 0
return
Thank You

Ayush Kumar Rai Question 5 , PPT work

Anirud thiyagarajan Question 1(c,d and e)

Sudheer Kumar Question( 1(a,b) and 3)

MC Dhanush Question(2 and 4)

You might also like