1. Homepage
  2. Programming
  3. Computer Programming Project Task 5: Call yourself a method!

Computer Programming Project Task 5: Call yourself a method!

Engage in a Conversation
NetherlandsVUVrije Universiteit AmsterdamComputer ProgrammingC dreaded BinariesCall yourself a method

5 - Call yourself a method!

What to implement

Operations

  • OP_INVOKEVIRTUAL
  • OP_IRETURN

In order to pass test5, correctly implement all of the instructions listed above. CourseNana.COM

Note: ideally, if you pass all of the basic tests, you should also pass most, if not all, of the advanced tests. CourseNana.COM


Please make sure you (re)read the chapter on the IJVM from the book Structured Computer Organization for a more in-depth explanation of IJVM method invocation. CourseNana.COM


Introduction

In this chapter, you will be tasked with implementing methods. Method invocation in the IJVM can be a bit tricky, as it has some strange behaviour that is left over from the JVM (Java Virtual Machine). For example, when invoking a method, the caller has to push an OBJREF (object reference) onto the stack as the first parameter. Since the IJVM does not support different objects, pushing this reference has no meaningful functionality. Nevertheless, it's still there just to keep (some kind of) compatibility with Java. CourseNana.COM

To invoke a method, the caller first pushes the method arguments onto the stack and executes the INVOKEVIRTUAL instruction. This instruction itself has one argument, which is the index of an element in the constant pool. The value of this constant is the address of the start of a section of the program text called the method area, which stores the number of argument and the number of local variables of the method and its method text. CourseNana.COM

The method area first contains two shorts, the first one signifying the number of arguments the method expects (including OBJREF), and the second one being the number of local variables. The fifth byte in the method area is the actual first instruction to be executed, so adjust your program counter accordingly! CourseNana.COM

figure 5.1 CourseNana.COM

The figure above is an example of executing INVOKEVIRTUAL 0x2. The pointer to the method is looked up at constant index 0x2 (with value 0x500). The method area is found at offset 0x500 in the program text. Then the number of arguments and local variables is read. After this the frame is set up, and the first instruction of the method starts executing. CourseNana.COM

You might wonder why the IJVM employs this roundabout behaviour, where we get a pointer from an index in the constant pool, instead of directly getting the pointer. This behaviour is left over from the JVM, where it makes dynamic linking possible. On the JVM, if a method is not from the present program but from an external library, then the constant pool initially contains a symbolic reference of the external method, i.e. the name of the library and the method, instead of the actual adress of the method (because there is no actual adress yet, the library still has to be loaded). When such a symbolic reference is encountered the library is loaded into memory, and the symbolic reference in the constant pool is changed to a pointer to the method in memory. This indirection makes this possible. CourseNana.COM

Setting up a local frame

When a method is invoked, a new local frame is created. Have a look at the figure below to see what the memory should look like after a method invocation: CourseNana.COM

figure 5.2 CourseNana.COM

A complete example of how all of this works, is demonstrated in this interactive webpage by TA Zain Munir. CourseNana.COM

The memory layout of your implementation does not necessarily need to match that of the figure or example above, it is also fine to employ an alternative layout if you find this easier. CourseNana.COM

Returning from a method

At the end of a method, the IRETURN instruction is called. When this happens, the program counter, the stack pointer and the frame pointer (lv) are restored to their previous values. Then, the top of the stack of the current frame is returned by overwriting the link pointer with the return value, and then pointing the stack pointer to this location. Another way of looking at this is that the pushed arguments and the link pointer are removed from the stack of the previous frame, and then a return value is placed at the top of the stack: CourseNana.COM

figure 5.3 CourseNana.COM

In this process, the link pointer is used to find the prev_lv and prev_pc. In the figure, the prev_pc and prev_lv are located directly below the word pointed at by sp, but this is not always true. The stack (above the previous lv) may contain surplus elements next to the return value. If this happens, this is not an error and the topmost element is the return value. Hence, in the diagram above there may be an arbitrary number of elements between the previous lv and the return value. Because of this, the link pointer is needed to find the prev_lv and prev_pc as they are not always directly below the return value. CourseNana.COM

The suggested approach

Before you start implementing any instructions, make sure you keep track of the current frame using a frame pointer lv and are accessing the local variables as an offset from that. CourseNana.COM

Once you've made sure you keep track of the current frame, start implementing the INVOKEVIRTUAL and IRETURN instructions. CourseNana.COM

OpCodeInstructionArgsDescription
0xB6INVOKEVIRTUALshortInvoke a method
0xACIRETURNN/AReturn from the current method

Done? Did you pass all basic tests and at least 4 of the advanced tests? Hurray! Your program is sufficient to pass the course. CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Netherlands代写,VU代写,Vrije Universiteit Amsterdam代写,Computer Programming代写,C代写, dreaded Binaries代写,Call yourself a method代写,Netherlands代编,VU代编,Vrije Universiteit Amsterdam代编,Computer Programming代编,C代编, dreaded Binaries代编,Call yourself a method代编,Netherlands代考,VU代考,Vrije Universiteit Amsterdam代考,Computer Programming代考,C代考, dreaded Binaries代考,Call yourself a method代考,Netherlandshelp,VUhelp,Vrije Universiteit Amsterdamhelp,Computer Programminghelp,Chelp, dreaded Binarieshelp,Call yourself a methodhelp,Netherlands作业代写,VU作业代写,Vrije Universiteit Amsterdam作业代写,Computer Programming作业代写,C作业代写, dreaded Binaries作业代写,Call yourself a method作业代写,Netherlands编程代写,VU编程代写,Vrije Universiteit Amsterdam编程代写,Computer Programming编程代写,C编程代写, dreaded Binaries编程代写,Call yourself a method编程代写,Netherlandsprogramming help,VUprogramming help,Vrije Universiteit Amsterdamprogramming help,Computer Programmingprogramming help,Cprogramming help, dreaded Binariesprogramming help,Call yourself a methodprogramming help,Netherlandsassignment help,VUassignment help,Vrije Universiteit Amsterdamassignment help,Computer Programmingassignment help,Cassignment help, dreaded Binariesassignment help,Call yourself a methodassignment help,Netherlandssolution,VUsolution,Vrije Universiteit Amsterdamsolution,Computer Programmingsolution,Csolution, dreaded Binariessolution,Call yourself a methodsolution,