Amir Saniyan
Amir Saniyan

Reputation: 13729

Is it possible that write a program with Java bytecode instructions directly?

In .NET platform it is possible to write a program with Common Intermediate Language directly and compile the sources with IL Assembler (ILASM).

For example below code is "Hello World" program.

.assembly Hello {}
.assembly extern mscorlib {}
.method static void Main()
{
    .entrypoint
    .maxstack 1
    ldstr "Hello, world!"
    call void [mscorlib]System.Console::WriteLine(string)
    ret
}

Is it possible that write a program with Java bytecode instructions directly like .NET?

Upvotes: 12

Views: 4108

Answers (2)

someone
someone

Reputation: 6572

You could use Jasmin

 .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

You compile it using:

java -jar jasmin.jar hello.j

You could refer this also

Upvotes: 7

Swapnil
Swapnil

Reputation: 8318

You can check out Jasmin. From Wikipedia,

Some projects provide Java assemblers to enable writing Java bytecode by hand. Assembly code may be also generated by machine, for example by compiler targeting Java virtual machine. Notable Java assemblers include:

Jasmin, takes textual descriptions for Java classes, written in a simple assembly-like syntax using Java Virtual Machine instruction set and generates a Java class file.

Jamaica, a macro assembly language for the Java virtual machine. Java syntax is used for class or interface definition. Method bodies are specified using bytecode instructions.

Note: I've not used any of these tools personally.

Upvotes: 7

Related Questions