Skip to content

Instantly share code, notes, and snippets.

@AppLoidx
Last active October 26, 2019 11:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AppLoidx/7173d7277dd73025ae06377a7cc75ed0 to your computer and use it in GitHub Desktop.
Save AppLoidx/7173d7277dd73025ae06377a7cc75ed0 to your computer and use it in GitHub Desktop.

Hello World program in bytecode

cafe babe 
0000 0034 

001d 


0a 0006 000f

09 0010 0011 

08 0012 

0a 0013 0014

07 0015

07 0016

01 0006 
3c 69 6e 69 74 3e 

01 0003 
28 29 56 

01 0004 
43 6f 64 65 

01 000f 
4c 69 6e 65 4e 75 6d
62 65 72 54 61 62 6c 65

01 0004 
6d 61 69 6e 

01 0016 
28 5b 4c 6a 61 76 61 2f 6c 61 6e 67
2f 53 74 72 69 6e 67 3b 29 56 

01 000a 
53 6f 75 72 63 65 46 69 6c 65

01 0009 
4d 61 69 6e 2e 6a 61 76 61 

0c 0007 0008 

07 0017 

0c 0018 0019

01 000b 
48 65 6c 6c 6f 20 57 6f 72 6c 64 

07 001a

0c 001b 001c 

01 0004 
4d 61 69 6e 

01 0010
6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74

01 0010 
6a 61 76 61 2f 6c 61 6e 67 2f 53 79 73 74 65 6d

01 0003 
6f 75 74 

01 0015 
4c 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 
72 65 61 6d 3b

01 0013 
6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 
65 61 6d 

01 0007 
70 72 69 6e 74 6c 6e

01 0015 
28 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 
6e 67 3b 29 56 




00 21

00 05 

00 06

0000

0000 


0002

0001 
0007
0008 
0001

0009
0000 001d
00 01 00 01 00 00 00 05 2a b7 00 01 b1 00 00 00 
01 00 0a 00 00 00 06 00 01 00 00 00 01

0089 
000b 
000c 
0001 

0009 
0000 0025 
00 02 00 01 00 00 00 09 b2 00 02 12 03 b6 00 04 
b1 00 00 00 01 00 0a 00 00 00 0a 00 02 00 00 00 
04 00 08 00 05 

0001
000d 
0000 0002
00 0e

Output: Hello Wordl

Assemble:

Classfile /C:/Users/Àðòóð/playground/java/jvm/Main.class
  Last modified 26.10.2019; size 413 bytes
  MD5 checksum 6449121a3bb611fee394e4f322401ee1
  Compiled from "Main.java"
public class Main
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #6.#15         // java/lang/Object."<init>":()V
   #2 = Fieldref           #16.#17        // java/lang/System.out:Ljava/io/PrintStream;
   #3 = String             #18            // Hello World
   #4 = Methodref          #19.#20        // java/io/PrintStream.println:(Ljava/lang/String;)V
   #5 = Class              #21            // Main
   #6 = Class              #22            // java/lang/Object
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               main
  #12 = Utf8               ([Ljava/lang/String;)V
  #13 = Utf8               SourceFile
  #14 = Utf8               Main.java
  #15 = NameAndType        #7:#8          // "<init>":()V
  #16 = Class              #23            // java/lang/System
  #17 = NameAndType        #24:#25        // out:Ljava/io/PrintStream;
  #18 = Utf8               Hello World
  #19 = Class              #26            // java/io/PrintStream
  #20 = NameAndType        #27:#28        // println:(Ljava/lang/String;)V
  #21 = Utf8               Main
  #22 = Utf8               java/lang/Object
  #23 = Utf8               java/lang/System
  #24 = Utf8               out
  #25 = Utf8               Ljava/io/PrintStream;
  #26 = Utf8               java/io/PrintStream
  #27 = Utf8               println
  #28 = Utf8               (Ljava/lang/String;)V
{
  public Main();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 1: 0

  public static void main(java.lang.String...);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC, ACC_VARARGS
    Code:
      stack=2, locals=1, args_size=1
         0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: ldc           #3                  // String Hello World
         5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
         8: return
      LineNumberTable:
        line 4: 0
        line 5: 8
}
SourceFile: "Main.java"

Explained bytecode:

cafe babe -- magic 

0000 0034 -- minor and major version

001d -- constant pool count

-- [Constant Pool]

-- methodref 
0a 0006 000f

-- fieldref
09 0010 0011 

-- string
08 0012 

-- methodref
0a 0013 0014

-- Class
07 0015

-- Class
07 0016

-- Utf8
01 0006 
3c 69 6e 69 74 3e 

-- Utf8
01 0003 
28 29 56 

-- Utf8
01 0004 
43 6f 64 65 

-- Utf8
01 000f 
4c 69 6e 65 4e 75 6d
62 65 72 54 61 62 6c 65

-- Utf8
01 0004 
6d 61 69 6e 

-- Utf8
01 0016 
28 5b 4c 6a 61 76 61 2f 6c 61 6e 67
2f 53 74 72 69 6e 67 3b 29 56 

-- Utf8
01 000a 
53 6f 75 72 63 65 46 69 6c 65

-- Utf8
01 0009 
4d 61 69 6e 2e 6a 61 76 61 

-- NameAndType
0c 0007 0008 

-- Class
07 0017 

-- NameAndType
0c 0018 0019

-- Utf8
01 000b 
48 65 6c 6c 6f 20 57 6f 72 6c 64 

-- Class
07 001a

-- NameAndType
0c 001b 001c 

-- Utf8
01 0004 
4d 61 69 6e 

-- Utf8
01 0010
6a 61 76 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74

-- Utf8
01 0010 
6a 61 76 61 2f 6c 61 6e 67 2f 53 79 73 74 65 6d

-- Utf8
01 0003 
6f 75 74 

-- Utf8
01 0015 
4c 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 
72 65 61 6d 3b

-- Utf8
01 0013 
6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 
65 61 6d 

-- Utf8
01 0007 
70 72 69 6e 74 6c 6e

-- Utf8
01 0015 
28 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 
6e 67 3b 29 56 

-- [Constant Pool END]

00 21 -- access_flags

00 05 -- this_class

00 06 -- super_class


0000 -- interfaces_count

-- empty interface[]

0000 -- fields_count

-- empty field[]

0002 -- methods_count

-- [methods]

-- public Main();

0001 --access_flags
0007 -- name_index
0008 -- descriptor_index
0001 -- attributes_count

-- attribute_info
0009 -- attribute_name_index (Code)
0000 001d - attribute_length
00 01 -- max_stack
00 01 -- max_locals
00 00 00 05 -- code_length 
2a b7 00 01 b1 -- code[]
0000 -- exception_table_length
0001 --  attributes_count;
00 0a -- attribute_name_index
00 00 00 06 -- attribute_length
00 01 00 00 00 01

-- public static void main(java.lang.String...);

0089  --access_flags
000b  -- name_index
000c  -- descriptor_index
0001  -- attributes_count

-- attribute_info
0009  -- attribute_name_index (Code)
0000 0025 -- attribute_length
0002 -- max_stack
0001 -- max_locals 
0000 0009 -- code_length 
b2 00 02 12 03 b6 00 04 b1 -- code[]

0000 -- exception_table_length

00 01 -- attributes_count

00 0a -- attribute_name_index
00 00 00 0a -- attribute_length
00 02 00 00 00 04 00 08 00 05 

-- [methods END]


0001 -- attributes_count (ClassFile's)

000d -- attribute_name_index
0000 0002 -- attribute_length
00 0e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment