News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Passing 2d arrays and 1d arrays from c++ to asm

Started by pawkondr, January 13, 2015, 03:18:19 AM

Previous topic - Next topic

jj2007

Quote from: jj2007 on January 16, 2015, 02:11:37 AMYou call MyProc exactly as you would call your static C++ library.

Post the VS "project" where you CALL your static C++ library, complete with the arrays and the output that your proggie produces. There must be something like
  MyProc(bunch, of, paras);

That is what we need to see. And btw assembler is a lot easier and simpler than C++.

dedndave

i would imagine that C procedures default to C calling convention
we often use StdCall in ASM - and most windows API's also use this convention

so - see how they are defined and how they are prototyped - you probably have a mismatch somewhere

pawkondr

Hello again, I have uploaded my whole VS2013 solution. It was much larger than 0,5 MB so, I put it on Google Drive. Here is the link:
https://drive.google.com/file/d/0B0kbQ3Gi5BRTdDgtQXJrUGw3b0E/view?usp=sharing

jj2007

163,154,560 bytes for a hello world proggie? I love C++ :greensml:

Besides, my version of VS doesn't like it, see below, and the only interesting part is this:
hDll = LoadLibrary("FibAsmLib");

if (hDll != NULL){

myAsmProc = (DLLFunc)GetProcAddress(hDll, "MyProc");
if (myAsmProc != NULL){
result = myAsmProc(arrayA, arrayB, arrayAlfa, arrayBeta, rowA);
}


That is code for loading a DLL, as mentioned twice in this thread, and it is only slightly different from linking to a static lib. Once you are able to link to a static C++ library, and using myAsmProc(arrayA, arrayB, arrayAlfa, arrayBeta, rowA);, show us that part only.

pawkondr

Ok, I will do that.
BTW. Try to open .sln file in notepad and change this lines:
Microsoft Visual Studio Solution File, Format Version 12.00
Visual Studio 2013
To:
Microsoft Visual Studio Solution File, Format Version 10.00
Visual Studio 2010

jj2007

#20
Quote from: pawkondr on January 17, 2015, 10:48:59 AM
Ok, I will do that.
BTW. Try to open .sln file in notepad and change this lines:
Microsoft Visual Studio Solution File, Format Version 12.00
Visual Studio 2013
To:
Microsoft Visual Studio Solution File, Format Version 10.00
Visual Studio 2010

This is a hilarious hack, but the converter swallows it :t
However, VS then complains that the toolset is not valid. OK, Google is our friend, and many many people are fighting with Microsoft, so that is also solved.

The good news: your project builds without errors, and it even runs!!!

Until it hits this simple line:
   hDll = LoadLibrary("FibAsmLib");
And then, Windows 7-64 complains that MSVCR120.dll is not present on the computer.

Seriously, what else do you expect from a hello world proggie that needs 163,154,560 bytes of disk space?

Redmond!! REEEEEDDDDDMMOOOOOOOND!!!!

QuoteThere was once a young man who, in his youth, professed his desire to become a great writer. When asked to define "great" he said, "I want to write stuff that the whole world will read, stuff that people will react to on a truly emotional level. Stuff that will make them scream, cry, and howl in pain and anger!"

He now works for Microsoft, writing error messages.

QuoteA helicopter was flying around above Seattle when an electrical malfunction disabled all of the aircraft's electronic navigation and communications equipment. Due to the clouds and haze, the pilot could not determine the helicopter's position and course to fly to the airport. The pilot saw a tall building, flew toward it, circled, drew a handwritten sign, and held it in the helicopter's window. The pilot's sign said "WHERE AM I?" in large letters. People in the tall building quickly responded to the aircraft, drew a large sign, and held it in a building window. Their sign read "YOU ARE IN A HELICOPTER." The pilot smiled, waved, looked at his map, determined the course to steer to SEATAC airport, and landed safely. After they were on the ground, the co-pilot asked the pilot how the "YOU ARE IN A HELICOPTER" sign helped determine their position. The pilot responded "I knew that had to be the Microsoft building because they gave me a technically correct, but completely useless answer."

Back to work: I attach a version of your Main.cpp that builds and runs because it uses a static lib. Main points:

#define asmLibDll 0
#if asmLibDll==0
#pragma comment(lib, "testJJ.lib") // the static library
extern "C" int __stdcall myAsmProc(double** A, double* B, double** alfa, double* beta, int n);
#endif


#if asmLibDll
DLLFunc myAsmProc;
#endif


//ASM library
#if asmLibDll==0
//_asm int 3;
cout << "\n#elements=" << rowA;
result = myAsmProc(arrayA, arrayB, arrayAlfa, arrayBeta, rowA);
//_asm int 3;
//_asm mov eax, result;
cout << "\nResult from library: " << result;
#else
cout << "\nloading FibAsmLib\n";
hDll = LoadLibrary("FibAsmLib");
cout << "loaded\n";


The asm part works in principle, i.e. it does return with eax==111111111, and the stack is balanced, but it doesn't do anything useful:
myAsmProc proc uses ecx edx esi edi ebx pA, B, pAlfa, beta, n ; double** A, double* B, double** alfa, double* beta, int n)
  mov esi, pA
  mov eax, 111111111
  ret


Main.cpp, library and asm source are attached. Hope it helps ;-)

pawkondr

That static lib works perfectly, but I read once again requirements of my project and I must use dynamic library (dll) compiled in visual studio, so I returned to your code(that one from reply #9), I put it to my fibAsm.asm file inside of FibAsmLib project, I compiled it then I corrected 5 milion errors and in the end VS built it, but when i try to call that dll it throws my fauvorite error:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.  This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
This error sais: "Something is wrong find it and correct it because we dont give an shit"
Do you have an Idea what's causing it?
I am starting to hate everything connected with this project, so much that I will start to write error messages for MS  :greensml:

jj2007

Quote from: pawkondr on January 18, 2015, 12:25:21 AMThat static lib works perfectly, but ... a function declared with one calling convention with a function pointer declared with a different calling convention.

The problem is your declaration of DllFunc, probably hidden somewhere in the headers. I bet it is ccall.
So, attached the original DLL as CCALL, with source in RTF format. It should work, but test yourself - my VS throws that stupid error msvcrt120.dll not found. For a LoadLibrary call, good grief :eusa_boohoo:

pawkondr

How to tell compiler that the call convenction is ccall? I had _stdcall it didn't work, i deleted _stdcall, also didn't work. I tried to add something like this _ccall. Errors.

typedef int(_stdcall *DLLFunc)(double**, double*, double**, double*, int);

Or maybe is not that line or sth?

TWell

Compiling that dll in C someone can have easily exported names as excepted.

In cpp mangled names exists in dll like:
?fnFibAsmLib@@YAHXZ
?fnFibAsmLib@@3HA

qWord

pawkondr,

use the following declaration in the header file FibAsmLib.h:
extern "C" FIBASMLIB_API int __cdecl MyProc(double** A, double* B, double** alfa, double* beta, int n);

The ASM file is:
.686
.xmm
.model flat, C

PREAL8 typedef ptr REAL8

.code
MyProc proc C A: ptr PREAL8, B: ptr REAL8, alfa: ptr PREAL8, beta: ptr REAL8, n: SDWORD
; ...
mov eax, 5
ret

MyProc endp

end


then
- add the project directory FibAsmLib to the include paths of project Przyklad
- add library to linker input: "$(OutDir)FibAsmlib.lib"
- do not forget to set configuration == win32.
- include FibAsmLib.h into main.cpp and call MyProc directly (means: do not use GetProcAddress)
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

Quote from: qWord on January 18, 2015, 03:33:10 AMcall MyProc directly (means: do not use GetProcAddress)
Quote from: pawkondr on January 18, 2015, 12:25:21 AM
That static lib works perfectly, but I read once again requirements of my project and I must use dynamic library (dll)

Solution is to use somealgo proc C in assembler, and something like this in C++ (tested, it works):
if (hDll != NULL){
myAsmProc = (DLLFunc)GetProcAddress(hDll, "myAsmAlgo");
cout << "GetProcAddress = " << myAsmProc << "\n";
if (myAsmProc != NULL)
result = myAsmProc(arrayA, arrayB, arrayAlfa, arrayBeta, rowA);
else
result = -1;
cout << "Result from dll: " << result;
cout << "\nFreeLibrary result is " << FreeLibrary(hDll);
}

qWord

Quote from: jj2007 on January 18, 2015, 04:29:06 AM
Quote from: qWord on January 18, 2015, 03:33:10 AMcall MyProc directly (means: do not use GetProcAddress)
Quote from: pawkondr on January 18, 2015, 12:25:21 AM
That static lib works perfectly, but I read once again requirements of my project and I must use dynamic library (dll)

Solution is to use somealgo proc C in assembler.
?
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

See above - teacher wants him to use LoadLibrary 8)

Attention: I lost one hour chasing a "bug" that consisted simply in believing current directory is where main.cpp sits. Nope, VS expects the DLL in the debug and/or release folders.

qWord

Quote from: jj2007 on January 18, 2015, 04:44:29 AM
See above - teacher wants him to use LoadLibrary 8)
then he just has to use something like:
   typedef int(__cdecl * DLLFunc)(double** A, double* B, double** alfa, double* beta, int n);
...
   hDll = LoadLibrary("FibAsmLib");

   if (hDll != NULL){

      DLLFunc myAsmProc;
     
      myAsmProc = (DLLFunc)GetProcAddress(hDll, "MyProc");

      if (myAsmProc != NULL){
         result = myAsmProc(arrayA, arrayB, arrayAlfa, arrayBeta, rowA);
      }
   }
MREAL macros - when you need floating point arithmetic while assembling!