flat assembler
Message board for the users of flat assembler.

Index > Non-x86 architectures > Assembling JVM bytecode

Author
Thread Post new topic Reply to topic
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8354
Location: Kraków, Poland
Tomasz Grysztar 10 Feb 2012, 18:39
I've been suspecting for a long time that it should be possible to use fasm to assemble Java bytecode, and out of curiosity I tried it this afternoon. I created a set of simple macros for composing the .class file with fasm, and a bunch of even simpler macros for generating bytecode, stitched it all together and got a working class. Perhaps more complex macros could be devised that would allow simpler usage, but with this simple approach the structure of .class file is nicely visualized, and it is anyway easy to play with it if you already know something about JVM and its interfaces.

It can be useful for educational purposes, I doubt one would want to write some programs this way... Or perhaps I'm mistaken? Wink

To test it, assemble Test.asm into Test.class and then run "java Test".


Description: JVM class assembled with fasm
Download
Filename: fasmjava.zip
Filesize: 3.79 KB
Downloaded: 1516 Time(s)

Post 10 Feb 2012, 18:39
View user's profile Send private message Visit poster's website Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u 10 Feb 2012, 20:46
This looks very interesting, i am not into java, but this could just change my mind.
Thanks, i will have a play with it.
Post 10 Feb 2012, 20:46
View user's profile Send private message Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 10 Feb 2012, 22:26
Thank you, sounds interesting. Very Happy
Post 10 Feb 2012, 22:26
View user's profile Send private message Send e-mail Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8354
Location: Kraków, Poland
Tomasz Grysztar 10 Feb 2012, 22:37
One more sample - this time a code that looks a little bit more like assembly programming. It loops and displays squares of numbers from 17 down to 1. As you can see there, the JVM's local variables resemble a bit the CPU registers in usage (even though JVM is stack-based architecture).

This is what the "main" method code in second sample looks like:
Code:
                ldc number
                istore 1
           example_loop:
                iload 1
                dup
                imul
                invokestatic Integer_toString
                getstatic System.out
                swap
                invokevirtual PrintStream_println
                iinc 1,-1
                iload 1
                ifne example_loop    
One can also replace "ldc number" with "bipush 17" and get rid of the number entry in constant pool. And "swap" would not be needed if "getstatic System.out" was moved to the top of the loop. These sub-optimalities are there for the demonstration purposes.


Description: Another JVM bytecode example
Download
Filename: fasmjava2.zip
Filesize: 3.74 KB
Downloaded: 1377 Time(s)

Post 10 Feb 2012, 22:37
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 11 Feb 2012, 02:58
Quote:
I doubt one would want to write some programs this way... Or perhaps I'm mistaken?
Well, I happen to have a relatively mediocre Java-enabled cellphone which would surely be noticeable if the Java compiler performs such a bytecode that ends up slower than a hand written one. Not sure if I'll able to have time to test this (have planned to do this with Java assemblers long ago), but I'll let you know if I come up with something Razz
Post 11 Feb 2012, 02:58
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8354
Location: Kraków, Poland
Tomasz Grysztar 11 Feb 2012, 23:49
Just for the sake of it, I tried to write a class that actually does something useful. It implements "getProperty" method that allows to retrieve value of bean-like property from object. For example when it is called with any object as first argument, and "weight" string as second argument, it will return the result of invoking ".getWeight()" method on that object. It is written in a very dirty way and will fail on method names starting with something other than latin letter, or on the too long ones. But it is simple and probably fast. Smile Though while writing it I learned more about the rigor that Java verification facility puts on the bytecode, and it really prevents trying to do some really dirty optimization tricks.


Description: Assembled class used from Java
Download
Filename: DirtyRetriever.zip
Filesize: 5.12 KB
Downloaded: 1356 Time(s)

Post 11 Feb 2012, 23:49
View user's profile Send private message Visit poster's website Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 841
Location: Jakarta, Indonesia
TmX 12 Feb 2012, 14:34
Cool Very Happy, maybe one day FASM will be listed on this site:
http://www.is-research.de/info/vmlanguages/category/jvm-language/
Post 12 Feb 2012, 14:34
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4339
Location: Now
edfed 13 Feb 2012, 13:49
how do you make it work? i'm sure i am doing it wrong.

i tested under win98, win7 and ubuntu, using javaw.exe and javaws.exe from jre1.6... everytime i try to launch a .class or .java bin with the java runtime environment, it don't works, it says "invalid or corrupted file"
or "missing field" under win98, i'll post a win98 shot tonight.


Description: ShellExecute() is cool!!! :) under win7...
Filesize: 12.88 KB
Viewed: 32910 Time(s)

fasmjavacorrupt.png


Post 13 Feb 2012, 13:49
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8354
Location: Kraków, Poland
Tomasz Grysztar 13 Feb 2012, 14:11
There is a TEST.BAT file in the last example, try if it works for you.
Post 13 Feb 2012, 14:11
View user's profile Send private message Visit poster's website Reply with quote
shutdownall



Joined: 02 Apr 2010
Posts: 517
Location: Munich
shutdownall 13 Feb 2012, 16:28
edfed wrote:
how do you make it work? i'm sure i am doing it wrong.

i tested under win98, win7 and ubuntu, using javaw.exe and javaws.exe from jre1.6... everytime i try to launch a .class or .java bin with the java runtime environment, it don't works


What do you enter exactly ?
The error means, that javaw is missing a java archive (jar).

Try to use java (console) instead of javaw.
It will automatically expect the extension .class - so just type "java Test" and not "java Test.class" and be sure to call from the directory containing the Test.class file.
Post 13 Feb 2012, 16:28
View user's profile Send private message Send e-mail Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 13 Feb 2012, 17:13
A project that might be of interest on this thread: http://sourceforge.net/projects/rawxmllibrary/?source=directory (fasm, java assembly, .Net assembly)
Post 13 Feb 2012, 17:13
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4339
Location: Now
edfed 13 Feb 2012, 22:50
lol, finally i get it. in fact, it worked, i didn't noticed that because of some errors reported by the bat file execution, for 98, only this is needed.
Code:
fasm DirtyRetriever.asm
java Test
pause


    

to get this very beautyfull result:
Code:

C:\fasmj>

C:\fasmj>test.bat

C:\fasmj>fasm DirtyRetriever.asm
flat assembler  version 1.69.14  (138098 kilobytes memory)
2 passes, 847 bytes.

C:\fasmj>java Test
main
([Ljava/lang/String; )V

C:\fasmj>pause
Appuyez sur une touche pour continuer . . .
    

sorry for the disturbing quiche problem. tomorrow, i will test again on 7, and maybe XP, maybe i just need to install correctlly java to get it work. the same for ubuntu.

and now, let's code a little to see what can be done Smile

[edit] bored about win7 tests, nothing wants to work properlly without dealing with this f******* system, then... give it up.
Post 13 Feb 2012, 22:50
View user's profile Send private message Visit poster's website Reply with quote
nocona



Joined: 04 Aug 2007
Posts: 35
nocona 11 Mar 2012, 15:16
few years back i did played with jvm bytecode/java class format and also .net clr header and its associated structure in the pe file format. although succeed in building test app manually with fasm, eventually i give up while trying to create set of macros to simplify the programming process. coz i cant find a way to dynamically create the entry in the constant_pool structure in the class file which i think is impossible to achieve.

and for the .net extension to pe... it's just too complex Sad.

too bad all my example files are lost..
Post 11 Mar 2012, 15:16
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8354
Location: Kraków, Poland
Tomasz Grysztar 11 Mar 2012, 17:25
nocona wrote:
eventually i give up while trying to create set of macros to simplify the programming process. coz i cant find a way to dynamically create the entry in the constant_pool structure in the class file which i think is impossible to achieve.
Yes, this is something that would require the "addressable virtual spaces" feature which I mentioned in my presentation about ideas for fasm 2, which is not possible with current fasm 1.x internal architecture.
Post 11 Mar 2012, 17:25
View user's profile Send private message Visit poster's website Reply with quote
nocona



Joined: 04 Aug 2007
Posts: 35
nocona 18 Mar 2012, 11:45
is there other link where i can download the video? i cant access the link mentioned in that thread http://tokk.biz/fasmcon2009/M2U00510.avi. also maybe any documents which i can read about fasm 2?
Post 18 Mar 2012, 11:45
View user's profile Send private message Reply with quote
Madis731



Joined: 25 Sep 2003
Posts: 2139
Location: Estonia
Madis731 20 Mar 2012, 12:10
Yeah, like OllyDBG 2. FASM 2 should have a special place. People could brainstorm & arrange ideas there and there would be some documents like prelude or something.
Post 20 Mar 2012, 12:10
View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4043
Location: vpcmpistri
bitRAKE 23 Aug 2013, 21:47
Is there an error in bytecode.inc?
Code:
macro ret index { if index<100h
                    db 0xa9,index
                  else
                    db 0xc4,(index) shr 8,(index) and 0FFh
                  end if }    
...if I'm reading the documentation correctly 0xc4 needs to be followed by the opcode (0xa9 in this case).
Post 23 Aug 2013, 21:47
View user's profile Send private message Visit poster's website Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777 25 Aug 2013, 10:34
bitRAKE: Yes, 0xc4=wide prefix then 0xa9 opcode. 16BIT value after is high-endian. Fourth line should be:
Code:
db 0xc4,0xa9,(index) shr 8,(index) and 0FFh    

JASM: Java Assembler. Example Applet+JFrame Application written in FASM!
Post 25 Aug 2013, 10:34
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.