Like Tree2Likes
  • 1 Post By Subsonics
  • 1 Post By phantomotap

How to link and compile a .c and .asm file?

This is a discussion on How to link and compile a .c and .asm file? within the C Programming forums, part of the General Programming Boards category; Hello, I have the following files: mainlinemem.c... Code: #include <stdio.h> int gcdmem(int a,int b); int main() { int result; int ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    11

    How to link and compile a .c and .asm file?

    Hello,

    I have the following files:

    mainlinemem.c...

    Code:
    #include <stdio.h>
    
    int gcdmem(int a,int b);
    
    int main()
    {
        int result;
        int a;
        int b;
    
        a = 46;
        b = 90;
        printf("%d and %d have a gcd of %d\n",a,b,gcdmem(a,b));
    
        a = 9863;
        b = 41272;
        printf("%d and %d have a gcd of %d\n",a,b,gcdmem(a,b));
    }
    and:

    gcdmem.asm...

    Code:
    segment .data
    
    segment .bss
    x:    resd  1
    y:    resd  1
    segment .text
        global gcdmem
    gcdmem:
        push ebp
        mov  ebp,esp
        mov  eax,[ebp+8]        ; x
        mov  [x],eax
        mov  eax,[ebp+12]       ; y
        mov  [y],eax
    looptop:
        mov  eax,[x]
        cmp  eax,0              ; if (x == 0) we're done
        je   goback
        mov  ebx,[y]
        cmp  eax,ebx            ; make certain x is the larger number
        jge  modulo
        mov  [x],ebx
        mov  [y],eax
    modulo:
        mov  ebx,[y]
        mov  eax,[x]
        cdq                    ; set up for division
        idiv ebx               ; divide edxeax by ebx
        mov  [x],edx           ; the remainder is in edx
        jmp  looptop
    
    goback:
        mov  eax,[y]           ; return y
        mov  esp,ebp
        pop  ebp
        ret
    Can anyone show me how to compile these files so I can run it?

    I am on Windows XP and have mingw installed, and also NASM and alink.exe.

    I've used google to find the answer, but every solution I tried was unsuccessful.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Use GCC, it makes C/ASM integration incredibly easy:
    Code:
    gcc mainlinemem.c gcdmem.c
    You could obviously add more command-line options, but that's really all it takes.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I did this a while back (did not target XP though), you need to specify the object filetype to nasm and compile the C file to an object file (-c for gcc), then you can link them.

    Code:
    nasm -f <object fileformat> -o gcdmem.o gcdmem.asm
    gcc -c mainlinemem.c
    gcc mainlinemem.o gcdmem.o
    You may need to add an entry point argument to nasm as well.
    ak22 likes this.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,178
    I am on Windows XP and have mingw installed, and also NASM and alink.exe.
    You should pretty much always use the compiler to do linking.

    Here it doesn't matter so much because both files are simple and only C and assembler are involved.

    If you add C++, for example, at a later date things can extremely messy trying to use the linker directly.

    Soma
    ak22 likes this.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Quote Originally Posted by Subsonics View Post
    I did this a while back (did not target XP though), you need to specify the object filetype to nasm and compile the C file to an object file (-c for gcc), then you can link them.

    Code:
    nasm -f <object fileformat> -o gcdmem.o gcdmem.asm
    gcc -c mainlinemem.c
    gcc mainlinemem.o gcdmem.o
    You may need to add an entry point argument to nasm as well.
    Thank you, it works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC Compile Problem (link)
    By Timbuktu in forum C Programming
    Replies: 5
    Last Post: 12-07-2011, 05:02 PM
  2. How to compile and link koolplot?
    By angela3017 in forum Linux Programming
    Replies: 4
    Last Post: 02-18-2011, 12:00 AM
  3. Compile and Link tutorials
    By tapti in forum C Programming
    Replies: 12
    Last Post: 08-04-2009, 03:23 AM
  4. Compile/Link errors...
    By Xenent in forum Game Programming
    Replies: 2
    Last Post: 10-23-2002, 05:39 PM
  5. Compile/Link errors...
    By Xenent in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 04:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21