KSP Task 4 (VM4: call/return, arguments, return value, Boolean expressions)Now it's getting serious... ;-)With the solution of this task, you have the complete ninja at your disposal (except the array and record objects). You can use it to execute the following programs, for example:a) ggT (see attachment file)b) n! recursive (see attachment file)c) n! iterative (see annex file)But take one after the other...
New VM Instructions1. here is a discussion of the subroutine call instructions and here is a list of all instructions of the VM of this release.
Implement subroutine jumps and jumps with the instructions "call" and "ret". Here is a test program with no arguments and no return value. You should be able to follow the execution with the help of your debugger in a single step procedure.
Implement the "drop" instruction and test access to the arguments of a procedure with this test program.
Add the return register to the machine architecture and implement the "pushr" and "popr" instructions. You can check the return of a value with this test program.
5. then finally this test program should work without further work.
6. implement the instruction "dup", which duplicates the top stack entry: ... n --> ... n n
7. the assembler belonging to this task is of course also available:nyes
8. last but not least here again the reference implementation:njvm (see attachment file).
The Ninja Compiler (Answer questions also)Now there's a first version of the Ninja compiler (see attachment file) - still without arrays, records and other bells and whistles, but with procedures and functions, control structures, assignments and expressions. The grammar (see file) together with the tokens (see document) and the predefined identifiers (see document)) describes what a correct ninja program (in our somewhat reduced language) looks like.
Translate the three test programs mentioned above (see file) using the Ninja compiler. The output is ninja assembler and can be viewed with a text editor. Try to assign each assembler statement generated by the compiler to a ninja source statement. If you have any questions, please discuss them with the tutors during the internship!
Assemble the three test programs and run them through your VM. Follow the execution with the debugger of your VM.
Write at least five more test programs, either in ninja or assembler, to make sure the VM is working correctly. Also sound out constructions that were not part of the tests until now. An example: Can a function call another function in its return statement? Can this also be nested?
Which sequence of instructions does the compiler generate for the logical "and" and the logical "or"? Explain exactly how the short circuit evaluation is implemented!