• Java虚拟机运算指令学习


    做一个测试程序,

    1. public class arthtest {
    2. public int mymaths (int a, int b) {
    3. int c = a + b;
    4. int d = a - b;
    5. int e = a * b;
    6. int f = a / b;
    7. int g = a % b;
    8. return 1;
    9. }
    10. }

    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 类型数值相加并将结果压入栈顶

    其他指令类似;

  • 相关阅读:
    项目开发过程中遇到了什么困难?
    想分析全国用电及煤气、液化石油气供应利用情况,这部分数据对你有帮助!
    LVS集群(Linux Virtual server)
    Java8新特性—四大内置函数式接口
    轻量级RPC框架‘dubbo’
    自然人如何在浙江法院网上完成诉讼——具体流程
    76. 最小覆盖子串
    后端跨域问题的处理
    【AutoGPT】踩坑帖(follow李鱼皮)
    ubuntu 安装 notepad++
  • 原文地址:https://blog.csdn.net/bcbobo21cn/article/details/132735671