I was reading java bytecode and saw this:
getfield #5 (Field java.lang.String name)
What does #5
mean?
And how can I write a program in bytecode?
Java class files and bytecode
Java class files (bytecode-files) is composed by different components:
http://en.wikipedia.org/wiki/Java_class_file
The number #5 simply refers to a location in the constant pool. And in that position a CONSTANT_FieldRef is found which contains a reference to a CONSTANT_NameAndType among other attributes. And CONSTANT_NameAndType contains a reference to a CONSTANT_Utf8 (which contains the actual string/name.)
So the flow looks like this:
getfield #number -> FieldRef -> NameAndType -> Utf8 -> string
http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html
So instead of saving a whole string in each getfield
instruction a number is saved. This improves performance in the interpreter (or JIT) and space in the class file.
Hand-write bytecodes
Hand-written bytecodes can be assembled to a class file with this tool (it contains a lot of examples):
http://jasmin.sourceforge.net/
The getfield
instruction (IIRC) makes a reference into the class file's constant pool for information about what field should be looked up. The #5 here means "constant pool entry number 5," and this constant pool then contains information saying "look up the field name
of type java.lang.String
). The reason for this is that it keeps the size of the getfield
instruction the same, regardless of the name or type of the field to look up.
I'm not sure I understand what you mean by "how can I write program in bytecode?" This is a pretty open-ended question; it's akin to asking how to write programs in any language, and requires a lot of learning. You may want to look into the Jasmin Java assembler, which can greatly simplify this process.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With