Since my 8051 compiler doesn't have a feature that detects unused variables, I decide to try to implement my own, but it doesn't work.
When the program runs, it successfully identifies all labels by reading the main file then for each label name, it scans the entire file and for each line scanned, the following function is called:
findlabel(labelname,fileline);
Upon executing the program, it incorrectly identifies the following variables as unused:
PQR, MNO, TUV, and the null
The file that I am having this program continuously scan has the following contents:
ABC equ 1h
GHI equ 2h
JKL equ 3h
TUV equ 6h
MNO equ 4h
PQR equ 5h
cjne A,#ABC,def
mov GHI,#1h
mov JKL,MNO
def:
But MNO is used in the "mov JKL,MNO" line.
I also tried trimming out carriage returns and extra spacing and that was no help.
What am I doing wrong?
Source code follows:
void trim(char* astr){
while (astr[0]==' ' || astr[0]=='\r'|| astr[0]=='\t' || astr[0]=='\n'){
strcpy(astr,astr+1);
}
int sz=strlen(astr)-2;
while(astr[sz]==' ' || astr[sz]=='\r'|| astr[sz]=='\t' || astr[sz]=='\n'){
astr[sz]='\0';sz--;
}
}
int findlabel(char* lbl,char*fline){
int par,isdec=0;
char* semicolon=strcasestr(fline,";");
char* qs=strcasestr(fline,"';'");
if (semicolon && !qs){
//strip everything after semicolon if not quoted
memcpy(fline,fline,semicolon-fline);
fline[semicolon-fline]='\0';
}
trim(fline);
char* spc=strcasestr(fline," "); //Make sure there's a space inbetween text
if (spc){
strcpy(fline,spc+1); // toss out command
trim(fline);
for (par=1;par<=3;par++){
char ilbl[2000];
char* comma=strcasestr(fline,",");
if (comma){
//found comma so strip it and save parameter to ilbl
memcpy(ilbl,fline,comma-fline);
ilbl[comma-fline]='\0';
strcpy(fline,comma+1);
}else{
//no comma so run this loop one more time with last part of file line as parameter
strcpy(ilbl,fline);par=99;
}
trim(ilbl);
if (strcasecmp(ilbl,lbl)==0){isdec=1;par=99;} //first param = #label
if (ilbl[0]=='#'){
strcpy(ilbl,ilbl+1);
if (strcasecmp(ilbl,lbl)==0){isdec=1;par=99;} //first param = label
}
}
}
return isdec;
}