1

I'm trying to call C function in assembler. This is my code: C:

int multiply(int what)
{
    return what * 2;
}

ASM:

extern multiply

start:
    mov eax, 10
    push eax
    call multiply

    jmp $

;empty 
times 510-($-$$) db 0
dw 0xAA55

I'm compiling C code to elf by gcc (MinGW) and ASM code by NASM. I'm compiling it without any problems, but when I'm trying to use this code(for creating .bin file):

gcc -o test.bin work.o test.o

I' getting this error: My error

Does anybody know how to call C function from ASM code, compile it and link it to working .bin file? Please help.

user35443
  • 6,309
  • 12
  • 52
  • 75

2 Answers2

3

Try to add '_' to multiply:

extern _multiply

Works for me in this simple example:

global  _main

extern  _printf

section .data

text    db      "291 is the best!", 10, 0
strformat db    "%s", 0

section .code

_main
    push    dword text
    push    dword strformat
    call    _printf
    add     esp, 8
    ret
Blood
  • 4,126
  • 3
  • 27
  • 37
  • +1 @user see also: http://stackoverflow.com/questions/1034852/adding-leading-underscores-to-assembly-symbols-with-gcc-on-win32 – T.J. Crowder Jul 05 '12 at 12:22
1

Try "global multiply" instead of "extern multiply" in your .asm file. You shouldn't need the underscore for ELF (I don't think), but you can get Nasm to automagically add an underscore to anything "extern" or "global" by adding "--prefix _" to Nasm's command line.

Edit: I take that back, "extern" is correct. You seem not to have a "main". Try adding "--nostartfiles" (may be only one hyphen) to gcc's command line.

Best,

Frank

Frank Kotler
  • 1,462
  • 1
  • 8
  • 4