LEX 'COUNTLEX' count occurrences of char in str (HP71) * * Tapani Tarvainen 86/09/10 * * Function COUNT(,) * returns the number of times occurs in * must be single character * * E.g., COUNT("abracadabra","a") = 5. * POP1S EQU #0BD38 pop string HDFLT EQU #1B31B hex integer to dec floating-point FNRTN1 EQU #0F216 function return ARGERR EQU #0BF19 "Invalid Arg" -error ID #5E POLL 0 MSG 0 ENTRY COUNT CHAR #F function KEY 'COUNT' TOKEN 7 ENDTXT NIBHEX 4422 2 string params COUNT GOSBVL POP1S str2 A=A-1 A length-1 A=A-1 A length-2 (nibs!) ?A=0 A length=1? (byte!) GOYES str2OK yes GOVLNG ARGERR no, Invalid Arg str2OK C=DAT1 B the char we're counting D=C B save in D(B) D1=D1+ 2 GOSBVL POP1S str1 A=A+A A A=A+A A A=A+A A ASR A length in bytes B=0 A initialize result A=A-1 A set counter = length-1 GOC null length=0, return 0 loop C=DAT1 B next char D1=D1+ 2 ?C#D B = str2? GOYES nxt B=B+1 A yes, increase result nxt A=A-1 A decr char count GONC loop repeat if any left null A=B A result GOSBVL HDFLT convert to floating point C=A W FNRTN1 wants it in C GOVLNG FNRTN1 return END