alloca() for TurboC

Roger Neil Hook ssuhook at rosemary.cs.reading.ac.uk
Sat Aug 18 07:48:20 AEST 1990


I am trying to compile GNU bison version 1.10 on a PC running DOS 3.3, using
Turbo C v2.0. The source references a function called 'alloca' to allocate
memory off the stack which is automatically freed when the function calling it
returns.
 
'alloca' is not included in the TurboC library, and is anyway not standard C.
I have come across two sets of assembly sources for a __680x0__ version, but
do not understand precisely how they function, not having much experience in
assembly.

Turbo C describes two methods of calling C functions which it calls 'cdecl'
and 'pascal' conventions. 

I have been trying to work out how to write an 80x86 assembly version of
alloca to work with Turbo C. The problems I have encountered relate to not
understanding what the conventions for calling a function are, and also 
confusion arising from trying to work out what the stack pointer is doing.
To whit: 
   I assume it is possible for a C compiler (on a PC but not necessarily 
   TurboC) to have a function, say A(), which does some stuff which ends
   up with data stored on the stack. Then it calls alloca() which I assume
   modifies the stack pointer to hide the alloca'd block.
 
1) This being the case, how does A find it's stacked data?
2) Also how does the memory get freed?
3) How can I write a version of alloca in 8086 assembly for the PC, and 
   TurboC in particular?

4) Finally, could someone please explain what alloca _should_ do, and also
   what the version below (extracted from a GNU library for the Atari GCC)
   does, especially the last op:

         .text
         .even

.globl   _alloca
_alloca:
       movel   sp at +,a0           ;get return addr
       movel   sp at +,d0           ;get size -- assist in bug fix, add 4 to sp
       addql   #1,d0             ;ensure address even
       andl    #0xFFFFFFFE,d0    ;lop off extra bits
       subl    d0,sp             ;increase stack frame size by that much
       movel   sp,d0             ;set up to return it
       lea     sp@(-4),sp        ;new top of stack (real bug fix here)
       jmp     a0@               ;return by jumping via saved addr


In summary, I am mainly interested in finding out how to get an alloca() for
the PC for Turbo C, and also what it is that alloca() actually does.

My apologies in advance if bits of this aren't relevant to any particular 
group.

Please email me, and I will endeavour to summarize to the net if there is 
enough interest. Thanks,

Alias
JANET: ssuhook at susssys1.rdg.ac.uk 



More information about the Comp.lang.c mailing list