PDA

View Full Version : Assembler to C



sean
01-26-2002, 06:56 PM
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?

lightatdawn
01-26-2002, 08:28 PM
Maybe i dont understand completly but dont you just...



void AnAssemblerFunction()
{
asm
{
//assebler goes here
}

return;
}

VirtualAce
01-26-2002, 11:41 PM
For inline do what lightatdawn shows.

For incorporating pure asm files do this:

ASM file


.model LARGE
.code

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
;example
;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
ret

_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.

Unregistered
01-28-2002, 11:07 AM
If previous replies do not work:
which platform ?
which compiler ?