KSP Task 2From this task on you have an assembler at your disposal, so that the annoying coding of the commands no longer has to be carried out by hand. Write the three small test programs from the previous task into a file that should have the extension .asm. Have the programs assembled; the resulting files should have the extension .bin. Inspect these binary files with the command "hexdump -C". Try to explain exactly how the observed output comes about! Study the description of the ninja binaer format .
Now change your VM from the previous task sheet so that the binary program is loaded from a file into the program memory.
a) The name of the program to be loaded should be passed as a command line argument.
b) Before you can do anything with the contents of the file, you must "open" the file:FILE *fopen(const char *path, const char *mode);
c) What you need to do with the contents of a ninja binaer file to run the program it contains is also described in the ninja binaer format description.
d) You will need the fread() function to read from a file:size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
e) You will also need the malloc() function to request memory:void *malloc(size_t size);
f) If you have used all information from the file, the file will be closed again:int fclose(FILE *fp);
Add version 2 to your VM by implementing the new instructions. A discussion of global variables and their associated instructions can be found here , the discussion of local variables and stack frames with their instructions can be found here .
Now check the functioning of your VM by assembling and executing the three small programs from task part 1 as well as the two test programs prog1.asm and prog2.asm . It is strongly recommended to program and execute at least five more self-selected calculations in the stack machine assembler. You should be able to record the stack at any time during execution!
5. and here, as always, the reference implementation:njvm (see attached file)