Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write native code in ASM

I am an avid ASM writer. I am attempting to write native java code in asm. The issue I am experiencing is that parameters passed from Java do not appear to be receive by the asm code. C++ code appears to receive the paramters using the windows 64 bit calling convention, ie rcx,rdx,r8,r9,stack, and initially I presumed it would be the same for asm but it does not seem to be so. I have spent quite some time searching for possible explainations and examples but have yet to find any so I thought I would ask. I will very much appreaciate some assistence in identifying where I am going wrong. Thank you very much. Below are code snipets explaining what I have developed so far...

In NativeInterface.java

public native static String test(int _number);

In Calling.java

String s=NativeInterface.test(123);

In NativeJava.asm

.data
dString     db  "This is my string",0
.code
java_test PROC _iNumber:QWORD
 invoke dialog_showMessageInteger,"ASM CODE","Number=",_iNumber
 mov rax,rv(java_createStringUTF8,JNIenv,ADDR dString)
 ret
java_test ENDP

The dialog opened in asm does not show "123" but instead "1918844240" The above code passes the string to java successfully.

It seems I can successfuly pass parameters to java from asm. At this point I cannot pass parameters to asm from java.

I was hoping the standard windows 64 bit calling convention would be used but doesn't seem that way. I cannot find any documentation anywhere. Can someone please explain what I should expect in asm. Many, many Thanks.

like image 516
Penelope Hubble Avatar asked Nov 29 '25 15:11

Penelope Hubble


1 Answers

Thanks to Peter Cordes who suggested that there might be some hidden parameters that are passed and to disassemble some C++ code for find out for sure.

I didn't need to go that far. I had already been looking at come C++ code and I noticed that all of the native routines had JNIenv and jclass as the first two parameters.

I altered my code to read...

java_test PROC _JNIenv:QWORD,_jclass:QWORD,_iNumber:QWORD

and the value of 123 was indeed given successfully.

It is a guess that the first two paramters are the JNIenv and the calling class but considering it is how the C++ functions are formed I think that is a pretty good guess.

Thanks for your help and support. Regards Penny Take Care

like image 98
Penelope Hubble Avatar answered Dec 02 '25 08:12

Penelope Hubble



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!