PyvesB/javassembly

FASM. Some error

Closed this issue · 4 comments

Sorry new to assembly in all shapes.
I was actually doing the same thing as you are but relying on Jna natives, then just went for a simple flip to your method when I realized how short you made it.
Your asm arraysum is giving me the error only on line
global Java_JNIArraySum_computeNativeArraySum
It's probably a FASM thing? There's usually some declaring at the top, like:

format PE GUI 4.0
entry start
include 'win32a.inc'

I added this and it still brought be to the above line. Is this just a FASM thing? Sorry I know in the read me you didn't mention FASM

Hello @Motley90 ! 👋🏻

The idea is to make the mangled method name visible from other translation units, so that the assembly method can be called from your Java program. If I recall correctly, FASM uses the public keyword instead of global. Try that instead and let me know if it helps.

Thanks for the reply. I did the switch and it's telling me it's a illegal instruction.
on: public Java_JNIArraySum_computeNativeArraySum
I also specified the compiler at the top

The actual code I have available from your example. I removed the added compiler and still the same results

format PE GUI 4.0 DLL
entry DllEntryPoint

include 'win32a.inc'

public Java_JNIArraySum_computeNativeArraySum

section .data

; JNI function indexes can be found in the docs:
; http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html
GetIntArrayElements: equ 187 * 8
ReleaseIntArrayElements: equ 195 * 8

Java_JNIArraySum_computeNativeArraySum:

; retrieve sum result
ret

I had a user named 'Grom PE' help me by telling me what's wrong. He saying it needs a fasm rewrite
Also
"public is for assembling object files, for DLL you need export. See DLL example.
Also, variables defined with equ don't end up in the assembling result, they are available at the stage of assembly. So your data section is empty and not needed."

format PE GUI 4.0 DLL
entry Java_JNIArraySum_computeNativeArraySum

include 'win32a.inc'

; JNI function indexes can be found in the docs:
; http://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html
GetIntArrayElements:       my equ 187 * 8
ReleaseIntArrayElements:   my equ 195 * 8

section '.text'

Java_JNIArraySum_computeNativeArraySum:

; ERROR the rest of the file.

push rdx                  ; save Java array pointer for later use
push rdi                  ; save JNIEnv pointer for later use
push rcx                  ; save array length for later use
mov rsi, rdx              ; set array parameter for GetIntArrayElements
mov rax, [rdi]            ; get location of JNI function table
xor edx, edx              ; set isCopy parameter to false for GetIntArrayElements
call [rax + GetIntArrayElements]
pop rcx                   ; retrieve array length
lea rcx, [rax + 4 * rcx]  ; compute loop end address (after last array element)
mov rdx, rax              ; set elems parameter for future ReleaseIntArrayElements
xor r8d, r8d              ; initialise sum accumulator
add_element:
    movsxd r9, dword [rax]; get current element
    add r8, r9            ; add to sum
    add rax, 4            ; move array pointer to next element
    cmp rax, rcx          ; has all array been processed?
    jne add_element
pop rdi                   ; retrieve JNIEnv
pop rsi                   ; retrieve Java array pointer
push r8                   ; store sum result
mov rax, [rdi]            ; get location of JNI function table
call [rax + ReleaseIntArrayElements]
pop rax                   ; retrieve sum result
ret          

So I guess this answers the rest of my issue. I suppose I could learn assembly and try writing something of my own. If that's something you wouldn't have a problem with I would appreciate it if not no biggie. No expectations.
I suppose you could close this I appreciate the help.

public is for assembling object files, for DLL you need export

Indeed, the example in this repository does work with object files, I hadn't clocked you were trying to do something different.

So I guess this answers the rest of my issue. I suppose I could learn assembly and try writing something of my own. If that's something you wouldn't have a problem with I would appreciate it if not no biggie. No expectations.
I suppose you could close this I appreciate the help.

Sure, feel free to write whatever you fancy, don't let me stop you. You may also want to check out this other repository of mine for inspiration: https://github.com/PyvesB/asm-game-of-life 😉