Thread: Calling asm in C

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    Angry 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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's all highly dependent on your compiler.
    Say which one you have.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    An assembly function looks just like any other normal C function to the C compiler. It will not know the difference.

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    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.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    Thanks everyone.....I am using Turbo C, MASM and LINK

  6. #6
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    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.
    Last edited by valis; 04-26-2006 at 11:43 AM.

  7. #7
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    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
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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)
    Last edited by Ancient Dragon; 04-26-2006 at 12:40 PM.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    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 ….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 ….good that here pple share what they know.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help calling function is asm
    By brietje698 in forum C++ Programming
    Replies: 24
    Last Post: 12-06-2007, 04:48 PM
  2. calling function in asm
    By brietje698 in forum C++ Programming
    Replies: 10
    Last Post: 11-29-2007, 02:07 PM
  3. Calling Class Functions in ASM?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 10-14-2003, 12:48 AM
  4. MSVC++ :: Calling 'cout' w/ ASM
    By SyntaxBubble in forum Game Programming
    Replies: 1
    Last Post: 06-19-2003, 12:54 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM