Thread: Mixing C and Assembly

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Mixing C and Assembly

    I'm using NASM and GCC (Assembly and C) compilers and I'm trying to make a function ad.asm in assembly and link it with my main.c by using the following commands:
    nasm -f coff ad.asm
    gcc -Wall -o main.exe main.c ad.o

    and I get the following error message:
    D:\Hwk3\Task2>gcc -Wall -o main.exe main.o ad.o
    main.o(.text+0x63):main.c: undefined reference to `ad'
    collect2: ld returned 1 exit status

    Here's my code, can anyone tell me what I'm doing wrong?
    main.c
    Code:
    #include <stdio.h>
    
    int ad(int,int);
    
    int main(int argv, char *argc[])
    {
    	int a,b,total;
    	printf("Enter two integers");
    	scanf("%i %i", &a, &b);
    	total=ad(a,b);
    	printf("The sum of %i + %i = %i",a,b,total);
    	return 0;
    }
    ad.asm
    Code:
    ad:
    	push ebp
    	mov ebp,esp
    	mov eax,[ebp+8]
    	mov ebx,[ebp+12]
    	add eax,ebx
    	mov esp,ebp
    	pop ebp
    	ret

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Your compiler is looking for int ad() within the file.


    extern "C" int ad(int,int)


    should clear the error up.


    The body of your function lies outside of the current module therefore you must declare it with extern.

  3. #3
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Do I need that "C" as well? Because I tried both and it didn't work.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Implement ad() in the C program as C code, then compile it with
    gcc -S prog.c
    Examine prog.s to find out exactly what ad() is called - it may have a leading underscore for example.

    Then adjust the name in your asm file.
    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.

  5. #5
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Okay, I was wrong, that didn't fix anything. Here's my code now:

    _ad.asm
    Code:
    ;add.asm - function of main.c adds two given variables and returns answer
    ;Andy Gearhart
    ;Nov 13, 2003
    _ad:	push ebp
    	mov ebp,esp
    	mov eax,[ebp+8]
    	mov ebx,[ebp+12]
    	add eax,ebx
    	mov esp,ebp
    	pop ebp
    	ret
    main.c
    Code:
    *
    Program Name: main.c 
    Description: Adds 2 ints using an Assembly function
    Programmer: Andy Gearhart
    */
    #include <stdio.h>
     int ad(int,int);
    
    int main(int argv, char *argc[])
    {
    	int a,b,total;
    	printf("Enter two integers");
    	scanf("%i %i", &a, &b);
    	total=ad(a,b);
    	printf("The sum of %i + %i = %i",a,b,total);
    	return 0;
    }
    Here's the error message I get:
    D:\Hwk3\Task2>gcc -Wall -o main.exe main.c _ad.o
    d:/djgpp/tmp\cchG8Zlv.o(.text+0x6e):main.c: undefined reference to `ad'
    collect2: ld returned 1 exit status
    Last edited by Dragoon_42; 11-11-2003 at 07:14 PM.

  6. #6
    .
    Join Date
    Nov 2003
    Posts
    307
    gcc supports inline asm, as I recall.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Should be something like:

    ; main.asm
    global _ad
    ; ...
    section .text
    ; ...
    _ad:
    ; ...

    /* main.c */
    extern int ad(int, int);
    /* ... */
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed