Aman
Aman

Reputation: 783

Link and compile an assembly function

I have an assembly function like so

rfact:  
pushl   %ebp
movl    %esp, %ebp
pushl   %ebx
subl    $4, %esp
movl    8(%ebp), %ebx
movl    $1, %eax
cmpl    $1, %ebx
jle     .L53
leal    -1(%ebx), %eax
movl    %eax, (%esp)
call    rfact
imull   %ebx, %eax
.L53:   
addl    $4, %esp
popl    %ebx
popl    %ebp
ret 

I understand I can't just save this as rfact.s and compile it. There has to be certain items (such as .text) appended to the top of the assembly. What are these for a linux system? And I'd like to call this function from a main function written in normal c file called rfactmain.c

Upvotes: 2

Views: 107

Answers (1)

Brett Hale
Brett Hale

Reputation: 22318

Here's a 'minimal' prefix of directives - for ELF/SysV i386, and GNU as:

.text
.p2align 4

.globl  rfact
.type   rfact, @function

I'd also recommend appending a function size directive at the end:

.size   rfact, .-rfact

The easiest way to compile is with: gcc [-m32] -c -P rfact.S
With the -P option, you can use C-style comments and not have to worry about line number output, etc. This results in an object file you can link with. The -m32 flag is required if gcc targets x86-64 by default.

Upvotes: 3

Related Questions