×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

calling an assembly procedure from C

calling an assembly procedure from C

calling an assembly procedure from C

(OP)
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.

RE: calling an assembly procedure from C

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:

.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."

RE: calling an assembly procedure from C

(OP)
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

RE: calling an assembly procedure from C

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."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close