Re^2: QuickC

danr at hcx2.SSD.HARRIS.COM danr at hcx2.SSD.HARRIS.COM
Wed Sep 7 09:00:00 AEST 1988


> /* Written 11:45 am  Aug 27, 1988 by leif at ambone.UUCP in hcx2:comp.lang.c */
> /* ---------- "Re^2: QuickC" ---------- */
> TURGUT at TREARN.BITNET (Turgut Kalfaoglu) writes:
> >square(num)
> >int num;
> >{
> >  num = num*num;
> >}
> 
> >OK? There is no 'return' statement in the function. However, it works!
> >I get '4' as an answer. So I thought maybe it was keeping the result
> >of the last operation, so I added some dummy lines,
> 
> >square(num)
> >int num;
> >{
> >  int dummy;
> >  num = num*num;
> >  dummy=222;
> >}
> 
> >but the call STILL works... Can anyone shed some light onto this?
> >WHY does it work?
>
>  UUCP: leif at ambone.dk, phone: +45 2424 111; ABC BBS: +45 68 00 544 (2:505/38)
> /* End of text from hcx2:comp.lang.c */

I don't know whether any solution has been determined yet for this, because
only one reply comes even close (that I have seen...).  Here's my vote, 
considering what I learned in my computer science courses where we used
Zenith AT-clones running ZENIX.      

The convention used by the C compiler on the 80*86 - based c compilers I 
have seen (Microsoft, Zenix, MS-DOS) calls for integers to be returned
in the AX register.  If the value of the square was allocated the AX 
register (which I believe you would see if you eyeballed the assembly),
then that value would still be there when the function was returned.
EVEN if no 'return ()' was explicitly encoded.  The example using 'dummy'
still works because the register allocator does its job and allocates 
different register(s) for that assignment (or probably does the job
using a mov imediate to mem instruction, so no register is used).
So, the moral of the story is that you were lucky this time, simply 
because of a quirk in the calling convention.  

I have seen this sort of behavior on other machines where the return value
is in a register which may have been used for results, etc in the body
of the function.

-Dan Rittersdorf

:: Please excuse those of us who are still learning the etiquete of the
   net if we break any sacred rules.  I promise to look real sad if 
   convicted of such a horrid act. :)  (my FIRST smiley!!!!)

:: Please also forgive the lack of a real nifty name, address, and 
   disclaimer, as I have not yet taken the time.  Instead, let me
   state that all views expressed above are in no way those of 
   Harris Corporation, but are MINE, MINE, MINE!!!

:: And last, but not least, let me say that although I may have a 
   machine containing an Intel cpu on my desk at home, I would
   not wish my machine on anyone I like.  Thank you.



More information about the Comp.lang.c mailing list