Thread: Assembler to C

  1. #1
    Super Moderator
    Join Date
    Sep 2001

    Assembler to C

    I know I've asked this before, but I can't find the original post:

    How can you incorporate your assembler code into C?

    Do you need to compile it to Machine code with debug or something?

  2. #2
    Maybe i dont understand completly but dont you just...

    void AnAssemblerFunction()
       //assebler goes here
    "There's always another way"
    -lightatdawn (

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    For inline do what lightatdawn shows.

    For incorporating pure asm files do this:

    ASM file
    .model LARGE
    public _Test
    _Test    PROC
       ;to pass parameters do the following
      ARG x:WORD, y:WORD, z:WORD
      ;to use local variables by name instead of address do this
      LOCAL temp1:WORD,temp2:WORD
      ;mov       ax,x              - x refers to WORD x from ARG 
      ;add       ax,y              - y refers to WORD y from ARG
      ;mov       temp1,ax     - can refer to temp1 as a variable
      push     bp               ;do not forget if you use ARG
      mov      bp,sp           ;do not forget if you use ARG
      ;Do your asm here
      pop       bp               ;do not forget this if you use ARG
    _Test   ENDP
    The ARG directive allows you to access parameters w/o having to worry about the diff memory models - but model has to be large to use ARG. No need to use bp+6, bp+4, etc., when you use ARG.
    Other requirement is that you must push bp and mov bp,sp and pop bp before you leave the procedure - otherwise it will crash the system.

    Then save the file with your asm functions in it. The extension should be .asm
    Include the asm file(s) in your project or their objs. Just include the text version of the asm file and set it's local options to use your assembler instead of the C compiler. This way you can compose assembly and C within the same editor and each program will return what errors occured and show you where they occurred.

    Now in C we want to call these just like standard functions:

    extern "C"
    void Test(WORD x,WORD y,WORD z);
    //any other asm functions

    Now you can call your asm Test() from C just like any other C function

    If you want to return values from asm functions, C returns values from functions in the AX register.

  4. #4
    If previous replies do not work:
    which platform ?
    which compiler ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Assembler in GCC
    By Magos in forum Linux Programming
    Replies: 4
    Last Post: 11-05-2002, 04:22 PM
  3. assembler and linker stuff...question
    By dirkduck in forum A Brief History of
    Replies: 2
    Last Post: 12-17-2001, 12:10 AM
  4. MAINFRAME Assembler.
    By sean in forum A Brief History of
    Replies: 3
    Last Post: 12-05-2001, 05:32 PM
  5. interfacing assembler with c++
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2001, 12:13 AM