Thread: Calling functions written in assembly?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    412

    Calling functions written in assembly?

    Ok, I have a free compiler (Borland C++ 5.02) which requires a $125 assembler program (TASM) to do inline assembly. I refuse to pay this; I'd write my own assembler first.

    I know there are some freeware assemblers (I own a few, come to think of it) -- can I use one of them assemble to an OBJ that I can link to, and call functions from within, a C/C++ program?

    And how do I have to declare the prototypes? extern C?

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    I downloaded a non-warez TASM for free...want me to email it to you?

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    oh wait, I don't have it anymore...sorry.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I use nasm and gcc so it possible to use a different assembler
    and still link the c code. tasm for dos is easy to
    pickup, tasm32 might be harder to find. You also need
    to assemble to your compiler's object format I think. Nasm
    supports a number of these ,-f elf is what I use on linux.

    This should work on linux

    Code:
    /* t.h */
    int f(int a, int b);
    
    /* main.c */
    #include <stdio.h>
    #include "t.h"
    
    int main(void)
    {
        printf("4 + 3 = %d\n", f(4, 3));
    
        return 0;
    }
    
    /* t.asm */
    section .text
    
    global f
    f:
    	push ebp
    	mov ebp, esp
    
    	mov eax, [ebp+8]     ; eax = a
    	mov ecx, [ebp+12]   ; ecx = b
    
    	add eax, ecx           ; return in eax
    .exit:
    	leave
    	ret

  5. #5
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    t.h should have the extern "c" stuft, that way you
    can link with c++ .

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Thanks for the pointers. MASM is still free (it's bundled with some DLs from Microsoft) so I've gotten that, and (eventually) gotten it working.

    I just tried out the sample code of yours, and made it work under the compiler (I compiled the rest as C++ so I needed to declare the function as a "C" called function to disable name mangling) -- everything seems all well and good! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack issues when calling a COM library
    By notnot in forum C Programming
    Replies: 3
    Last Post: 06-01-2009, 02:12 AM
  2. Segmentation Fault when Calling Functions
    By thetinman in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 12:04 PM
  3. Calling Class Functions in ASM?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 10-14-2003, 12:48 AM
  4. calling fastcall functions
    By Hankyaku in forum C++ Programming
    Replies: 6
    Last Post: 10-11-2003, 09:32 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM