Search code examples
compiler-constructionjvmjasmin

Setting array references equal jasmin assembler


I am having some problems figuring out how to assign to arrays equal to each other in jasmine. I wish to compile code like the following

array a[1];
array b[1];

a = b;

Now I want an actual copy of b not assigning them to the same thing. Suppose a is in register 0 and b is in register 1

then the following is what I thought would work

aload   1
astore  0

but in fact now if I change b then a will also change. How would I do this in jasmin so that the arrays are distinct e.g.

array a[1];
array b[1];
a[1] = 'a';
b[1] = 'b';
a = b;
b[1] = 'a';
print a; //should print 'b' but prints 'a' currently
print b; //should print 'a' and does currently

Note the code that I have written is correct java, however, this is for a different language.


Solution

  • It's not about Jasmin or bytecode at all.

    If you want to copy the contents of the array, call Object.clone with invokevirtual instruction or Arrays.copyOf / System.arraycopy with invokestatic.