When I have compiled my ByteCode how do I insert it into my methods

I am new to java and processing what I actually do understand is that they are C++ based. I only understand a little about C. I do not have time to look at thousands of java files just to do something that should be simple. I do understand enough about assembly to do what I need to do. I really do need explicit instructions to do this. So if you want to know why, it is because I am working on an antivirus and I need to be able to simulate the instructions. so what I need is a PEEK function a POKE function and to simulate coprocessor functions and read and write their values because the coprocessor uses a floating point value that is inaccessible with Java and Processing.
the JavaAsmBlock() does not work with Eclipse and when I try to import a .java file with it says that I need the ‘@’ character

Java does not have the equivalent of PEEK and POKE which are BASIC programming language instructions to directly access memory. Java was designed to prevent users getting or using memory addresses directly.

Unlike C and C++ where you can directly access memory. In fact there are very few simuarities between Java and C++ except in syntax styling.

3 Likes

Hi, @AntiViral_Builder

While it is possible to access memory outside JVM e.g. by using JNI, it is difficult and complex – by design. JVM is a process virtual machine, and virtual machines are not set up for this – they are the opposite. Think from Pascal p-code (p-machine) to the Infocom z-machine for Zork… JVM is a VM in that VM lineage. It abstracts hardware for portability. It is also a higher-level language – e.g. it has automatic memory management with dynamic memory allocation and garbage collection.

Happy to help you try to do whatever you are trying to do, but it is possible that this is just the wrong tool for your needs.

Consider: “I’m on this train thing, I don’t have time for the details, just tell me how to simply turn the steering wheel left, like I easily can in my car.” Well, trains don’t (usually) do that by design. Or: “Okay, I’m in this boat thing, don’t bother me with this complex ocean stuff just tell me how to pull the parking break – this should be simple.” Well, boats don’t (usually) do that. There is a thing called a sea anchor, but using it may be complicated, and it is certainly situation-dependent. “I’m flying this airplane, tell me where the ‘reverse’ gear is so I can back up like in my car – this should be simple.” Well, planes don’t usually have a usable reverse gear while they are in the air – you can only back an airplane up in special circumstances on the ground, and some planes you can’t at all without complicated assistance. Trying to pilot a boat or a plane as if it is a car often doesn’t work.

Processing is also very different from assembly – just like trains aren’t good at free steering, boats aren’t good at unassisted parking, and planes aren’t good at backing up in midair, JVM is not good at peeking and direct memory management. It might be possible, but it is also possible that there is another way to use Processing in order to get you where you want to go. Or that something else would work better for you.


That said, I have no experience with it but it looks like you can play around with MappedByteBuffers, so this is technically possible: java - Allocating memory outside JVM and using it inside JVM - Stack Overflow

…and there are frameworks in Java (not Processing) for bytecode manipulation – in particular, check out ASM:

…but my impression (not having done it) is that this is a big undertaking. If you don’t want to deal with huge complex Java API in order to get this working, you may end up disappointed. You are basically backing up a 747 jet, and that takes a whole team and a lot of special equipment.

2 Likes

I have just read @jeremydouglass’s comment and reading the discussion title again …

When I have compiled my ByteCode how do I insert it into my methods

it reminded my that I used ASM in the Jasmine library (Java ASM IN Expression*).

For those that don’t know Jasmine it evaluates equations expressed as strings at runtime e.g.
5x^2 + sin(x) / sqrt(x)

It does this by compiling the string expression into executable ByteCode embeds that into a Java class so it can be executed.

The trick I found with Jasmine was to prepare as much as possible in Java source code. So it creates an Expression class which is very basic - here it is without the commensts

public abstract class Expression extends AbstractExpression {

    public abstract Expression eval(double... vars);
}

Jasmine can also compile algorithmic code containing constructs to perform sequence/selection/iteration statements. The AbstractExpression class is the base class for both the Expression and Algorithm classes it is not large but I won’t include it here.

So the library uses ASM to create a child class of Expression with just one method overrides the eval method (done in CompileExpression class). All that is left is to instantiate an object of the new class and call the eval method.

So if you want to use ASM you might look at the Jasmine source code for inspiration and the above explanation provides a quick insight in how I used ASM.
HTH :slight_smile:

PS good grief I wrote Jasmine 6 years ago, all I remember was that the ASM learning curve is huge and I just touched the surface of its power. Good luck

2 Likes