Posts Tagged opcode
Archive: IJVM Machine Code Example
Yet another blog post from ye’ old blog.
This post was originally titled “IJVM Machine Code Example” and posted on the 8th of September, 2007
This is a little addition to my IJVM/ISA theme/posts. Now I will be going a little further into the whole Opcode part of IJVM/ISA.
First let me introduce you to a little “fun” machine code snippet (Hex representation of IJVM Machine Code):
00 01 00 00 10 ff a7 ff fe
It might not be that obvious, but this little 9 byte machine code, will actually make an endless loop, pushing fe (-1) onto the stack for each iteration. The end result will of course be a stack growing until there are no more memory.
Lets try to look at the machine code, and describe how the bytes are formatted/arranged. The first 4-bytes defines how many arguments and local variables there are. The first 2-bytes is a number representing how many arguments there are, and the next 2-bytes how many locals. In this particular case we have:
00 01 = 1 argument ; there is always the Obj-Ref/Link Pointer 00 00 = 0 locals ; the code isn't using any locals
Next we have the actual start of the code, from now on, each byte is a representation of either a Opcode, or an argument for the last Opcode. Each opcode, defines how many arguments it is taking. Here are some examples.
| Opcode | Symoblic name | Argument size | Description |
| 0×10 | BIPUSH | 1-byte |
Pushes the next 1-byte argument (representing an Integer in the range [-128, 127]) onto the stack |
| 0×59 | DUP | 0-byte |
This duplicates (copy) the top-word on the stack |
| 0xa7 | GOTO | 2-byte |
Jumps the execution to the new part in the memory. Moves the pointer relatively to the current pointer by the number of words defined in the 2-byte argument representing an integer in the range [-32.768, 32.767] |
Now lets see what the last 5-bytes of the machine code example, is actually doing.
10 ff = bipush -1 ; 10 is the opcode for bipush, and takes a 1-byte argument (ff) which is an integer representation of -1 a7 ff fe = goto -2 ; a7 is the opcode for goto, and this takes a 2-byte argument (ff fe), which is an integer representation of -2
So it pushes the number -1 onto the stack, and goes back two bytes (words). Since the pointer is at the 0xa7 instruction when executing, it will go two bytes back, to the 0×10 instruction, which again will make it push another number -1, onto the stack, and so fourth.
To execute this machine code in the IJVM Virtual Machine, you have to add some extra information though. Information of where the main method is starting, and information on the constant pool. Also remember to use UNIX formatting of the file, for example if you are using Windows to edit the files.
Now lets look at this example, wrapped with the information for the virtual machine executer.
main index: 0 method area: 9 bytes 00 01 00 00 10 ff a7 ff fe constant pool: 1 words 00000000
So that is it, for this example.
Facebook
Twitter
Youtube
Last
Ping