Path: utzoo!attcan!uunet!lll-winken!lll-lcc!ames!mailrus!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!killer!bigtex!james From: james@bigtex.uucp (James Van Artsdalen) Newsgroups: comp.sources.d Subject: Re: Simulation of 'alloca' -- can it be done in TurboC or MSC? Message-ID: <2211@bigtex.uucp> Date: 28 May 88 07:57:11 GMT References: <203@lcuxa.UUCP> <134@lakart.UUCP> Reply-To: james@bigtex.UUCP (James Van Artsdalen) Organization: F.B.N. Software, Austin TX Lines: 47 IN article <134@lakart.UUCP>, dg@lakart.UUCP (David Goodenough) wrote: > From article <203@lcuxa.UUCP>, by mike2@lcuxa.UUCP (M S Slomin): > > Quick question: has anyone worked out a way to simulate the 'alloca' > > function in either TurboC or MSC (DOS for those who don't know)? MS-C 5.0 has alloca(). > Write it in assembler. I don't know how to do alloca in C in a portable > manner. Come to that - since you have to mess with your frame/stack pointer > I don't think it can be done in anything other than assembler. Sure can't > on a z80. There is a version in C in the GNU gawk distribution. I can mail it to anyone who can't get it from the archives. I doubt it's quick, but it is supposed to be portable. They use malloc() to allocate, and some heuristic to detect when the stack pointer is different than it was the last invocation: I'm not sure of exactly how they work it. > This should be fairly easy to convert to 8086 assembler (What, your C compiler > can't talk in assembler?? :-) Different segments may be a pain - this space > will be in SS:, and I can just see referencing through a 16 bit pointer as DS: > UGH!!. Still if you send back a 32 bit pointer SEG:OFFSET or make sure > DS: and SS: are the same, you should be flying. DS: and ES: are typically going to be the same in a small model program (or in native 386 mode). For Microport 386, I just put this in my malloc.h include file: asm char *alloca(n) { %mem n subl n, %esp andl $-4, %esp mov %esp,%eax %reg n subl n, %esp andl $-4, %esp mov %esp,%eax %con n subl n, %esp andl $-4, %esp mov %esp,%eax } Note that it longword-aligns the allocated area. -- James R. Van Artsdalen ...!ut-sally!utastro!bigtex!james "Live Free or Die" Home: 512-346-2444 Work: 328-0282; 110 Wild Basin Rd. Ste #230, Austin TX 78746