0

I am trying to store a String in Jasmin Bytecode. After allot of research I could not find if this was possible and if it was, how it should be done. I could only find out how I could print it out(this is how I print a string). I also thought of storing a string as an array of chars, but thought there should be an easier way.

.class public HelloWorld
.super java/lang/Object

.method public static main([Ljava/lang/String;)V
  .limit stack 3
  .limit locals 1

  getstatic      java/lang/System/out Ljava/io/PrintStream;
  ldc            "Hello World."
  invokevirtual  java/io/PrintStream/println(Ljava/lang/String;)V

  return

.end method
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
shiteatlife
  • 57
  • 2
  • 12

2 Answers2

0

Found the answer: A string is not a primitive datatype so you save it/load it with:

aastore (number)
aaload (number)
shiteatlife
  • 57
  • 2
  • 12
0

Saw your answer, now I am not sure what the question was.

Anyway, this works for me:

.class public HelloWorld
.super java/lang/Object

.field static private message Ljava/lang/String;

.method public static main([Ljava/lang/String;)V
  .limit stack 1
  .limit locals 1

  ldc            "Hello World."
  putstatic      HelloWorld/message Ljava/lang/String;
  invokestatic   HelloWorld/print()V
  return
.end method

.method public static print()V
  .limit stack 2
  .limit locals 2
  getstatic      java/lang/System/out Ljava/io/PrintStream;
  getstatic      HelloWorld/message Ljava/lang/String;
  invokevirtual  java/io/PrintStream/println(Ljava/lang/String;)V
  return
.end method

Everything is static, which is not nice, but main sets message to "Hello World.", and invokes print. Then print gets message and prints it with the System.out.println call, stolen from your original code.

While I would love to be the magical being who reads those two pages of jasmin documentation and knows everything, but in the process actually I also digged out that JDK comes with a decompiler (JDK/bin/javap), and it produces 'disassembly' if you invoke it with -c switch. Somehow invokevirtual just did not want to work on the static method...

tevemadar
  • 12,389
  • 3
  • 21
  • 49