欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

Java bytecode assembler : jasmin,bytecodeassembler

来源: javaer 分享于  点击 13077 次 点评:62

Java bytecode assembler : jasmin,bytecodeassembler


   如果要自己来写Java的编译器,我是指出编译出来的目标程序文件是Java的class执行文件.那么这里有个很好的工具: jasmin. 这个工具是一个Java的汇编编译器.能够将类式汇编的bytecode指令,翻译成最终的Java的class文件.

  jasmin本身也是Java来写.不过和其它很多Java bytecode Assembler比起来,感觉它的优势是最大的,最广泛使用的.

  Jasmin也是一个有名的开源项目,它的主页是: http://jasmin.sourceforge.net

  这里我贴一下jasmin里面的一个例子程序HelloWorld.j的编译过程.

  ; --- Copyright Jonathan Meyer 1996. All rights reserved. -----------------
; File:      jasmin/examples/HelloWorld.j
; Author:    Jonathan Meyer, 10 July 1996
; Purpose:   Prints out "Hello World!"
; -------------------------------------------------------------------------


.class public examples/HelloWorld
.super java/lang/Object

;
; standard initializer
.method public <init>()V
   aload_0
 
   invokenonvirtual java/lang/Object/<init>()V
   return
.end method

.method public static main([Ljava/lang/String;)V
   .limit stack 2

   ; push System.out onto the stack
   getstatic java/lang/System/out Ljava/io/PrintStream;

   ; push a string onto the stack
   ldc "Hello World!"

   ; call the PrintStream.println() method.
   invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

   ; done
   return
.end method

 然后使用java -jar jasmin.jar examples/HelloWorld.j 命令,就可以编译得到HelloWorld.class这个Java文件了.

  是不是很爽啊?

  有了这个工具,大家就可以不用深入研究Java class文件结构了,而可以直接写Java编译器.甚至是其它编译器,然后最后可以生成Java class文件.


相关文章

    暂无相关文章
相关栏目:

用户点评