C51 compiler V6.23, at optimization levels of 7 or above generates crash code as shown in the listing below. The compiler PUSHes R1,R2,and R3 on the stack upon return from the function. When the Do While loop terminates, the function returns using the pushed value instead of the return address, in this case, a jump to location 0 causing a reset. Has anyone found a workaround or fix?
C51 COMPILER V6.23a, COMPILATION OF MODULE TEST OBJECT MODULE PLACED IN Test.OBJ COMPILER INVOKED BY: C:\SOFTUTIL\KEIL\C51\BIN\C51.EXE Test.c OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND CODE stmt level source 1 char * TestSomething(); 2 bit TestFunction(char *frame) 3 { 4 1 char temp; 5 1 do 6 1 { 7 2 temp = 0; 8 2 } 9 1 while ((frame = TestSomething()) != 0); 10 1 return (1); 11 1 } 12 ASSEMBLY LISTING OF GENERATED OBJECT CODE ; FUNCTION _TestFunction (BEGIN) ; SOURCE LINE # 2 ;---- Variable 'frame' assigned to Register 'R1/R2/R3' ---- ; SOURCE LINE # 3 0000 ?C0003: ; SOURCE LINE # 6 ; SOURCE LINE # 7 0000 E4 CLR A 0001 F500 R MOV temp,A ; SOURCE LINE # 8 ; SOURCE LINE # 9 0003 120000 E LCALL TestSomething 0006 C003 PUSH AR3 0008 C002 PUSH AR2 000A C001 PUSH AR1 000C E9 MOV A,R1 000D 4A ORL A,R2 000E 70F0 JNZ ?C0003 ; SOURCE LINE # 10 0010 D3 SETB C ; SOURCE LINE # 11 0011 ?C0004: 0011 22 RET ; FUNCTION _TestFunction (END)