# Can I make my own library with my assembly code inside it and how would I do it

**URL:** <https://discourse.processing.org/t/can-i-make-my-own-library-with-my-assembly-code-inside-it-and-how-would-i-do-it/21515>\
**Category:** Project Guidance\
**Created:** [June 3, 2020, 12:41am UTC](https://discourse.processing.org/t/can-i-make-my-own-library-with-my-assembly-code-inside-it-and-how-would-i-do-it/21515 "2020-06-03T00:41:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AntiViral\_Builder](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@AntiViral\_Builder](https://discourse.processing.org/u/AntiViral_Builder)\
**Post date:** [June 3, 2020, 12:41am UTC](https://discourse.processing.org/t/can-i-make-my-own-library-with-my-assembly-code-inside-it-and-how-would-i-do-it/21515/1 "2020-06-03T00:41:52Z")

</div>

So what I need is to somehow include assembly instructions into my project I don’t have time for a lot of complicated things that I cannot understand. If building a library is the easies way to do it then I have to take that route whatever it costs. Please help me.

---

<div class="post-metadata">

**Author:** ![mgood7123](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@mgood7123](https://discourse.processing.org/u/mgood7123)\
**Post date:** [June 3, 2020, 1:12am UTC](https://discourse.processing.org/t/can-i-make-my-own-library-with-my-assembly-code-inside-it-and-how-would-i-do-it/21515/2 "2020-06-03T01:12:41Z")

</div>

wouldnt using assembly make your Processing 3 project platform dependent?

---

<div class="post-metadata">

**Author:** ![noahbuddy](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@noahbuddy](https://discourse.processing.org/u/noahbuddy)\
**Post date:** [June 3, 2020, 7:07am UTC](https://discourse.processing.org/t/can-i-make-my-own-library-with-my-assembly-code-inside-it-and-how-would-i-do-it/21515/3 "2020-06-03T07:07:45Z")

</div>

Unless you are referring to Java bytecode, Java Native Interface would probably be the way to go.

> **[Java Native Interface](https://en.wikipedia.org/wiki/Java_Native_Interface)**
>
> In software design, the Java Native Interface (JNI) is a foreign function interface programming framework that enables Java code running in a Java virtual machine (JVM) to call and be called by native applications (programs specific to a hardware and operating system platform) and libraries written in other languages such as C, C++ and assembly.
> JNI enables programmers to write native methods to handle situations when an application cannot be written entirely in the Java programming language, e....

I am curious what you are working on given your apparent need for low-level execution.

I would not expect a lot of speedup above using C/C++.  
That is a note on the amount of work put into compilers and not a comment on your skills.

Some things to consider in addition to being platform specific as mentioned by @mgood7123:  
Will this be under an operating system? (very likely if using a virtual machine: Java)  
How much data will need to be processed? (transferring from Java to your library)  
Multithreading? (locks, blocking writes, etc)  
File access?  
Might you get a speedup using resources like shaders in OpenGL? (not sure what is being processed…)  
Will you run your Java code on a “Just In Time” (JIT) capable virtual machine

If you are looking to gain from using single-instruction-multiple-data (SIMD) machine code, consider the following:

```auto
for (int i=0; i<N; i++)
{
  process( array1[i] );
  process( array2[i] );
  process( array3[i] );
}

```

If N is large, it may be more efficient if written as:

```auto
for (int i=0; i<N; i++)
{
  process( array1[i] );
}

for (int j=0; j<N; j++)
{
  process( array2[j] );
}

for (int k=0; k<N; k++)
{
  process( array3[k] );
}

```

The speedup comes from allowing the CPU to work on a single array at a given time.  
That means the array, or parts of it, can be cached.

Similarly:

```auto
// Working with four arrays at once
for (int i=0; i<N; i++)
{
  output[i] = array1[i] * array2[i] + array3[i];
}

```

May be rewritten and _can_ get a speedup

```auto
// Work with two arrays at once
for (int i=0; i<N; i++)
{
  output[i] = array1[i];
}

for (int j=0; j<N; j++)
{
  output[j] *= array2[j];
}

for (int k=0; k<N; k++)
{
  output[k] += array3[k];
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 3, 2020, 4:14pm UTC](https://discourse.processing.org/t/can-i-make-my-own-library-with-my-assembly-code-inside-it-and-how-would-i-do-it/21515/4 "2020-06-03T16:14:51Z")

</div>

See previous related discussion:

> [@Simple Assembly](https://discourse.processing.org/t/simple-assembly/21330):
>
> \*\*Hello There, I just need a simple way to install assembly code in certain routines as long as Processing puts its variables in it’s Stack I am good 2 go I really don’t need a lot of stuff just to read and write to memory and access real-precision variables. I know how to use assembly language, I just need to be able to insert it in to my Application, I do not know how to build my own libraries (yet), but that is what I need a routine to read/write to memory and to do the coprocessor stuff in …

There I asked:

> Do you mean you want to write your own JVM bytecode? Or do you mean calling out to a C library with inline assembly from Processing, e.g. through JNI?

I still want to know that. Do you understand that Java runs as bytecode on JVM?
