Disclaimers: I’ll use Windows and more particularly Visual C++ with its Inline Assembler. If you use MacOs or Linux you will have significant differences comparing to what is described in the article. Everything below is shown mostly for demonstration purposes Introduction Java is mature self-sufficient language, though as we all know it is possible to “connect” java to C (via Java-Native-Interface or JNI) to speed up some critical pieces of code. Also for C/C++ it is possible to delegate some even more critical pieces of code directly to Assembly. In this article I want to show you how this Java-C-Assembly Matryoshka can look like. But note that example will be pretty simple so in real world there is no advantage of such delegation as it won’t speed up anything. The example we’ll look at will be: given a command line program (written in Java) we execute the program with providing 2 integers as arguments (with error handling on the client side) main business logic is the “sum” method, which we consider a critical piece of the program we’d like to “speed up” using C and Assembly Java Setup First of all we need to download . I have pretty old version installed, but feel free to install newer version. After installation verify that everything works: JDK >java -version java version Java(TM) "1.8.0_121" SE Runtime Java Client Environment (build _121-b13) 1.8 .0 HotSpot (TM) VM (build -b13, mixed mode, sharing) 25.121 Code Here is our program: main function, parsing command line arguments and our target method “sum” (with implementation in java): { { (args.length != ) { System.out.println( ); ; } a; { a = Integer.parseInt(args[ ]); } (Throwable throwable) { System.out.println( ); ; } b; { b = Integer.parseInt(args[ ]); } (Throwable throwable) { System.out.println( ); ; } Test test = Test(); System.out.println(test.sum(a, b)); } { a + b; } } public class Test public static void main (String[] args) if 2 "Error: wrong params count" return int try 0 catch "First param is not a number" return int try 1 catch "Second param is not a number" return new public static int sum ( a, b) int int return Compile In order to run the program we first need to compile it with java compiler. It will generate binary file which we’ll later on execute. Test.class > javac Test.java Execute To execute program call java and provide arguments. See that our program works correctly and prints sum of numbers. > java Test 3 4 7 C/JNI Setup Install and using it install Visual C++ build tools: Chocolatey choco install visualcpp-build-tools We’ll need these tools to compile C files into library dll file. Specifically for compilation we’ll need command, so check that it works: cl > cl C/C++ Optimizing Compiler Version 19.16.27031.1 x86 Microsoft Corporation. All rights reserved. Microsoft (R) for Copyright (C) Code Java can communicate with C via JNI. In order to setup that communication we need to update our Java program: { { (args.length != ) { System.out.println( ); ; } a; { a = Integer.parseInt(args[ ]); } (Throwable throwable) { System.out.println( ); ; } b; { b = Integer.parseInt(args[ ]); } (Throwable throwable) { System.out.println( ); ; } System.loadLibrary( ); Test test = Test(); System.out.println(test.sum(a, b)); } ; } public class Test public static void main (String[] args) if 2 "Error: wrong params count" return int try 0 catch "First param is not a number" return int try 1 catch "Second param is not a number" return "Test" new public native int sum ( a, b) int int First, instead of static method with implementation we provide so called native method. It doesn’t have any implementation because we expect it to be provided via JNI. Second, we need to load our C library — and we do that with method. System.loadLibrary After we updated our program we need to generate Test.h header file: > javah Test Generated header file will contain all the setup for our C program. We had one native method in our Java program and here we have method declaration for our method generated in header file: #include <jni.h> #ifndef _Included_Test #define _Included_Test #ifdef __cplusplus extern { #endif ; #ifdef __cplusplus } #endif #endif /* DO NOT EDIT THIS FILE - it is machine generated */ /* Header for class Test */ "C" /* * Class: Test * Method: sum * Signature: (II)I */ JNIEXPORT jint JNICALL Java_Test_sum (JNIEnv *, jobject, jint, jint) We need to have an implementation of our function, so create Test.c file and implement method: #include { a + b; } {} "Test.h" JNIEXPORT jint JNICALL Java_Test_sum (JNIEnv *env, jobject obj, jint a, jint b) return void main () Compile Compile our C library into Test.dll: > cl -I -I -LD Test.c -FeTest.dll "%JAVA_HOME%\include" "%JAVA_HOME%\include\win32" Execute Recompile our Java program as shown above and execute to see that it still works: > java Test 3 4 7 Assembly Code Previously we had C implementation which looked basically as on java: . When we work with Assembly we work on a lower level so such small operations require quite more code. Let’s update our C program to use Assembly — for this we add — which is Inline Assembler for Visual C++. Inside that block we write instructions. You see that we put our variable a into register , put our variable into register . Then we make a sum from contents of registers and store it in the register (this is what add command does). Lastly we store value of the register in our result field: a + b __asm block eax b ebx eax eax #include #include <stdio.h> JNIEXPORT jint JNICALL Java_Test_sum (JNIEnv *env, jobject obj, jint a, jint b) { int result; __asm { mov eax, a mov ebx, b add eax, ebx mov result, eax } result; } main() {} "Test.h" return void Recompile C library (no need to recompile Java program) and execute Java program again: > java Test 3 4 7 So, it works. Hope you’ve enjoyed and maybe learned something today. If not then I hope at least it was funny. Happy coding! References The javac tool reads class and interface definitions, written in the Java programming language, and compiles them into…docs.oracle.com If you have a few years of experience in the Java ecosystem, and you're interested in sharing that experience with the…www.baeldung.com Assembly language serves many purposes, such as improving program speed, reducing memory needs, and controlling…docs.microsoft.com