I need to code a couple of short .asm functions that will be called by C and have pulled an example off the web. I'm using Fedora 9 and gcc. I'm getting several undefined references and I'm not sure why.
Here's my compile, followed by the program listings:
[michael@localhost ~]$ nasm -f elf maxofthree.asm -o maxofthree.o
[michael@localhost ~]$ gcc -c callmaxofthree.c -o callmaxofthree.o
[michael@localhost ~]$ gcc callmaxofthree.o maxofthree.o -o callmaxofthree
callmaxofthree.o: In function `main':
callmaxofthree.c:(.text+0x29): undefined reference to `maxofthree'
callmaxofthree.c:(.text+0x55): undefined reference to `maxofthree'
callmaxofthree.c:(.text+0x81): undefined reference to `maxofthree'
callmaxofthree.c:(.text+0xad): undefined reference to `maxofthree'
callmaxofthree.c:(.text+0xd9): undefined reference to `maxofthree'
callmaxofthree.o:callmaxofthree.c:(.text+0x105): more undefined references to `maxofthree' follow
collect2: ld returned 1 exit status
[michael@localhost ~]$ cat callmaxofthree.c
/*
* callmaxofthree.c
*
* Illustrates how to call the maxofthree function we wrote in assembly
* language.
*/
#include
int maxofthree(int, int, int);
int main() {
printf("%d\n", maxofthree(1, -4, -7));
printf("%d\n", maxofthree(2, -6, 1));
printf("%d\n", maxofthree(2, 3, 1));
printf("%d\n", maxofthree(-2, 4, 3));
printf("%d\n", maxofthree(2, -6, 5));
printf("%d\n", maxofthree(2, 4, 6));
return 0;
}
[michael@localhost ~]$ cat maxofthree.asm
; ----------------------------------------------------------------------------
; maxofthree.asm
;
; NASM implementation of a function that returns the maximum value of its
; three integer parameters. The function has prototype:
;
; int maxofthree(int x, int y, int z)
;
; Note that only eax, ecx, and edx were used so no registers had to be saved
; and restored.
; ----------------------------------------------------------------------------
global _maxofthree
section .text
_maxofthree:
mov eax, [esp+4]
mov ecx, [esp+8]
mov edx, [esp+12]
cmp eax, ecx
cmovl eax, ecx
cmp eax, edx
cmovl eax, edx
ret
[michael@localhost ~]$