הלולאה של המספר המקסימלי עובדת לי הכל טוב ויפה הבעיה מתחילה מהלולאה של ה-min number

קוד:
#include "reg_89c51.h"
xdata char num[6] _at_ 0x200;
xdata char even _at_ 0x1000;
void main ()
{
 
 unsigned char data i,z;
 char data max,min,sum;
 for(i=0; i<6; i++)
 num[i]=P1;
 for(z=0; z<6; z++)   //the max number
 if (num[z]>num[z+1])
 {
  max=num[z];
  num[z]=num[z+1];
  num[z+1]=max;
 }
 for(z=6; z>0; z--)     //the min number
 if (num[z]<num[z-1])
 {
  min=num[z];
  num[z]=num[z-1];
  num[z-1]=min;
 }
 for (z=0; z<6; z++)  //How many even numbers we have?
 sum = num[z] & 2;
 if (sum==0)
 {
  even += num[z];
 }
 while (1);
}