Code Optimization for...next cycling through variables

Flitch

New Member
is it posible to optimize this in some kind of loop that will just change variables that needs switching


Code:
	bintoascii b2,bino,bini,binii						
	gosub LCD_SetCursor
	Char = bini				'
	gosub LCD_WriteChar
	Char = binii						
	gosub LCD_WriteChar
	
	
	bintoascii b1,bino,bini,binii						
	Char = ":"
	gosub LCD_WriteChar
	Char = bini				'
	gosub LCD_WriteChar
	Char = binii						
	gosub LCD_WriteChar
	
	Char = 32
	gosub LCD_WriteChar
	
	bintoascii b4,bino,bini,binii						
	Char = bini				'
	gosub LCD_WriteChar
	Char = binii						
	gosub LCD_WriteChar
	
	
	bintoascii b5,bino,bini,binii						
	Char = "/"
	gosub LCD_WriteChar
	Char = bini				'
	gosub LCD_WriteChar
	Char = binii						
	gosub LCD_WriteChar
	
	
	bintoascii b6,bino,bini,binii
	Char = "/"
	gosub LCD_WriteChar
	Char = bini	
	gosub LCD_WriteChar
	Char = binii					
	gosub LCD_WriteChar
 

vttom

Senior Member
is it posible to optimize this in some kind of loop that will just change variables that needs switching


Code:
	bintoascii b2,bino,bini,binii						
	gosub LCD_SetCursor
	Char = bini				'
	gosub LCD_WriteChar
	Char = binii						
	gosub LCD_WriteChar
	
	
	bintoascii b1,bino,bini,binii						
	Char = ":"
	gosub LCD_WriteChar
	Char = bini				'
	gosub LCD_WriteChar
	Char = binii						
	gosub LCD_WriteChar
	
	Char = 32
	gosub LCD_WriteChar
	
	bintoascii b4,bino,bini,binii						
	Char = bini				'
	gosub LCD_WriteChar
	Char = binii						
	gosub LCD_WriteChar
	
	
	bintoascii b5,bino,bini,binii						
	Char = "/"
	gosub LCD_WriteChar
	Char = bini				'
	gosub LCD_WriteChar
	Char = binii						
	gosub LCD_WriteChar
	
	
	bintoascii b6,bino,bini,binii
	Char = "/"
	gosub LCD_WriteChar
	Char = bini	
	gosub LCD_WriteChar
	Char = binii					
	gosub LCD_WriteChar
This isn't exactly a loop, but it may reduce the amount of code space because it puts the bintoascii in the subroutine...

Code:
Char=255 : b0=b2 : gosub mysub
Char=":" : b0=b1 : gosub mysub
Char=32  : b0=b4 : gosub mysub
Char="/" : b0=b5 : gosub mysub
Char="/" : b0=b6 : gosub mysub

mysub:
	if Char = 255 then
		gosub LCD_SetCursor
	else
		gosub LCD_WriteChar
	endif

	bintoascii b0,bino,bini,binii						
	Char = bini
	gosub LCD_WriteChar
	Char = binii
	gosub LCD_WriteChar
	return
 

Flitch

New Member
wow dude, that saved me 130 bytes :D which was rly needed i am now on "Memory used = 1384 bytes out of 2048" which is rly what i needed cos i am not even at half of my work yet :S

Can You give me formula for this one too?
Code:
ConvertToDecimal:
	b1 = b1 / 16 * $FFFA + b1 ;minuta
	b2 = b2 / 16 * $FFFA + b2 ;sat
	b3 = b3 / 16 * $FFFA + b3 ;dan
	b4 = b4 / 16 * $FFFA + b4 ;datum
	b5 = b5 / 16 * $FFFA + b5 ;mjesec
	b6 = b6 / 16 * $FFFA + b6 ;godina
	
Return
 

lbenson

Senior Member
"Memory used = 1384 bytes out of 2048"

Note that if you are using 14M2, 18M2+, 20M2, then you have an extra 2K of programming space in slot 1 thanks to PE6.
 

AllyCat

Senior Member
Hi,

Beware that functions like BINTOASCII are "macros" created by the Program Editor and sometimes can be written even more efficiently yourself (smaller and/or faster). That's because the PE doesn't "know" if all the digits are actually being used, and often has to use PEEKs and POKEs because it doesn't know if any registers are (or will remain) free.

But using the "Byte Pointer" (bptr) is most likely to give the greatest economies, and also the opportunity to "free up" many registers (b0 - b25...) because it can "point" well beyond b25 (i.e. up to 127, 255 or even 511 depending on the chip). I believe the following does what you want in about 1/3 the number of bytes:
Code:
for bptr = 1 to 6
	@bptr = @bptr / 16 * $FFFA + @bptr
next bptr
@bptrinc is sometimes even more efficient (and faster) but doesn't appear to help in this particular example. For fastest speed, simple IF ... THEN constructs can be faster than the "mathematical" expressions like that above.

Cheers, Alan.
 

Flitch

New Member
@Iberson
PLSSSSSSSSSSSSSSS TELL ME THAT I CAN MERGE THEM TOGETHER INTO 4K


@AllyCat
Thanks man, that is exacly what i was looking for (some form of for-next for variables)

this forum rocks so hard :DDDDD
 

lbenson

Senior Member
>PLSSSSSSSSSSSSSSS TELL ME THAT I CAN MERGE THEM TOGETHER INTO 4K

No, you have to code in such a way that some things are done in slot1 and some are done in slot0 (what you've been programming in before the other slot became available). Variables are all shared between slots.

As an example, I'm working on a program which reads 16 sensors and sets 5 outputs and based on the sensors establishes some values in ram for subsequent display. I do all of that in slot1, and all user interaction in slot0. It is still effectively one big "do" loop performed over and over forever, but you have to take some care when switching between slots.

I'm still not certain of the best way to do that. Right now I have a variable which I set to a constant before running slot 1. The code at the beginning of slot1 is a "SELECT" statement which calls one or more routines based on the variable. These routines return to the beginning code, which runs slot0. Slot 0 must then determine where to pick up based on the routine variable or some other variable or variables.

The release of PE6 was most timely for me--it came out the day before I ran out of the 2k of programming space on the 14M2. Presto, all of my hardware was upgraded to double the programming memory.
 
Top