Tutorial Week 12
Task 1. Write a MIPS assembler program that dynamically generates some MIPS machine code, writing it to some free space in the program’s text segment. The generated code should increment the content of register $a0, and then returns to the address stored in $ra. After your program has generated this code, it should do three things:
1. Print the value of $a0, then
2. Jump-and-link (command jal) to the generated code, and then finally
3. Print the value of $a0 again.
When you run your code, you should see that jumping to the generated code causes $a1 to be incremented.
Note also that to run code that solves this task in MARS, you have to enable the self-modifying code under the settings menu. This is because, by default, MARS will not allow execution of code in the data segment of memory.
Note that you can find the numbers corresponding to each assembly command by first writing the command as part of a program, and then viewing the assembled bytecode in the MARS execute tab. They can also be found in the text MIPS Architecture For Programmers Volume II-A: The MIPS32 Instruction Set that is linked to from the introduciton to MIPS at Study Direct.
Task 2. Consider the Java program below. Draw the layout of the data in the heap at the point denoted here. Assume that there was no garbage collection, i.e. no heap memory is reused. There is no need to be super precise about how exactly the JVM lays out objects on the heap, just use a simple scheme like we did in the lectures on the compilation of OO languages. What matters are the pointers and the offset of fields from the top of contiguous memory regions.
class A {
public static int [] sa = new int [ 3 ];
public int [] a;
public A ( int n ) { a = new int [ n ]; }
public void f () {
for ( int x : a ) { System.out.println ( x ); } } }
class B {
A a;
int [] aa;
B ( int n ) {
a = new A ( n );
aa = a.a; } }
class C {
public C c;
public A a;
public C ( A _a ) {
c = this;
a = _a; }
void f ( C _c ) {
this.c = _c; }
void g ( C _c ) {
c.c = _c; } }
class D extends C {
int x = 0;
public D () { super ( new A ( 2 ) ); }
void f ( C _c ) {
c.c = _c; } }
class Main {
public static void main ( String [] args ) {
A a = new A ( 2 );
B b = new B ( 3 );
C c = new C ( a );
C c2 = new C ( new A ( 4 ) );
c.f ( c2 );
c.g ( c );
c = new D ();
D d = new D ();
c.f ( d );
d.g ( c );
// here} }