∟AsmTools/JCOD Example - HelloWorld.jcod

This section provides a tutorial example on how to write a simple program in AsmTools/JCOD: HelloWorld.jcod.

Now let's write a simple program in AsmTools/JCOD: HelloWorld.jcod.

class HelloWorld {
  0xCAFEBABE;   // bytecode file magic number
  0;            // minor version
  55;           // major version (JVM 11)

  [] {          // Constant Pool
    ;                    // #0: required to be empty
    Field #2 #3;         // #1: java/lang/System.out:Ljava/io/PrintStream;
    class #4;            // #2: java/lang/System
    NameAndType #5 #6;   // #3: out:Ljava/io/PrintStream;
    Utf8 "java/lang/System";
    Utf8 "out";          // #5
    Utf8 "Ljava/io/PrintStream;";
    String #8;           // #7: "Hello, World from JCOD!"
    Utf8 "Hello, World from JCOD!";
    Method #10 #11;      // #9: java/io/PrintStream.println:(Ljava/lang/String;)V
    class #12;           // #10: java/io/PrintStream
    NameAndType #13 #14; // #11: println:(Ljava/lang/String;)V
    Utf8 "java/io/PrintStream";
    Utf8 "println";      // #13
    Utf8 "(Ljava/lang/String;)V";
    class #16;           // #15: HelloWorld
    Utf8 "HelloWorld";   // #16
    Utf8 "Code";         // #17
    Utf8 "main";         // #18
    Utf8 "([Ljava/lang/String;)V";
    class #21;           // #20: java/lang/Object
    Utf8 "java/lang/Object";
  }

  0x0021;   // access flags (ACC_PUBLIC | ACC_SUPER)
  #15;      // this_class (HelloWorld)
  #20;      // super_class (java/lang/Object)

  [] {}     // no interfaces to be implemented
  [] {}     // do fields to be declared

  [] {          // method list
    {           // method 0
      0x0009;   // access flags: ACC_PUBLIC | ACC_STATIC
      #18;      // name: "main"
      #19;      // descriptor: "([Ljava/lang/String;)V"
      [] {      // attribute list
        Attr(#17) {     // the "Code" attribute
          2;            // max_stack
          1;            // max_locals
          Bytes[] {     // bytecode
            0xB2; 0x0001;   // getstatic #1 (System.out)
            0x12; 0x07;     // ldc #7 ("Hello, World from JCOD!")
            0xB6; 0x0009;   // invokevirtual #9 (PrintStream.println)
            0xB1;       // return
          }
          [] {}         // no exceptions
          [] {}         // no code attributes
        }
      }
    }
  }

  [] {}                 // no class attributes
}

Note that the "ldc #7" (0x12; 0x07;) instruction takes only 1 byte as the constant pool index. It can be replaced with the 2-byte version "ldc_w #7" (0x13; 0x0007;).

Then compile it by invoking AsmTools/JASM "jcoder" sub-command. No errors are reported.

herong> java -jar asmtools-core-7.0.b10-ea.jar jcoder HelloWorld.jcod

Run it on JVM. It works!

herong> java HelloWorld

Hello, World from JCOD!

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 JVM Runtime Data Areas

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 OpenJ9 by Eclipse Foundation

 JRockit JVM 28.2.7 by Oracle Corporation

 JVM Bytecode and Instruction Set

►JVM Assembler/Disassembler Tools

 What Is JVM Assembler

 What Is Jasmin

 Compare HelloWorld.j with "javap" Output

 Jasmin Assembly Statements

 Jasmin Example - Empty.j

 Jasmin Example - Calculator.j

 What Is JASM

 JASM Assembly Statements

 JASM Example - Calculator.jasm

 JASM Disassembler: "jasm -d"

 What Is AsmTools

 AsmTools/JASM Assembly Statements

 AsmTools/JASM Example - Calculator.jasm

 AsmTools/JASM Disassembler: "jdis"

 AsmTools/JCOD Assembly File Format

►AsmTools/JCOD Example - HelloWorld.jcod

 AsmTools/JCOD Disassembler: "jdec"

 What Is Lilac

 Lilac Assembly Statements

 Lilac Macro Example - HelloWorldMacro.jasm

 Lilac Disassembler: "jdasm"

 Archived Tutorials

 References

 Index

 Full Version in PDF/EPUB