[comp.lang.c] Assembly langauge subroutines for use with C

ryders@watdcsu.UUCP (07/19/87)

How does one go about writing an assembly function that can be linked
and used by a C program? I have tried several times to get my modules
to link properly but with the end result of "Unresolved external".
I have prefaced the function name with an underscore and I still can't
manage to get my C code to realize that yes, there is an assembly function
by that name in one of the linked modules. HELP!
I'm using MSC 4.0 on an IBM-PC AT.

-- 
Steve Ryder, Independent Studies, University of Waterloo
             Waterloo, Ontario, CANADA, N2L 3G1. (519) 885-1211 Ext. 2352       {allegra,decvax,inhp4,utzoo}!watmath!watdcsu!ryders          RYDERS@WATDCSU.UUCP
"Diligence is the mother of good fortune, and idleness, its opposite, never      brought a man to the goal of any of his best wishes." -CERVANTES, Don Quixote

cramer@kontron.UUCP (Clayton Cramer) (07/21/87)

> How does one go about writing an assembly function that can be linked
> and used by a C program? I have tried several times to get my modules
> to link properly but with the end result of "Unresolved external".
> I have prefaced the function name with an underscore and I still can't
> manage to get my C code to realize that yes, there is an assembly function
> by that name in one of the linked modules. HELP!
> I'm using MSC 4.0 on an IBM-PC AT.
> 
> Steve Ryder, Independent Studies, University of Waterloo

The underscore on the function name is necessary, but consider the
case.  MASM folds all names to uppercase in the symbol table unless
you use the /MX option.  If your assembler subroutine is named
"_foo", and you call it as "FOO" from C, it will work.

Clayton E>+ >+ >/B

platt@emory.uucp (Dan Platt) (07/22/87)

In article <3647@watdcsu.waterloo.edu> ryders@watdcsu.UUCP writes:
>How does one go about writing an assembly function that can be linked
>and used by a C program? I have tried several times to get my modules
>to link properly but with the end result of "Unresolved external".
>I have prefaced the function name with an underscore and I still can't
>manage to get my C code to realize that yes, there is an assembly function
>by that name in one of the linked modules. HELP!
>I'm using MSC 4.0 on an IBM-PC AT.
>


From what I can see,  the symbol is unsresolved because it is not
available to the linker.  To make the symbol available to the linker
the symbol must be declared as public.  The examples in the user's
guide show a correct format for making the symbol public.

If you missed the 'public', did you make sure that the memory
model was correct? How about the calling sequence (saving bp,di
and si?) and all the other niggling details?

Hope this is a help...

Dan

dg@wrs.UUCP (David Goodenough) (07/22/87)

In article <3647@watdcsu.waterloo.edu> ryders@watdcsu.UUCP writes:
>How does one go about writing an assembly function that can be linked
>and used by a C program? 

Lots and lots of ranting and raving and mental anguish deleted

>I'm using MSC 4.0 on an IBM-PC AT.

I rest my case.
--
		dg@wrs.UUCP - David Goodenough

					+---+
					| +-+-+
					+-+-+ |
					  +---+

Rick_R_Kitts@cup.portal.com (07/23/87)

In article <3647@watdcsu.waterloo.edu>  ryders@watdcsu.UUCP writes:

>How does one go about writing an assembly function that can be linked
>and used by a C program? I have tried several times to get my modules
>to link properly but with the end result of "Unresolved external".
>I have prefaced the function name with an underscore and I still can't
>manage to get my C code to realize that yes, there is an assembly function
>by that name in one of the linked modules. HELP!
>I'm using MSC 4.0 on an IBM-PC AT.
>
>--
>Steve Ryder, Independent Studies, University of Waterloo
>             Waterloo, Ontario, CANADA, N2L 3G1. (519) 885-1211 Ext. 2352
>allegra,decvax,inhp4,utzoo}!watmath!watdcsu!ryders          RYDERS@WATDCSU.UUC
>"Diligence is the mother of good fortune, and idleness, its opposite, never
>brought a man to the goal of any of his best wishes." -CERVANTES, Don Quixote


Finally _I_ get to help someone out :-)

Chances are that you neglected to include the PUBLIC decleration required
to access assembler functions form MSC. All functions in MSC are PUBLIC.
Below is a bit of code that should help:

**********************************************************************

PUBLIC  _handler, _setivec, _ptr, _getivec

DGROUP  GROUP   _DATA
ASSUME CS:_TEXT, DS:DGROUP, SS:DGROUP,ES:DGROUP

_DATA   SEGMENT WORD PUBLIC 'DATA'

; If you have any variables that you would like access to in
; C, put them here. If they are declared in C preceed them with
; EXTRN

int12   label   DWORD
        off12   DW      0               ; I do not need these in the C
        seg12   DW      0               ; function, hence no underscores
_DATA   ENDS

_TEXT   SEGMENT WORD PUBLIC 'CODE'

; setivec(i_handler, int_number);
; use in Small model

_setivec PROC NEAR
        push    bp                              ; Use these 2 lines for
        mov     bp, sp                          ; ALL assembly functions
        mov     al, [bp + 4]                    ;Get the int_number
        mov     ah, 25h                         ; from the stack
	push 	ds
        lds     dx, DWORD PTR [bp + 6]          ; Get the address of
        int     21h                             ; int_handler from stack
        pop     ds
        pop     bp                              ; This is also mandatory
        ret
_setivec ENDP

; getivec();
; Get interupt vector
;

_getivec PROC NEAR
        push    bp                              ; See?
        mov     bp,sp
        mov     ah, 35h
        mov     al, 0Ch                         ; Hard wired for INT 12
        int     21h
        mov     off12, bx			; Store offset and segment
        mov     seg12, es
        pop     bp
        ret
_getivec ENDP
_TEXT ENDS
END


**********************************************************************

sun!cup.portal.com!Rick_R_Kitts






; If you have any variables that

davis@bdmrrr.bdm.com (Arthur Davis x4675) (07/25/87)

You must declare the routines that you wish to access from C as
"public" and preface the routine identifier with an underscore, as}i
in 

   public _linkroutine

_linkroutine    proc {near|far}

That should do it.

feg@clyde.ATT.COM (Forrest Gehrke) (07/29/87)

> In article <3647@watdcsu.waterloo.edu>  ryders@watdcsu.UUCP writes:
> 
> >How does one go about writing an assembly function that can be linked
> >and used by a C program? I have tried several times to get my modules
> >to link properly but with the end result of "Unresolved external".
> >


Write a short C function which includes a couple of declarations. 
Compile with the assembly language output switch on. The resulting module
(having the suffix ".cod") will give you a template of what your 
assembly language function needs to look like. Pay attention to the
entry and exit "housekeeping" assembly language code statements.

Forrest Gehrke  

steve@nuchat.UUCP (Steve Nuchia) (08/01/87)

In article <3647@watdcsu.waterloo.edu>, ryders@watdcsu.UUCP writes:
> How does one go about writing an assembly function that can be linked
> and used by a C program? I have tried several times to get my modules
> to link properly but with the end result of "Unresolved external".
> I have prefaced the function name with an underscore and I still can't
> manage to get my C code to realize that yes, there is an assembly function
> by that name in one of the linked modules. HELP!
> I'm using MSC 4.0 on an IBM-PC AT.

First, I suspect you've left off whatever magic phrase tells the
assembler that the entry point to your function is a global symbol.

Secondly, the method I use when I have to hack up an assembly module
for some compiler is to coax it into emitting an assembly source
from a C source containing a stub of the routine to be implemented.
This way you have a good go-by.  Of course this doesn't work if
your compiler won't write assembly source; I don't know about MSC 4.0,
but I do know that I wouldn't buy a compiler that didn't emit assembly
source, no matter how good the debugger is.

	Steve Nuchia
	{{soma,academ}!uhnix1,sun!housun}!nuchat!steve