PRB:-Aw/-Au Uses Wrong Segment if Passed Pascal Func. Return

ID Number: Q66508

6.00 6.00a 6.00ax | 6.00 6.00a

MS-DOS | OS/2

buglist6.00 buglist6.00a buglist6.00ax fixlist7.00

Summary:

SYMPTOMS

In Microsoft C versions 6.0, 6.0a, and 6.0ax, when compiling for

DS!=SS (/Au or /Aw) and when passing the return value of a Pascal

function that returns a structure to another function, the compiler

will use the BX register for accessing the returned structure

without an SS override. (Memory accesses default to the DS register

unless the BP or SP register is involved in the address

calculation, in which case SS is the default.)

The compiler does correctly issue the following warning in this

situation:

warning C4059: segment lost in conversion

STATUS

Microsoft has confirmed this to be a problem in C versions 6.0,

6.0a, and 6.0ax. This problem was corrected in C/C++ version 7.0.

More Information:

The sample code below may be used to illustrate this problem. The

problem can be observed in the following excerpt of the .COD (/Fc)

listing. Note the structure was returned on the stack, not in DGROUP.

Without /Au or /Aw this is acceptable, because the stack is in DGROUP;

however, when /Au or /Aw is used, this will cause the words to be read

from the wrong segment.

;| |*** func1(func2()); /* this line causes warning C4059 */

; Line 18

*** 00006d 8d 46 f8 lea ax,WORD PTR [bp-8]

*** 000070 50 push ax

*** 000071 e8 8c ff call FUNC2

*** 000074 8b d8 mov bx,ax

*** 000076 ff 77 06 push WORD PTR [bx+6]

*** 000079 ff 77 04 push WORD PTR [bx+4]

*** 00007c ff 77 02 push WORD PTR [bx+2]

*** 00007f ff 37 push WORD PTR [bx]

*** 000081 e8 c0 ff call _func1

Sample Code

-----------

/* Compile options needed: /Au

*/

#include <stdio.h>

struct s {int i,j,k,l; };

struct s pascal func2(void)

{

struct s b = {1, 2, 3, 4};

return b;

}

void func1(struct s b)

{

printf("%d %d %d %d\n", b.i, b.j, b.k, b.l);

}

void main ()

{

func1(func2()); /* this line causes warning C4059 */

}

Additional reference words: 6.00 6.00a 6.00ax 7.00