News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

call C++ func from asm

Started by qwe522y, November 03, 2011, 07:37:05 AM

Previous topic - Next topic

qwe522y

hello everyone, i have a problem when i want to call function from .lib file
asm code
.686
.model flat, stdcall
asmprint proto
.data
val1 word 123
val2 word 23
val3 word 50
val4 word 25
.code
start:
mov  ax, val1
sub ax, val2
sub ax, val2
sub ax, val3
sub ax, val4
call asmprint
ret
end start


and asmprint.lib code
#include<stdio.h>
void asmprint(){
unsigned int veax;
__asm mov veax, eax
printf("%.8H\n", veax);
}

compiled by vc9.0 as static library .lib
ml /c /coff "test1.asm"  --- ok
then
Link /SUBSYSTEM:CONSOLE test1.obj asmprint.lib --- error
test.obj: LMK2001: unresolved external symbol _asmprint@0

please show me my mistake
thanks.

jj2007

#1
Perhaps your C++ code needs the extern keyword:

extern void __stdcall asmprint();
...
void __stdcall asmprint(){
  unsigned int veax;
  __asm mov veax, eax
  printf("%.8H\n", veax);
}


Just guessing, though - C++ is not my domain of expertise.
[Thanks, ToutEnMasm :U]

ToutEnMasm

Quote
void asmprint(){ ....

Miss the stdcall in the c declare.In c, all prototypes are PROTO C by defaut.

qwe522y

extern void __stdcall asmprint();
void __stdcall asmprint(){
unsigned int veax;
__asm mov veax, eax
printf("%.8H\n", veax);
}

dont work :'(
err message no change
test.obj: LMK2001: unresolved external symbol _asmprint@0

with includelib dont working with same err msg

ToutEnMasm

A proc in a library don't finish by end start
You must compile this (ml -->.obj) then build lib  (lib --> .lib)
Quote
asmprint proto
.data
val1 word 123
val2 word 23
val3 word 50
val4 word 25
.code
asmprint PROC
mov  ax, val1
sub ax, val2
sub ax, val2
sub ax, val3
sub ax, val4
ret
asmprint  ENDP
end

I suggest that you post your code to sAve time.

qwe522y

I'm not sure I understand you(language barrier)
i dont want .lib file in output
i want to link test1.obj as executable with lib written in visual studio in C++
when i commented "call asmprint"  the test.exe working wihout any error
Why he cant find func in the included lib?

ToutEnMasm


qwe522y

no no its not a riddle. all my code I have already given in the first msg. I just want to call my func from .lib via masm

jj2007

Here is what works for me:

"%ProgramFiles%\Microsoft Visual Studio 9.0\VC\bin\cl.exe" /c /Zl /Fa /Fp TestAsmC.c
\Masm32\bin\ml.exe /nologo /c /coff  /Fl /Sn /Fo "TestC"
\masm32\bin\link.exe \masm32\lib\msvcrt.lib TestAsmC.obj "TestC.obj" /OUT:"TestC.exe"


#include <stdio.h>

void __stdcall asmprint();

void __stdcall asmprint(){
unsigned int veax;
__asm mov veax, eax
printf("The number is %u\nCute, isn't it?", veax);
}


.686
.model flat, stdcall

asmprint proto

.data
val1 dword 12445
val2 dword 25
val3 dword 50
val4 dword 25

.code
start:
mov eax, val1 ; 12445
sub eax, val2 ; 12420
sub eax, val3 ; 12370
sub eax, val4 ; 12345
call asmprint
ret
end start

ToutEnMasm


Where is the code of the asmprint function ?
I see only the code of the execute file.No code  for the lib???????


jcfuller

This is a test I just put together to test calling c functions from jwasm code linked with jwlink
Everything works fine using gcc to create the object files and ar to create the library.
James

c sources:
jcf17.c


int jcftest17 (void);

int jcftest17 (void)
{
 int      i;
 i= 17;
 return i;
}


jcf18.c


int jcftest18 (void);

int jcftest18 (void)
{
 int      i;
 i= 18;
 return i;
}


I was using MinGWTDM64 hence the -m32
compiled with:
gcc -c jcf17.c -m32
gcc -c jcf18.c -m32

created the library with:

ar rcs libjcflib.a jcf17.o jcf18.o

This is the jwasm asm source:

                       ;
;+---------------------------+
;|  use CRT (MSVCRT) in ASM  |
;+---------------------------+

.386
.MODEL FLAT, stdcall
option casemap:none
    public _start

WIN32_LEAN_AND_MEAN equ 1
include \jwasm\wininc\include\windows.inc
include \jwasm\wininc\include\stdio.inc


;my c functions
jcftest17 proto c
jcftest18 proto c


;--- CStr(): macro function to simplify defining a string

CStr macro pszText:VARARG
local szText
.const
szText db pszText,0
.code
exitm <offset szText>
endm
.data
 buffer db 20 dup (0)
.data?
Argc dd ?
Argv db ?
CmdL LPSTR ?

.CODE

_start:



Mymain  proc
   invoke jcftest17
   invoke printf,CStr("%d",10),eax
   invoke jcftest18
   invoke printf,CStr("%d",10),eax
ret

Mymain endp
 invoke Mymain
 invoke ExitProcess, NULL

   end _start



And this is my batch file for compiling


@setlocal
@ECHO OFF
@SET LIB=\jwasm\wininc\lib;\jwasm\samples\jcfuller
@SET PATH=C:\jwasm;C:\jwasm\jwlink;c:\jwasm\wininc\lib;c:\jwasm\wininc;%PATH%
@REM add more libraries below
@SET LIBS=LIBRARY kernel32.lib, msvcrt.lib, libjcflib.a

jwasm  %1.asm
jwlink  name %1.exe file %1.obj %LIBS%

endlocal


ToutEnMasm


as explain  upper what is missing is the needed code for the .lib
It must be place here
Quote
   
; #########################################################################

   .386                      ; force 32 bit code
   .model flat, stdcall      ; memory model & calling convention
   option casemap :none      ; case sensitive

   ; -------------------------------------------------------------------
   ;only include files
   ; -------------------------------------------------------------------
   include \masm32\include\windows.inc
   include \masm32\include\kernel32.inc
   ; include \masm32\include\gdi32.inc
   ; include \masm32\include\user32.inc
   ;   include \masm32\include\kernel32.inc
   ; include \masm32\include\masm32.inc
   ;  include macros.txt   

   ; ---------------------------------------------------------
   ;create an include file with the prototypes
   ; ---------------------------------------------------------

   ;
   ;samples of extern def
   ;    EXTERNDEF Copier :PROTO  :DWORD,:DWORD
   ;    EXTERNDEF EtatMemoirePile :MEMORY_BASIC_INFORMATION
   ;    EXTERNDEF PoriginePile:DWORD         
   
.data   

   .code

; #########################################################################

OpenRapport proc

   ;code here

   ret

OpenRapport endp


; #########################################################################

end

Then compile it with ML
Give l this .obj to lib (made .lib) or link the execute code with the .obj


jj2007

Quote from: ToutEnMasm on November 04, 2011, 09:17:54 AM

Where is the code of the asmprint function ?
I see only the code of the execute file.No code  for the lib???????


Luce,
the lib code is everything that follows #include <stdio.h> in my last post above. Believe me it works perfectly :bg

ToutEnMasm


All this is a little confuse.What i search is the code he want to put in a library.
I have understand this.
If he want just to maqde a c call on the asmprint fonction who is in a asm library,it's not the same thing.
Two cases,which is the good ?
How is defined asmprint ?
There is no problem to do the two things,just need to know what he want ???

ToutEnMasm

If the problem is call a c program , named asmprint, the masm prog who call it must be modify
as follow:

Quote
.686
.model flat, stdcall
asmprint proto  C ;<<<<<<<<<<<<<<<<<<<<<<< error,not a stdcall
.data
val1 word 123
val2 word 23
val3 word 50
val4 word 25
.code
start:
mov  ax, val1
sub ax, val2
sub ax, val2
sub ax, val3
sub ax, val4
call asmprint
ret
end start
If i am not in error this look like a proto c
Quote
#include<stdio.h>
void asmprint(){
   unsigned int veax;
   __asm mov veax, eax
   printf("%.8H\n", veax);
}