Subj : Re: Math error ??? doen't make sense! To : borland.public.cpp.borlandcpp From : waking Date : Sun Jul 04 2004 05:48 am On Sat, 3 Jul 2004 23:12:17 -0400, "Stan DeGroff" wrote: >My experience in assembly and/or PLC programming >would have taken two 16 bit integers and expected a 32 bit result without >having to type cast. I would only need to provide a 32 bit location/register >for the result. No mention of any intermediate storage considerations being >required. Some of the implementation details of the C/C++ languages can be counter-intuitive to the uninitiated. (I programmed in 7 or 8 assembly languages before moving to C, and can appreciate your perspective.) Pay particular attention to the order of precedence for operators, and the rules for evaluating expressions. With the assignment operator (=) the expression to the right of the operator is evaluated independently of the expression to the left. Thus, in the statement z=x*y; the x*y is evaluated just as if it appeared all by itself: x*y; One caveat: be careful when examining asm or obj code generated by a compiler. Don't be too hasty to generalize based on what may actually be implementation-specific details. It's often better to work from the top down - using a good text or the official language spec - rather than starting at the output of the compiler and trying to work back to an inferred rule. Moving from the bottom up doesn't help you understand *why* the compiler is handling something the way that it is. It may be constrained by the requirements of the higher level language. That being said, if you remain curious, you can have the compiler generate asm output from your C/C++ source code and browse to your heart's content. For example, here is the 16-bit asm code generated by Turbo C++ 3.0. (BC++ 4.5x/5.0x would be similar if not identical.) ; z=x*y; ; mov ax,word ptr [bp-2] imul word ptr [bp-4] cwd mov word ptr [bp-6],dx mov word ptr [bp-8],ax ; z=x*(long)y; ; mov ax,word ptr [bp-2] cwd push ax mov ax,word ptr [bp-4] push dx cwd pop cx pop bx call near ptr N_LXMUL@ mov word ptr [bp-6],dx mov word ptr [bp-8],ax -- Wayne A. King (waking@idirect.com, Wayne_A_King@compuserve.com) .