-
Calling asm in C
I have this doubt.
I need to do a simple division program. The main program must be in C and the subroutines must be in assembly.
I have to get the dividend and divisor through key board using a C program (eg.25/5). ….I know how to do it.
The division should be done using an assembly subroutine…..I know that part too
Now, my doubt is how to pass this 25 and 5 from C into MASM? And then again return the result back to C main program?
Thanks in advance
Jay
-
It's all highly dependent on your compiler.
Say which one you have.
-
An assembly function looks just like any other normal C function to the C compiler. It will not know the difference.
-
Judging from your ........ty assembler I'm assuming you are using cl as your compiler?
If that's the case prepend an underscore (_) to your function name. So AsmDiv should be _AsmDiv, also know that unless you explicitly specify, C will push the arguments passed in reverse order and (if I recall) everything is passed as a dword.
AsmDiv(5, 25): arg1 (esp + 8) = 25, arg2 = 5
Remember to preserve the callers stack and all that jazz.
edit:
cl always expects the return to be in eax.
-
Thanks everyone.....I am using Turbo C, MASM and LINK
-
hmm, unfortunately I know literally nothing of that compiler, you might try debugging a normal function and seeing what all it preserves, how everything is passed (I assume it's still on the stack in reverse order), and where it puts the result before the ret instruction.
edit:
Just out of curiosity, where did you hear about turbo C? I don't often hear it spoken of.
It bothered me I didn't know so I went and looked it up, it *appears* turbo C expects the result in eax, make sure your assembler preserves case, it does prepend an _ to function names, preserve esi and edi (not sure if anything else, if it crashes you'll know), it passes things in reverse order. Just compile your assembly into an object, declare it extern in your C file and then link it with the rest of your stuff.
-
I don't think this has anything to do with the compiler. It has to do with the calling convention which is c-calling convention.
I googled for c calling convention and the following page seems to explain pretty well:
http://www.unixwiz.net/techtips/win32-callconv.html
-
>preserve esi and edi (not sure if anything else, if it crashes you'll know)
ebx, ebp, esi, and edi. The segment registers are also on that list, but not nearly as important. It's generally poor practice to rely on failures to tell you what you need to know. Better to do the research and be sure. ;)
>where did you hear about turbo C? I don't often hear it spoken of.
Hang around and you'll find that a surprising number of beginners find their way past all of the other suitable compilers and right to Turbo C. :rolleyes:
-
don't know about turbo c, but microsoft compilers general purpose registers (eax, ebx,ecx and edx) need not be saved, all other registers must be preserved and restored before the assembly function exits. If you have the compiler generate assembly code out of a small C program you will see how it works.
16-bit integer value (such as short) is returned in ax, 32-bit (such as long int) in dx and ax (in 16-bit compilers like turbo C) or eax in 32-bit compilers. And you should not use 32-bit assembly code with a 16-bit compiler such as Turbo C -- the compiler may not be able to link object code that contain 32-bit register references. (The compiler is too ancient)
-
Ok....thanks guys....still I could not pass those values from C , its ok.....I will 'embed' my asm code within C, for the time being......yep, my compiler is ancient and Borland itself calls it as 'Antique'.....but since this is part of my curriculum (16 bit C compiler and 8086 microprocessor- to learn the basics), I don’t have any other choice…if at all I use any 32 bit compiler, I need to stick with 16 bit code. Anyway its not boring :D ….only problem is when stuck somewhere, need to run around to get the right information….many of pple don’t know or if they know, they don’t want to share knowledge :mad: ….good that here pple share what they know. :)