Ok, so apparently I'm not intelligent enough to realize the difference between a 0 (zero) and O (the letter O)
If there is a way to make this code better, please let me know.
Ok, so apparently I'm not intelligent enough to realize the difference between a 0 (zero) and O (the letter O)
If there is a way to make this code better, please let me know.
Hi,
You could try a bunch of nested IF statements. It won't be "better" but it might compile to smaller code - worth a try.Another option would be to use an array instead of "discrete" variables and iterate thru it with a for-next loop. Might be worth a try.Code:IF CODE_ONE = 13 THEN IF CODE_TWO = 13 THEN 'With an oh IF CODE_THREE = 16 THEN IF CODE_FOUR = 21 THEN IF CODE_FIVE = 32 THEN 'Correct code ENDIF ENDIF ENDIF ENDIF ENDIF
/Henrik.
Using PBP 2.60C, MCSP v2.2.1.1, MPASM v5.46, PIC 18F46K22.
62 bytes:
138 bytes:Code:@ __CONFIG _CONFIG1H, _FOSC_HSHP_1H & _PLLCFG_OFF_1H & _PRICLKEN_OFF_1H & _FCMEN_OFF_1H & _IESO_OFF_1H @ __CONFIG _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L @ __CONFIG _CONFIG2H, _WDTEN_OFF_2H @ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _HFOFST_OFF_3H & _MCLRE_EXTMCLR_3H @ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L DEFINE OSC 20 ANSELA = 0 ANSELB = 0 ANSELC = 0 ANSELD = 0 ANSELE = 0 TRISA = %00000000 TRISB = %00000000 TRISC = %00000000 TRISD = %10000000 TRISE = %00000000 CODE_ONE VAR PortB.1 CODE_TWO VAR PortB.2 CODE_THREE VAR PortB.3 CODE_FOUR VAR PortB.4 CODE_FIVE VAR PortB.5 IF CODE_ONE = 13 THEN IF CODE_TWO = 13 THEN 'With an oh IF CODE_THREE = 16 THEN IF CODE_FOUR = 21 THEN IF CODE_FIVE = 32 THEN 'Correct code ENDIF ENDIF ENDIF ENDIF ENDIF END
I know, Port be is defined as output and used as input (noticed after test), but that has no impact on test significance.Code:@ __CONFIG _CONFIG1H, _FOSC_HSHP_1H & _PLLCFG_OFF_1H & _PRICLKEN_OFF_1H & _FCMEN_OFF_1H & _IESO_OFF_1H @ __CONFIG _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L @ __CONFIG _CONFIG2H, _WDTEN_OFF_2H @ __CONFIG _CONFIG3H, _PBADEN_OFF_3H & _HFOFST_OFF_3H & _MCLRE_EXTMCLR_3H @ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L DEFINE OSC 20 ANSELA = 0 ANSELB = 0 ANSELC = 0 ANSELD = 0 ANSELE = 0 TRISA = %00000000 TRISB = %00000000 TRISC = %00000000 TRISD = %10000000 TRISE = %00000000 CODE_ONE VAR PortB.1 CODE_TWO VAR PortB.2 CODE_THREE VAR PortB.3 CODE_FOUR VAR PortB.4 CODE_FIVE VAR PortB.5 IF CODE_ONE = 13 and CODE_TWO = 13 and CODE_THREE = 16 and CODE_FOUR = 21 and CODE_FIVE = 32 THEN 'Correct code ENDIF END
More than twice as big using single IF, nested IF is definitely more efficient. I remember reading about that somewhere recently too, can't remember where.
Robert
![]()
Last edited by Demon; - 24th September 2012 at 15:44.
you could also turn it on its head,
IF CODE_ONE <> 13 THEN EXIT_TO_ERROR
AND SO ON,
Then after the last comparison you have a valid password.
Hi,
I tried four examples here, compiled for a 16F628.
First test:Result: 75 WORDSCode:IF CODE_ONE = 13 and CODE_TWO = 13 and CODE_THREE = 16 and CODE_FOUR = 21 and CODE_FIVE = 32 THEN 'Correct code ENDIF
Second test:Result: 26 WORDSCode:If CODE_ONE = 13 THEN IF CODE_TWO = 13 then if CODE_THREE = 16 then if CODE_FOUR = 21 then if CODE_FIVE = 32 THEN 'correct code Endif endif endif endif endif
Third test:RESULT: 26 WORDS (same as test 2)Code:IF CODE_ONE <> 13 THEN EXIT_TO_ERROR IF CODE_TWO <> 13 THEN EXIT_TO_ERROR IF CODE_THREE <> 13 THEN EXIT_TO_ERROR IF CODE_FOUR <> 13 THEN EXIT_TO_ERROR IF CODE_FIVE <> 13 THEN EXIT_TO_ERROR 'correct code EXIT_TO_ERROR:
Forth test:
This one uses an array to hold the entered code and assumes that the correct password sequence is stored in EEPROM, starting at location 0RESULT: 41 WORDSCode:Correct = 1 For i = 0 to 4 Read i, Password if Code[i] <> Password THEN Correct = 0 ENDIF NEXT If Correct THEN 'correct code endif
So between these four method 2 or 3 produces the smallest code - by far. Personally I think Aerostars suggestion is the easiest to read but that might just be me...
/Henrik.
Bookmarks