Build of test code has push at top of loop but pop only occurs after loop terminates. Each iteration of loop pushes extra bytes.
Loop test includes a mask which was part of original code. Without masking operation the assembled code is correct and also code
built with version 2.8.0 #5117 (Jan 20 2009) (UNIX)
SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.9.0 #5416 (Aug 15 2009) (UNIX)
sdcc --Werror -mmcs51 --model-large -c average.c