Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 204425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:28:00+00:00 2026-05-11T17:28:00+00:00

I have a set of assembly function which I want to use in C

  • 0

I have a set of assembly function which I want to use in C programs by creating a header file. For instance, if I have asm_functions.s which defines the actual assembly routines and asm_functions.h which has prototypes for the functions as well as some standard #define’s I needed. My goal is to use a C program, say test_asm.c to call the assembly functions.

asm__functions.h:


 #define ASM_CONST_1    0x80
 #define ASM_CONST_2    0xaf

uint8_t asm_foo( int, int, int );

asm__functions.s:


 /* dont need this: #include "asm_functions.h" */

.section .text .type asm_foo, @function asm__foo: /* asm code with proper stack manipulation for C calling conventions */ ret

test__asm.c:


 #include "asm_foo.h"

int main() { uint8_t res = asm_foo( 1, 2, 3); return 0; }

In a situation like this what would be the proper way to compile a link the program? I was trying something like this:


gas -o asm_foo.o asm_foo.s
gcc -o test_asm test_asm.c

But I still get a linker error from GCC saying that my assembly routine is undefined. I hope this contrived example is good enough to explain the situation.

Thanks!

EDIT:

Here is a snippet of output when I compile with a single command:


tja@tja-desktop:~/RIT/SP2/latest$ gcc -o test_pci pci_config.s test_pci.c
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte':
(.text+0x8): undefined reference toPCI_FUNCTION_ID'
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte':
(.text+0xa): undefined reference toREAD_CONFIG_BYTE'
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte':
(.text+0x18): undefined reference toPCI_BIOS_FUNCTION_INT'
/tmp/ccY0SmMN.o: In function _pci_bios_read_byte':
(.text+0x1b): undefined reference toBAD_REGISTER_NUMBER'
/tmp/ccY0SmMN.o: In function _pci_bios_read_word':
(.text+0x30): undefined reference toPCI_FUNCTION_ID'
...

All of those, such as PCI_FUNCTION_ID, are defined in my header file, which is included by the C program. When I compile the assembly code by itself there are no errors.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:28:00+00:00Added an answer on May 11, 2026 at 5:28 pm

    Based on the files in your question, I managed to compile it. I’ve changed both the file names and the file contents.

    asm_const.h :

    #define ASM_CONST_1    0x80
    #define ASM_CONST_2    0xaf
    

    asm_functions.h :

    #include "asm_const.h"
    unsigned char asm_foo( int, int, int );
    

    asm_functions.S (the trailing S must be capital! #include needs it) :

    #include "asm_const.h"
    .section .text
    .globl asm_foo
    .type asm_foo, @function
    asm_foo:
      mov $ASM_CONST_1, %eax
      /* asm code with proper stack manipulation for C calling conventions */
      ret
    

    test_asm.c :

    #include "asm_functions.h"
    int main() {
      return asm_foo( 1, 2, 3);
    }
    

    Please note that you need the the assembly file extension .S with capital S. With .s, the .s file wouldn’t be run through the preprocessor, thus #include wouldn’t work, and you wouldn’t be able to use ASM_CONST_1 in the .s file.

    Compile with a single command:

    gcc -o test_asm asm_functions.S test_asm.c
    

    Or, as an alternative, compile with multiple commands, creating .o files:

    gcc -c asm_functions.S
    gcc -c test_asm.c
    gcc -o test_asm asm_functions.o test_asm.o
    

    The single-command gcc takes care of compiling the .S file using gas, the .c file with GCC’s C compiler, and linking the resulting temporary .o files together using ld. gcc runs all those commands with the appropriate flags by default.

    On some systems (but not on Linux with the default GCC installation) you have to prepend an underscore to the names of exported functions in the .S file (but not in the .c or .h files). So all instances of asm_foo would become _asm_foo only in the .S file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the set focus for a property which I am trying to use
I have set of scripts for doing scripted installs. You can use the scripts
I have a WordPress site (2.6.2) in which I have set the Home page
Task: I have a camera mounted on the end of our assembly line, which
I have a compiled assembly. I want to programmatically compare the method implementation of
We have set up a system where notifications get sent to a user with
I have set a canvas' background to an image of a company logo. I
I have set up transactional replication between two SQL Servers on different ends of
I have set up a version control system using TortoiseSVN at my home to
I have set up a Django application that uses images. I think I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.