Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling an assembly procedure from C

Status
Not open for further replies.

bigbeebrian

Programmer
Jan 15, 2001
9
0
0
US
Let's say we have a C-program. In that program we call a function like convertTo(inBuffer, size, outBuffer).
The prototype looks like this:

extern dword convertTo(char*inBuffer, dword size, char* outBuffer);

Question?: how would I get at the contents of: inBuffer,size and outBuffer . I presume these things are placed on the stack before the function is called.

any comments would be helpful.
 
First you need to know whether you are using STDCALL, PASCAL, or C calling convention. Yes, the calling procedure is C. No it doesn't mean the calling convention is C already. By default for C, it USED to be the C convention, but I think the newer, Windows-based C compilers now default to STDCALL. And you can tell your compiler to use PASCAL convention for a routine, even if both the caller and the called routines are in C.

If you have MASM, you can cheat and do something like this:
Code:
.MODEL FLAT,STDCALL
;Or whatever model and calling convention that
;you made your compiler use

;note: data addresses MIGHT be WORDs only if you are
;in 16-bit addressing mode, and small or compact model
convertTo PROTO :DWORD,:DWORD,:DWORD

convertTo proc inBuffer:DWORD, size:DWORD, outBuffer:DWORD
    ;we are going to use esi and edi, so save them
    ;(required for ALL calling conventions)
    push  esi
    push  edi
    ;get the first byte in inBuffer
    mov   esi,inBuffer
    mov   al,byte ptr [esi]
    ;put it in last byte in outBuffer
    mov   ecx,size
    mov   edi,outBuffer
    add   edi,ecx
    sub   edi
    mov   byte ptr [edi],al
    pop   edi
    pop   esi
    ret
convertTo endp

Right? "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
From: bigbeebrian

I want to pull the data off of the stack.
for the program above where the C function prototype is:

ConvertToASCII(char* inputBuffer, dword size, char* outputBuffer)

I understand when the C program calls this function the arguments are pushed on the stack from right to left.
My question is how do I pull off the offset of each argument?
Thanks
 
You don't. You're not supposed to pull off any data from the stack in the C calling convention. If you're using PASCAL and STDCALL calling standard, you're supposed to, but only after you finish the whole function.

Further -
What is your target operating system? Is it DOS? Windows32? Windows16? It does matter, because you need to know the size of the pointer. If DOS, what memory model do you use? Do you need to change the segment pointers (for medium and large model)?

I suggest that you try to use MASM's pseudo-high-level extensions as I showed above, because without them you will have a bit of hard time trying to interface to high-level.

I do hope you know what 'memory model' is. "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top