From: spc@pineal.sci.fau.edu (Sean 'Captain Napalm' Conner)
Subject: Re: ass'y gurus?
Organization: Florida Atlantic University, but who cares?
Distribution: alt
Date: Wed, 2 Sep 1992 17:48:51 GMT

In article <1992Sep2.1937.412@dosgate> "gord armstrong" <gord.armstrong@canrem.com> writes:
>
>Segments ARRRG!
>
>'C' Compiler: Microsoft C 6.0, Assembler: TASM 2.0
>
>I'm linking a 'C' and assembler prog together in DOS, and I need
>to pass a pointer from a C prog to an assembler proc. For a SMALL
>model this is not much of a problem since code and data are in the
>same segment, as follows:
>
  [ C and Assembly moved to /dev/bitbucket ]

>
>For a LARGE model, I can't seem to find a way or a keyword to
>do it neatly. A 'malloc' in the large model has 'ptr' pointing
>to another segment. I can make it work doing the following:
>
  [ More C and Assembly moved to /dev/bitbucket ]

>
>It works, but it looks messy, is there a neater way of doing this?
>If I use OS/2, I won't have this problem since it uses protected
>mode...right?
>
>All help appreciated!
>---------------------------------------------------------------------
>gord.armstrong@canremote.com   "If you blow chunks and she comes
>                                back, she's yours. If you spew and
>                                she bolts, it was never meant to be"
>                                - Wayne's World
>--
>Canada Remote Systems  - Toronto, Ontario/Detroit, MI
>World's Largest PCBOARD System - 416-629-7000/629-7044

  It's not all that bad.  For example, using LARGE memory model (far pointers),
a typical C fragment would be ...

/*************************************************************/

int asm_snafu(int,int far *);	/* prototype */

int fubar(void)
{
  int far *blah;
  int      ret_code;

  blah = malloc(1024);	/* allocate a 1K block of far memory */

  ret_code = asm_snafu(1024,blah); /* pass in size (bytes) and location of memory block */

  return(ret_code);
}

/*******************************************************************/

  And now for the Assembly portion (assuming LARGE memory, using old style
MASM constructs here (I tend not to do much C/Assembly mixing, for good
reason 8-)

	
;-------------------------------------------------------------------

		public	asm_snafu

;--------------------------------------------------------------------

;*********************************************************************
code		segment	byte	public	'TEXT'

;----------------------------------------------------------------------
;	ASM_SNAFU:	blah blah what this does blah blah
;proto: int asm_snafu(int,int far *)
;ret: 	some value
;----------------------------------------------------------------------

_intptr		=	8			;offset in stack to paramters
_intsize	=	6

_asm_snafu	push	bp			;create stack frame
		mov	bp,sp
		push	ds			;save DS

		lds	bx,[bp+_intptr]		;load DS:BX to point to data
		mov	cx,[bp+_intsize]	;get size of memory block

		.				;code to do whatever ...
		.
		.

		pop	ds			;exit code
		mov	sp,bp
		pop	bp
		retf

;----------------------------------------------------------------------

code		ends
;**********************************************************************
		end



  Hope this helps some (I've also assumed the code was called via a FAR call).

  -spc (and Protected mode is a whole 'nother ball o' wax ...)