Hi,

just wondered about that line:

wsprintfA PROTO C :DWORD,:VARARG

AFAIK wsprintf is a C function, but why can i use it in masm? why can i declare a prototype, or why do i have to? what does VARARG mean? AFAIK (again), both parameters are pointers to strings......

nop

edit: just answered the vararg question for myself: there is an ulimited (?) number of parameters possible....

nop
Posted on 2002-01-14 14:19:44 by NOP-erator
Hi NOP!

I think vararg means "variable arguments" (or something like that) - not a constant number of parameters.

About the wsprint question, Borland Delphi has exactly the same function too, so I believe it's just a common API.

/Delight
Posted on 2002-01-14 14:55:49 by Delight
The C function is called sprintf. It can handle floating point, which wsprintf cannot.

If you have your default calling convention set to STDCALL, then you need the C modifier to properly deallocate the arguments after the function returns.
Posted on 2002-01-14 15:56:33 by tank
REcently I found how to add Masm32 asm procedures to C programs,
Can you do the opposite

Add C function asm procedures to Masm32 programs,
I guess I'll have to try it
Has any one?

I wonder if you can use both C functions and Masm32 functions together in the same procedure?

Has anyone succeeded, and they did it be neat to see how they did it!
Posted on 2002-01-15 22:09:05 by andy981
wsprintf is like the C function sprintf, except that wsprintf doesnt support floating poing values :(
Posted on 2002-01-16 08:49:08 by dxantos
I'm totally confused now. why can't i use sprintf in my asm projects, when i can use wsprintf? is wsprintf a normal api? but why this C after PROTO?

NOP
Posted on 2002-01-16 13:25:35 by NOP-erator
sprintf is from the C runtime library (LIBC, CRT etc), while wsprintf is the (partial) conversion of wsprintf as a Windows API.

sprintf is in msvcrt.dll though...

The C is for cdecl calling convention... which is the one C uses... you have PASCAL too but it is very like stdcall...
in cdecl, it is to the caller to clean the stack... and not the proc itself.

Kind regards,
Posted on 2002-01-16 13:30:50 by JCP

REcently I found how to add Masm32 asm procedures to C programs,
Can you do the opposite

Add C function asm procedures to Masm32 programs,
I guess I'll have to try it
Has any one?

I wonder if you can use both C functions and Masm32 functions together in the same procedure?

Has anyone succeeded, and they did it be neat to see how they did it!


Andy,

If you do own VC++, you can create a .lib file from a C or C++ function (lib file project in New project, I don't remember the names exactly). Just define it as STDCALL (important), compile and then you have your .lib.
After, just create a proto to it (or use hutch's l2inc), include them both in your asm file like you would do with classical apis libraries...

But use that carefully with well written C or C++ functions... else, what is the point to write in assembly ? :tongue:
Posted on 2002-01-16 13:39:24 by JCP
You can call either way, and in any combination.

You must pay attention to these things:

1) The register saving convention. The caller must not assume that EAX, ECX, or EDI will be preserved. The callee must ensure that EBX, ESI, and EDI (also EBP if you're using it as an ordinary register) are preserved.

Note: If an ASM routine is called only by ASM routines, you can ignore this convention. However, when you are calling an API, assume you are calling a C function.

2) The naming convention (names seen by the linker). extern "C" is the simplest way on the C++ side.

3) The argument passing convention. The caller and callee must agree. Each function defines which convention will be used.

The two major conventions in Win32 are cdecl (default C/C++ convention) and stdcall (API convention). If you have VC, you can experiment with other conventions (e.g., fastcall).

4) Initialization code. Some standard C functions require extra code that's hidden in a .h, .obj (Borland), or .lib file.

Examples:
The old stdio code required the definition of a global _iostruct array, initialized to show stdin, stdout, and stderr as already open. If the current stdio code uses the C++ iostream library, some global objects need to be initialized on startup.

Some string functions must handle "locales". This generally requires initializing some tables on startup.
Posted on 2002-01-16 14:53:59 by tank
Originally posted by Readiosys


Andy,

If you do own VC++, you can create a .lib file from a C or C++ function (lib file project in New project, I don't remember the names exactly). Just define it as STDCALL (important), compile and then you have your .lib.


I tried what you said, I could'nt find what I think would do it,
I expiramented with making a DLL though,
What kind of project should I start?

Andy :)
Posted on 2002-01-16 18:41:39 by andy981
andy, if you want to call C from asm... first, make sure the symbol
names are correct. As tank said, this is done in C(++) by doing,
for example:


extern "C" void functionFromC(unsigned int param)
{
// do stuff here
}

This will ensure that a C++ compiler doesn't add all the vairous
stuff to the symbol name that C++ compilers like. A pure C
compiler might b1ch about the "C".

Next thing is you want to set up a proto in your asm source:


functionFromC proto C :dword


Assemble, Compile, Link. And that *should* work. If you use C++
stuff or call libc functions, additional stuff might/will be needed, and
some of that is definitely not beginner stuff :).
Posted on 2002-01-16 18:50:53 by f0dder
Maybe this will lead to some insight

http://www.cs.nyu.edu/exact/core/download/windowsInstr.html

It some kind of guide on making a library in C
to get familiar with the Compiler
Posted on 2002-01-16 19:08:00 by andy981



Andy,

If you do own VC++, you can create a .lib file from a C or C++ function (lib file project in New project, I don't remember the names exactly). Just define it as STDCALL (important), compile and then you have your .lib.
After, just create a proto to it (or use hutch's l2inc), include them both in your asm file like you would do with classical apis libraries...

But use that carefully with well written C or C++ functions... else, what is the point to write in assembly ? :tongue:


I tried what you said, I made the in Static Win32 Lib

used this to make my LIB

#include <stdio.h>
void __stdcall Simple()
{
printf("\n Look I work!\n");
}

I added this to User32.inc

PROTO Simple

and have these include's and lib's

include \masm32\include\user32.inc
includelib \masm32\lib\Simple.lib

and in the code

invoke Simple

and I get this error

Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/cgidemo4.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Posted on 2002-01-16 21:33:02 by andy981
In fact, the problem here seems to be that you use C runtimes functions...
When I was talking about C->lib convertion it was only "pure C code" or "C code with Win32 APIs"...

You can use the libc too, but it is somewhat cumbersome...
Posted on 2002-01-17 01:17:41 by JCP

In fact, the problem here seems to be that you use C runtimes functions...
When I was talking about C->lib convertion it was only "pure C code" or "C code with Win32 APIs"...

You can use the libc too, but it is somewhat cumbersome...


Yes I hope I can figure out how use C runtimes functions in ASM,
but maybe I can use API calls,
Anyway I'm going to let you see this excellent CGI Form program in C, it puts all the form entries in a structure called Entries with
the char arrays name and value as it's members
Make's CGI input a breeze,

I'll let you download it, and you'll see, It's kind of junky the way I have it,
And what I'd convert into a lib is all the functions in util.c and I guess one part in the main program maybe

check it out, you'll like it if you've never seen it before

Andy!
Posted on 2002-01-17 19:17:44 by andy981
When you nothing in the functio it works

#include <stdio.h>
void Simple();

void Simple(){


}

But I'm trying to figure how to link it all up to use

#include <stdio.h>
void Simple();

void Simple(){
printf("Test O'Kay");
}
Posted on 2002-01-18 12:45:43 by andy981
1) make sure libc is linked in.
2) make sure the entrypoint is the libc entrypoint, not yours.
Posted on 2002-01-18 13:01:49 by f0dder
What I'm doing
My include file looks like this
in A.inc

Simple PROTO C

My program, (just something gutted to figure this problem out)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
.386
.model flat, StdCall

option casemap :none ; case sensitive



include \masm32\include\windows.inc
include \masm32\include\A.inc

include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc

includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
includelib \masm32\lib\Simple.lib


Main PROTO



.data
lf db 13,10,0
content db "Content-type: text/plain",13,10,13,10,0



nums dd 99,20,62,0,52,31,88,71,15,7,3
len dd 40
br db "<br>",0

.code

start:
invoke Main
invoke ExitProcess,0



Main proc


invoke StdOut,ADDR content
invoke Simple ;<--If succeds it'll print
ret

Main endp


end start



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


1) make sure libc is linked in.

I don't really understand, I did add LIBC in Masm32 LIB it failed


2) make sure the entrypoint is the libc entrypoint, not yours.

I don't really understand,


Oh , I'm trying to assemble the ASM file in Masm32

And the make the lib in Visual C++
Posted on 2002-01-18 13:12:42 by andy981
I have the LIB file
going to
D:\masm32\lib\

instead of the default Debug like Visual C++ does
Posted on 2002-01-18 13:18:54 by andy981


1) make sure libc is linked in.

I don't really understand, I did add LIBC in Masm32 LIB it failed


2) make sure the entrypoint is the libc entrypoint, not yours.

I don't really understand,


Oh , I'm trying to assemble the ASM file in Masm32

And the make the lib in Visual C++
#include




#include <stdio.h>
void __stdcall Simple();

void __stdcall Simple(int d){
int *num;
d = (d*10)/2;
}

I got it to print 25 if d is 5

in my ASM program

but I like to see if I could figure out

1) make sure libc is linked in.

2) make sure the entrypoint is the libc entrypoint, not yours.

And I guess have the C function be


#include <stdio.h>
void Simple();

void Simple(){
printf("I figured how to run CLIB runtime functions written
in C in my ASM code assembled on Masm32 , Yah!");
}
Posted on 2002-01-18 20:39:05 by andy981