Java虚拟机运算指令学习

做一个测试程序,

public class arthtest {
    public int mymaths (int a,  int b) {
        int c = a + b;
        int d = a - b;
        int e = a * b;
        int f = a / b;
        int g = a % b;
        return 1;
    }
}

javac,然后再javap;我把操作和输出文本贴一下,可能用到;

D:\Javaprj>javac arthtest.java

D:\Javaprj>javap -v arthtest.class
Classfile /D:/Javaprj/arthtest.class
  Last modified 2023-9-7; size 292 bytes
  MD5 checksum 96ab4ce3e6c03d077d2d600a889bcf4d
  Compiled from "arthtest.java"
public class arthtest
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #3.#12         // java/lang/Object."":()V
   #2 = Class              #13            // arthtest
   #3 = Class              #14            // java/lang/Object
   #4 = Utf8              
   #5 = Utf8               ()V
   #6 = Utf8               Code
   #7 = Utf8               LineNumberTable
   #8 = Utf8               mymaths
   #9 = Utf8               (II)I
  #10 = Utf8               SourceFile
  #11 = Utf8               arthtest.java
  #12 = NameAndType        #4:#5          // "":()V
  #13 = Utf8               arthtest
  #14 = Utf8               java/lang/Object
{
  public arthtest();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."":()V
         4: return
      LineNumberTable:
        line 1: 0

  public int mymaths(int, int);
    descriptor: (II)I
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=8, args_size=3
         0: iload_1
         1: iload_2
         2: iadd
         3: istore_3
         4: iload_1
         5: iload_2
         6: isub
         7: istore        4
         9: iload_1
        10: iload_2
        11: imul
        12: istore        5
        14: iload_1
        15: iload_2
        16: idiv
        17: istore        6
        19: iload_1
        20: iload_2
        21: irem
        22: istore        7
        24: iconst_1
        25: ireturn
      LineNumberTable:
        line 3: 0
        line 4: 4
        line 5: 9
        line 6: 14
        line 7: 19
        line 8: 24
}
SourceFile: "arthtest.java"

D:\Javaprj>

看一下代码中加减乘除取模对应的指令依次是,iadd、isub、imul、idiv、irem;

这是整型的情况;

以add指令来说,有四类,

iadd
    将栈顶两 int 类型数值相加并将结果压入栈顶
ladd
    将栈顶两 1ong 类型数值相加并将结果压入栈顶
fadd
    将栈顶两 float 类型数值相加并将结果压入栈顶
dadd
    将栈顶两 double 类型数值相加并将结果压入栈顶

其他指令类似;

你可能感兴趣的:(Java,汇编语言,java,javac,javap,iadd)