Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Java JVM bytecode instruction set?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how the Java JVM bytecode instruction set is, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

How does Java cross platforms?

When we were in computer class, the teacher said: "the computer can only recognize 0 and 1, so the program we write can only be executed by the computer after being translated into a binary format composed of 0 and 1 by the compiler." The bytecode generated by our compilation is binary bytecode, and the bytecode cannot be run directly by the machine. Through JVM, the compiled bytecode is converted into machine code instructions (binary 01 code) that can be directly identified by the corresponding operating system platform. JVM acts as a bridge for intermediate conversion, so that the Java file we write can be compiled and run everywhere.

Platform-independent cornerstone

Bytecode (ByteCode), a unified program storage format used by virtual machines on different platforms and all platform beans, is the cornerstone of platform independence. The Java virtual machine is not bound to any language, including Java, but is only associated with a specific binary file format, "Class file", which contains the Java virtual machine instruction set and symbol table, as well as some other auxiliary information. Based on the consideration of security, the Java virtual machine specification requires the use of many mandatory syntax and structural constraints in Class files. Other languages can use their own compilers to compile valid Class files that can be accepted by Java virtual machines. In fact, many languages can now run on the Java virtual machine, such as kotlin, Scala, JRuby, Groovy, Jython.

Introduction to JVM bytecode instruction

JVM official document JVM virtual machine specification for JDK8 instruction set introduction address: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5, good English friends can have a look.

The instruction of a Java virtual machine consists of a byte-long number that represents the meaning of a particular operation (called opcode, Opcode), followed by zero or more parameters that represent the required parameters for this operation (called operands, Operands). Because the Java virtual machine uses an Operand stack-oriented architecture rather than a register-oriented architecture, most instructions do not contain operands, only one opcode.

Due to the limitation that the length of the Java virtual machine opcode is one byte (8 bits per byte)

Because the class file format abandons the length alignment of the operands of the compiled code, the number and length of operands depend on the opcode, if the length of an Operand exceeds one byte, it will be stored in big-endian order, that is, the byte order in front of the highest order. For example, if you want to put an unsigned 16-bit length

Integers are stored in two unsigned bytes (named Miao byte1 and byte2), and the value of this 16-bit unsigned integer is: (byte1 TestLock.txt disassembles the class file to get the following instruction code

Compiled from "TestLock.java" public class com.wkp.clone.TestLock {public com.wkp.clone.TestLock (); Code: 0: aload_0 1: invokespecial # 8 / / Method java/lang/Object. "": () V 4: return public void onlyMe (java.lang.Object) Code: 0: aload_1 / / push object f to the top of the Operand stack 1: dup / / copy the top element of the stack (reference to object f) 2: astore_2 / / copy the top element of the stack to the local variable table Slot 2 (third variable) 3: monitorenter / / takes the top stack element object f as the lock Start synchronization 4: aload_0 / / Stack elements of local variable Slot 0 (this pointer) 5: invokespecial # 16 / / call doSomething () method 8: aload_2 / / stack local variable Slot 2 element (f) 9: monitorexit / / release the lock and exit synchronization 10: goto 16 / / the method returns normally Jump to 16 13: aload_2 / / put the local variable table Slot 2 element (f) into the stack 14: monitorexit / / exit synchronization 15: athrow / / throw the exception object at the top of the stack to the caller of onlyMe 16: return / / method returns Exception table: from to target type 4 10 13 any 13 15 13 any}

The compiler must ensure that no matter how the method is completed, every momtor instruction called in the method must execute its corresponding momtorexlt instruction, regardless of whether the method ends normally or ends abnormally.

As you can see from the above instruction code, in order to ensure that the monitorenter and monitorexit instructions can still be paired correctly when the method exception completes, the compiler automatically generates an exception handler that declares that it can handle all exceptions, and its purpose is to execute momtorexit instructions.

A complete set of JVM instructions

The following three opcodes are reserved for internal use in the virtual machine.

Breakpoint marking during 0xca breakpoint debugging

Language backdoor reserved for specific software by 0xfe impdep1

Language backdoor reserved for specific hardware by 0xff impdep2

Here are all JVM instruction sets, which may vary individually. See Chapter 7 opcode mnemonics of the Java Virtual Machine Specification (JavaSE8 version).

Script mnemonic description

0x00 nop has no operation

0x01 aconst_null pushes null to the top of the stack

0x02 iconst_m1 pushes int-1 to the top of the stack

0x03 iconst_0 pushes int 0 to the top of the stack

0x04 iconst_1 pushes int 1 to the top of the stack

0x05 iconst_2 pushes int 2 to the top of the stack

0x06 iconst_3 pushes int 3 to the top of the stack

0x07 iconst_4 pushes int 4 to the top of the stack

0x08 iconst_5 pushes int 5 to the top of the stack

0x09 lconst_0 pushes long 0 to the top of the stack

0x0a lconst_1 pushes long 1 to the top of the stack

0x0b fconst_0 pushes float type 0 to the top of the stack

0x0c fconst_1 pushes float type 1 to the top of the stack

0x0d fconst_2 pushes float 2 to the top of the stack

0x0e dconst_0 pushes flexible 0 to the top of the stack

0x0f dconst_1 pushes malle1 to the top of the stack

0x10 bipush pushes the constant value of a single byte (- 128 to 127) to the top of the stack

0x11 sipush pushes a short integer constant value (- 32768mm 32767) to the top of the stack

0x12 ldc pushes int, float or string constant values from the constant pool to the top of the stack

0x13 ldc_w pushes int, float, or string constant values from the constant pool to the top of the stack (wide index)

0x14 ldc2_w pushes long or double constant values from the constant pool to the top of the stack (wide index)

0x15 iload pushes the specified int local variable to the top of the stack

0x16 lload pushes the specified long local variable to the top of the stack

0x17 fload pushes the specified float local variable to the top of the stack

0x18 dload pushes the specified string-type local variable to the top of the stack

0x19 aload pushes the local variable of the specified reference type to the top of the stack

0x1a iload_0 pushes the first int local variable to the top of the stack

0x1b iload_1 pushes the second int local variable to the top of the stack

0x1c iload_2 pushes the third int local variable to the top of the stack

0x1d iload_3 pushes the fourth int local variable to the top of the stack

0x1e lload_0 pushes the first long local variable to the top of the stack

0x1f lload_1 pushes the second long local variable to the top of the stack

0x20 lload_2 pushes the third long local variable to the top of the stack

0x21 lload_3 pushes the fourth long local variable to the top of the stack

0x22 fload_0 pushes the first float local variable to the top of the stack

0x23 fload_1 pushes the second float local variable to the top of the stack

0x24 fload_2 pushes the third float local variable to the top of the stack

0x25 fload_3 pushes the fourth float local variable to the top of the stack

0x26 dload_0 pushes the first invalid local variable to the top of the stack

0x27 dload_1 pushes the second declare local variable to the top of the stack

0x28 dload_2 pushes the third declare local variable to the top of the stack

0x29 dload_3 pushes the fourth declare local variable to the top of the stack

0x2a aload_0 pushes the first reference type local variable to the top of the stack

0x2b aload_1 pushes the second reference type local variable to the top of the stack

0x2c aload_2 pushes the third reference type local variable to the top of the stack

0x2d aload_3 pushes the fourth reference type local variable to the top of the stack

0x2e iaload pushes the value of the specified index of the int array to the top of the stack

0x2f laload pushes the value of the specified index of the long array to the top of the stack

0x30 faload pushes the value of the specified index of the float array to the top of the stack

0x31 daload pushes the value of the specified index of the array of types to the top of the stack

0x32 aaload pushes the value of the specified index of the referenced array to the top of the stack

0x33 baload pushes the value of the specified index of the boolean or byte array to the top of the stack

0x34 caload pushes the value of the specified index of the char array to the top of the stack

0x35 saload pushes the value of the specified index of the variant array to the top of the stack

0x36 istore stores the int value at the top of the stack in the specified local variable

0x37 lstore stores the long value at the top of the stack in the specified local variable

0x38 fstore stores the float value at the top of the stack in the specified local variable

0x39 dstore stores the value at the top of the stack in the specified local variable

0x3a astore stores the referenced value at the top of the stack into the specified local variable

0x3b istore_0 stores the int value at the top of the stack in the first local variable

0x3c istore_1 stores the int value at the top of the stack in the second local variable

0x3d istore_2 stores the int value at the top of the stack in the third local variable

0x3e istore_3 stores the int value at the top of the stack in the fourth local variable

0x3f lstore_0 stores the long value at the top of the stack in the first local variable

0x40 lstore_1 stores the long value at the top of the stack in the second local variable

0x41 lstore_2 stores the long value at the top of the stack in the third local variable

0x42 lstore_3 stores the long value at the top of the stack in the fourth local variable

0x43 fstore_0 stores the float value at the top of the stack in the first local variable

0x44 fstore_1 stores the float value at the top of the stack in the second local variable

0x45 fstore_2 stores the float value at the top of the stack in the third local variable

0x46 fstore_3 stores the float value at the top of the stack in the fourth local variable

0x47 dstore_0 stores the top stack value in the first local variable

0x48 dstore_1 stores the value of the top stack in the second local variable.

0x49 dstore_2 stores the top stack value in the third local variable

0x4a dstore_3 stores the top stack value in the fourth local variable

0x4b astore_0 stores the top-of-stack referenced value in the first local variable

0x4c astore_1 stores the referenced value at the top of the stack in the second local variable

0x4d astore_2 stores the top-of-stack referenced value in the third local variable

0x4e astore_3 stores the top-of-stack referenced value in the fourth local variable

0x4f iastore stores the int value at the top of the stack at the specified index of the specified array.

0x50 lastore stores the long value at the top of the stack in the specified index of the specified array.

0x51 fastore stores the float value at the top of the stack at the specified index of the specified array.

0x52 dastore stores the value at the top of the stack in the specified index of the specified array.

0x53 aastore stores the referenced values at the top of the stack at the specified index of the specified array.

0x54 bastore stores boolean or byte values at the top of the stack at the specified index of the specified array.

0x55 castore stores the char value at the top of the stack in the specified index of the specified array.

0x56 sastore stores the value at the top of the stack in the specified index of the specified array.

0x57 pop pops up the value at the top of the stack (values cannot be of type long or double)

0x58 pop2 pops up one (long or double type) or two values at the top of the stack (other)

0x59 dup copies the top value of the stack and presses the copy value into the top of the stack

0x5a dup_x1 copies the top values of the stack and presses two replication values into the top of the stack

0x5b dup_x2 copies the top values of the stack and presses three (or two) copy values into the top of the stack

0x5c dup2 copies one (long or double type) or two (other) values at the top of the stack and presses the copied values into the top of the stack

0x5d dup2_x1 copies one or two values at the top of the stack and inserts them below the two or three values at the top of the stack

0x5e dup2_x2 copies one or two values at the top of the stack and inserts them under the two, three, or four values at the top of the stack

0x5f swap swaps the two values at the top of the stack (values cannot be of type long or double)

0x60 iadd adds two int values at the top of the stack and presses the results to the top of the stack

0x61 ladd adds the values of the two lengths at the top of the stack and presses the results to the top of the stack

0x62 fadd adds the float values at the top of the stack and presses the results to the top of the stack

0x63 dadd adds the values at the top of the stack and presses the results to the top of the stack.

0x64 isub subtracts two int values from the top of the stack and presses the results into the top of the stack.

0x65 lsub subtracts the two lengths at the top of the stack and presses the results to the top of the stack.

0x66 fsub subtracts the float values at the top of the stack and presses the results into the top of the stack.

0x67 dsub subtracts the values at the top of the stack and presses the results to the top of the stack.

0x68 imul multiplies two int values at the top of the stack and presses the result onto the top of the stack

0x69 lmul multiplies the values of the two lengths at the top of the stack and presses the results on the top of the stack

0x6a fmul multiplies the float values at the top of the stack and presses the results on the top of the stack

0x6b dmul multiplies the two double values at the top of the stack and presses the results on the top of the stack

0x6c idiv divides the two int values at the top of the stack and presses the result into the top of the stack.

0x6d ldiv divides the two lengths at the top of the stack and presses the results on the top of the stack.

0x6e fdiv divides the float values at the top of the stack and presses the results on the top of the stack.

0x6f ddiv divides the two double values at the top of the stack and presses the results on the top of the stack.

0x70 irem takes two int values at the top of the stack for modular operation and presses the results into the top of the stack.

0x71 lrem takes the two lengths at the top of the stack for modular operation and presses the results on the top of the stack.

0x72 frem takes the float values at the top of the stack for modular operation and presses the results into the top of the stack.

0x73 drem takes the numerical values at the top of the stack for modular operation and presses the results to the top of the stack.

0x74 ineg negates the int value at the top of the stack and presses the result into the top of the stack.

0x75 lneg negates the value of the long at the top of the stack and presses the result to the top of the stack.

0x76 fneg negates the float value at the top of the stack and presses the result into the top of the stack.

0x77 dneg takes the negative value from the top of the stack and presses the result into the top of the stack.

0x78 ishl shifts the int value to the left by a specified number of digits and presses the result to the top of the stack

0x79 lshl shifts the long value to the left by a specified number of digits and presses the result to the top of the stack

0x7a ishr shifts the int value to the right (symbol) by a specified number of digits and presses the result to the top of the stack

0x7b lshr shifts the long value to the right (symbol) by a specified number of digits and presses the result to the top of the stack

0x7c iushr shifts the int value to the right (unsigned) by a specified number of digits and pushes the result onto the top of the stack

0x7d lushr shifts the long value to the right (unsigned) by a specified number of digits and pushes the result onto the top of the stack

0x7e iand makes the two int values at the top of the stack "bitwise and" and presses the results into the top of the stack.

0x7f land makes the values of the two lengths at the top of the stack "bitwise and" and presses the results to the top of the stack.

0x80 ior sets the two int values at the top of the stack as "bitwise OR" and presses the result into the top of the stack.

0x81 lor sets the two lengths at the top of the stack as "bitwise OR" and presses the result on the top of the stack.

0x82 ixor makes the two int values at the top of the stack "XOR by bit" and presses the result into the top of the stack.

0x83 lxor makes the values of the two lengths at the top of the stack "XOR by bit" and presses the results on the top of the stack.

0x84 iinc increases the specified int variable by the specified value (iTunes, iMurray, iLime2).

0x85 i2l forcibly converts the int value at the top of the stack to a long value and presses the result on the top of the stack

0x86 i2f forcibly converts int values at the top of the stack to float values and presses the results on the top of the stack

0x87 i2d forcibly converts an int value at the top of the stack to a double value and presses the result on the top of the stack.

0x88 L2i forcibly converts long values at the top of the stack to int values and presses the results on the top of the stack.

0x89 L2F forcibly converts long values at the top of the stack to float values and presses the results on the top of the stack.

0x8a L2d forcibly converts the long value at the top of the stack to the value of double and presses the result on the top of the stack.

0x8b f2i forcibly converts float values on the top of the stack to int values and presses the results on the top of the stack

0x8c f2l forcibly converts float values on the top of the stack to long values and presses the results on the top of the stack.

0x8d F2d forcibly converts float values on the top of the stack to double values and presses the results on the top of the stack.

0x8e D2i forcibly converts the value of the top of the stack to the value of int and presses the result on the top of the stack

0x8f d2l forcibly converts the value of the top of the stack to the value of long and presses the result on the top of the stack.

0x90 d2f forcibly converts the top stack double value to float value and presses the result on the top of the stack

0x91 i2b forcibly converts int values at the top of the stack to byte values and presses the results on the top of the stack

0x92 i2c forcibly converts int values at the top of the stack to char values and presses the results on the top of the stack

0x93 i2s forcibly converts the int value at the top of the stack to the value of the stack and presses the result on the top of the stack.

0x94 lcmp compares the numerical values of the two lengths at the top of the stack and presses the results (1, 0) into the top of the stack.

0x95 fcmpl compares the size of the two float values at the top of the stack and presses the result (1) into the top of the stack; when one of the values is NaN, press-1 into the top of the stack

0x96 fcmpg compares the size of the two float values at the top of the stack and presses the result (1) into the top of the stack; when one of the values is NaN, press 1 into the top of the stack

0x97 dcmpl compares the numerical values of the two pairs at the top of the stack and presses the result (1) into the top of the stack; when one of the values is NaN, press-1 into the top of the stack

0x98 dcmpg compares the numerical values of the two pairs at the top of the stack and presses the result (1) into the top of the stack; when one of the values is NaN, press 1 into the top of the stack

0x99 ifeq jumps when the int value at the top of the stack is equal to 0

0x9a ifne jumps when the int value at the top of the stack is not equal to 0

0x9b iflt jumps when the int value at the top of the stack is less than 0

0x9c ifge jumps when the int value at the top of the stack is greater than or equal to 0

0x9d ifgt jumps when the int value at the top of the stack is greater than 0

0x9e ifle jumps when the int value at the top of the stack is less than or equal to 0

0x9f if_icmpeq compares the size of two int values at the top of the stack and jumps when the result is equal to 0

0xa0 if_icmpne compares the size of two int values at the top of the stack and jumps when the result is not equal to 0

0xa1 if_icmplt compares the values of two int types at the top of the stack and jumps when the result is less than 0

0xa2 if_icmpge compares the two int values at the top of the stack and jumps when the result is greater than or equal to 0

0xa3 if_icmpgt compares the values of two int types at the top of the stack and jumps when the result is greater than 0

0xa4 if_icmple compares the two int values at the top of the stack and jumps when the result is less than or equal to 0

0xa5 if_acmpeq compares the two referenced values at the top of the stack and jumps when the results are equal

0xa6 if_acmpne compares the two referenced values at the top of the stack and jumps when the results are not equal

0xa7 goto unconditional jump

0xa8 jsr jumps to the specified 16-bit offset location and presses the next instruction address of jsr into the top of the stack

0xa9 ret returns to the instruction location of the index specified by the local variable (usually used in conjunction with jsr and jsr_w)

0xaa tableswitch is used for switch conditional jumps with consecutive case values (variable length instructions)

0xab lookupswitch is used for switch conditional jumps with discontiguous case values (variable length instructions)

0xac ireturn returns int from the current method

0xad lreturn returns long from the current method

0xae freturn returns float from the current method

0xaf dreturn returns double from the current method

0xb0 areturn returns an object reference from the current method

0xb1 return returns void from the current method

0xb2 getstatic gets the static domain of the specified class and presses its value to the top of the stack

0xb3 putstatic assigns a value to the static field of the specified class

0xb4 getfield gets the instance field of the specified class and presses its value to the top of the stack

0xb5 putfield assigns a value to the instance domain of the specified class

0xb6 invokevirtual calls instance method

0xb7 invokespecial calls superclass constructor method, instance initialization method, private method

0xb8 invokestatic calls static methods

0xb9 invokeinterface calls the interface method

0xba invokedynamic calls the dynamic link method

0xbb new creates an object and presses its reference value to the top of the stack

0xbc newarray creates a specified primitive type (such as int, float, char... And press its reference value to the top of the stack

0xbd anewarray creates an array of reference types (such as classes, interfaces, arrays) and presses its reference values on top of the stack

0xbe arraylength gets the length value of the array and presses it to the top of the stack

0xbf athrow throws the exception at the top of the stack

0xc0 checkcast verifies type conversion. ClassCastException will be thrown if the check fails.

0xc1 instanceof checks whether the object is an instance of the specified class. If you press 1 to the top of the stack, 0 will be pushed to the top of the stack.

0xc2 monitorenter acquires a lock on an object for synchronization methods or synchronization blocks

0xc3 monitorexit releases a lock on an object for synchronization methods or synchronization blocks

0xc4 wide expands the width of the local variable index

0xc5 multianewarray creates a multidimensional array of the specified type and dimension (when executing this instruction, the operation stack must contain the length values of each dimension) and presses its reference value to the top of the stack

Jump when 0xc6 ifnull is null

Jump when 0xc7 ifnonnull is not null

0xc8 goto_w unconditional jump

0xc9 jsr_w jumps to the specified 32-bit offset location and presses the next instruction address of jsr_w into the top of the stack

=

Breakpoint marking during 0xca breakpoint debugging

Language backdoor reserved for specific software by 0xfe impdep1

Language backdoor reserved for specific hardware by 0xff impdep2

The last three are reserved instructions.

About how the Java JVM bytecode instruction set is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report