I know Java is a secure language but when matrix calculations are needed, can I try something faster?
I am learning __asm{} in C++, Digital-Mars compiler and FASM. I wan
You cannot directly inline assembly in your Java code. Nevertheless, contrarily to what is claimed by some other answers, conveniently calling assembly without going through any intermediary C (or C++) layer is possible.
Quick walkthrough
Consider the following Java class:
public class MyJNIClass {
public native void printVersion();
}
The main idea is to declare a symbol using the JNI naming convention. In this case, the mangled name to use in your assembly code is Java_MyJNIClass_printVersion. This symbol must be visible from other translation units, which can for instance be achieved using the public directive in FASM or the global directive in NASM. If you're on macOS, prepend an extra underscore to the name.
Write your assembly code with the calling conventions of the targeted architecture (arguments may be passed in registers, on the stack, in other memory structures, etc.). The first argument passed to your assembly function is a pointer to JNIEnv, which itself is a pointer to the JNI function table. Use it to make calls to JNI functions. For instance, using NASM and targeting x86_64:
global Java_MyJNIClass_printVersion
section .text
Java_MyJNIClass_printVersion:
mov rax, [rdi]
call [rax + 8*4] ; pointer size in x86_64 * index of GetVersion
...
Indexes for JNI functions can be found in the Java documentation. As the JNI function table is basically an array of pointers, don't forget to multiply these indexes by the size of a pointer in the targeted architecture.
The second argument passed to your assembly function is a reference to the calling Java class or object. All subsequent arguments are the parameters of your native Java method.
Finally, assemble your code to generate an object file, and then create a shared library from that object file. GCC and Clang can perform this last step with a command similar to gcc/clang -shared -o ....
Additional resources
A more comprehensive walkthrough is available in this DZone article. I have also created a fully runnable example on GitHub, feel free to take a look and play around with it to get a better understanding.