- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 Java 代码:
public static void fun(int[] a) {
int min;
for(int j=0;j<a.length-1;j++) {
min=j;
for(int i=j+1;i<a.length;i++)
if(a[i]<a[min]) min=i;
if(min!=j) {
int temp = a[j];
a[j] = a[min];
a[min]=temp;
}
}
}
我在上述代码生成的类文件上使用了 javap -c
来打印上述程序的 Java 字节码,我得到以下输出:
0: iconst_0
1: istore_2
2: iload_2
3: aload_0
4: arraylength
5: iconst_1
6: isub
7: if_icmpge 64
10: iload_2
11: istore_1
12: iload_2
13: iconst_1
14: iadd
15: istore_3
16: iload_3
17: aload_0
18: arraylength
19: if_icmpge 39
22: aload_0
23: iload_3
24: iaload
25: aload_0
26: iload_1
27: iaload
28: if_icmpge 33
31: iload_3
32: istore_1
33: iinc 3, 1
36: goto 16
39: iload_1
40: iload_2
41: if_icmpeq 58
44: aload_0
45: iload_2
46: iaload
47: istore_3
48: aload_0
49: iload_2
50: aload_0
51: iload_1
52: iaload
53: iastore
54: aload_0
55: iload_1
56: iload_3
57: iastore
58: iinc 2, 1
61: goto 2
64: return
我的问题主要与 istore
命令有关。如您所见,在“1:”处,生成的字节码是istore_2
。为什么在这种情况下使用 istore_2
,而不是 istore_0
或 istore_1
?正如我们所见,稍后,在“11:”处,使用了 istore_1
,但是没有使用 istore_0
。有没有什么原因,为什么一开始用变量2,后面用变量1?
抱歉,如果上面的措辞不好,我现在才开始在大学学习 Java 字节码。
此外,我试图从我们必须学习使用的列表中找出哪些命令使用超过一个字节。这是列表,有人可以验证下面命令的“字节用法”(我不确定正确的术语)是否正确(我们必须在考试中使用下面给出的命令列表手动转换,我需要了解每个命令使用多少字节 - 在我们的讲座中解释得非常糟糕和快速,并且讲师没有在线注释)。
"aload_n" - 1 byte
"iconst_n" - 1 byte
"iaload"- 1 byte
"istore_n"- 1 byte
"arraylength"- 1 byte
"if_icmpge"- 3 bytes
"if_icmple"- 3 bytes
"iinc i c" - unsure? 3 bytes?
"goto n" - 3 bytes?
"ireturn" - 1 byte
最后,我相信 goto n
使用 3 个字节(如上所述),但是,有人可以解释为什么会这样吗?
非常感谢您的帮助,我意识到这是一个很长的问题!劳伦
最佳答案
关于字节的使用,可以引用specification .引用开头的注释:“指令格式图中的每个单元格代表一个 8 位字节”因此应该很容易获得每条指令的大小。
As you can see, at "1:", the bytecode generated is "istore_2". Why, in this case, is "istore_2" used, rather than "istore_0" or "istore_1"?
只是因为在第 1 行,字节码将零值存储到局部变量 j
中,该变量恰好位于索引 2 处。类似地,istore_1
将值存储在操作数堆栈到索引 1 处的局部变量(在本例中为 min
)。
之所以可行,是因为长话短说,每个方法都分配了一个堆栈帧,其中包含 local variables。在其中,可通过在编译时分配的索引访问。
对于goto
指令,这也在specification中:
goto
branchbyte1
branchbyte2
The unsigned bytes branchbyte1 and branchbyte2 are used to construct a signed 16-bit branchoffset, where branchoffset is (branchbyte1 << 8) | branchbyte2. Execution proceeds at that offset from the address of the opcode of this goto instruction. The target address must be that of an opcode of an instruction within the method that contains this goto instruction.
操作码后有两个字节,组成16位偏移量。生成的目标地址计算如下:
target_address = address_of_current_goto + 16_bit_offset
关于Java字节码初学者问题: order of "istore" variable usage, "byte usage"等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34559067/
我有以下代码: foreach (byte b in bytes) { byte inv = byte.MaxValue - b; // Add the new value to a
我需要从这个文本文件source.txt中读取内容并将内容反向写入这个文本文件destination.txt。读取和写入必须使用逐字节完成! 我使用 BufferedReader 和 Buffered
我需要存储大量 RGB 颜色对象。对于某些常见用途,这些占用了我的应用程序总内存的 8% 到 12%。我目前将其定义如下: class MyColor { byte red; byte green;
我有一个由字节数组表示的整数。 byte[] result = getResult(); resultInt1 = Integer.parseInt(Bytes.toString(result));/
我正在尝试使用 Rusoto 库调用 AWS Lambda 函数。该请求有一个 JSON 编码的有效负载,我目前将其作为一个字符串,但该库为此坚持使用 bytes::bytes::Bytes 结构。我
我正在尝试基于 Tokio's example 编写一个 TCP 服务器. 当我尝试发送缓冲区时,编译器返回错误 0277。 我的代码:(playground) extern crate tokio;
我知道我可以通过 IList 进行枚举,例如: public byte[] ConvertToByteArray(IList> list) { IList newList = new List
考虑这样一个文本文件: Some text here. --- More text another line. --- Third part of text. 我想把它分成三部分,用---分隔符分开。
如果我有一个字节变量:byte b = 0; 为什么以下工作: b++; b += 1; // compiles ...但这不是吗? b = b + 1; // compile er
我有一个简单的字节数组,我想从中获取颜色。我的计划是用红色表示三位,绿色表示三位,蓝色表示两位。 8 位。 我认为颜色是正确的: 如有错误请指正 byte[] colours = new byte[
我的目标是比较两个字节数组中的两个字符串值。它实际上需要创建两个新的字符串对象才能使用 contains 方法。是选择正确还是有什么办法可以使用优化方式而不使用新的关键字。 if(new String
我正在使用github.com/tarm/serial来连接一些串行仪器。在开发过程中,我使用/dev/ttyp0和/dev/ptyp0对,其中go进程连接到一个,我使用screen连接到另一个。我编
好的,所以如果一个字节是 8 位,那么半字节就是 4 位。并且您可以将四分之一字节作为 2 位(尽管我想,如果有的话,它会被称为双位)。 虽然这是一致的,但如果我使用这个词,有人会感到困惑(或惊讶)吗
我在解释文件时遇到问题。文件构建如下: "name"-@-"date"-@-"author"-@-"signature" 签名是一个字节数组。当我读回文件时,我将其解析为 String 并拆分它: m
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
Java 让我很难过,因为它需要 ArrayList 的包装类秒。我将如何添加 byte[]到 ArrayList ? 最佳答案 LOL 认为我必须包装所有东西。 ArrayList作品。谢谢一晒。
我有一个 16 字节的 md5 散列,我需要使用 XOR 将其“折叠”成 4 字节数据:{1st 4 bytes} XOR {2nd 4 bytes} XOR {3rd 4 bytes} XOR {4
我正在学习SMSC smc91cx驱动代码,我学习了如何根据Application Note 9-6的说明编写smc91c111网卡的测试代码。 .我无法理解“传输数据包”下的以下说明: Write
我必须附加(可变数量的)字节数组。集合似乎只适用于包装类,即 Byte。大约 20 小时后,我想到了这个,并且它有效,但我想知道它是否可以改进(添加到列表,但欢迎任何其他改进建议:),即 Collec
我有两个基本相同的操作: insert_bytes(from, count) delete_bytes(start, stop) -> delete_bytes(from, count) insert
我是一名优秀的程序员,十分优秀!