[TIB] Re: DOS programming


[Prev][Next][Index][Thread]

[TIB] Re: DOS programming



In a message dated 03/04/16 6:39:51 PM Eastern Standard Time, jtkirk86@bellsouth.net writes:


Example code

Input “c:\”,com

If com=cls Then:ClrIO:Goto start:EndIf

If left(com,2)=cd Then:setfold(right(com,3)):goto start:EndIf



Ok, I can't actually help you with the If's, but I can help you with something else down the road...  NEVER NEVER NEVER use a Goto x inside of an If:Then loop.

Look what happens in the program flow to see what I mean...

; if the test is true...:
Lbl Hi
If 1 = 1
; the next command executes
Then
; it's a then!  It'll keep running commands till it hits an end!  (In c++ and any other bracketed language, this is made clear by indenting...  So that's what I'll do.
     CLRIO
     ; it cleared the screen.  cool.
     Goto Hi
     ; here's your problem.  It went to Hi, but never reached the next command...  The end...
     End
     ; if the result is true, it'll never actually reach this end here, and the loop never ends!

The reason that that's a problem is that somehow, it uses up ~37 bytes of memory (on the 83+) every time the loop executes...  This will slow down the calculator, and eventually give you a out of memory error, for apparently no reason (because when the program ends, all the memory reserved by the program during it's execution is restored to the ram, so you never actually see that the # of free ram bytes went to 0...)

If you were to do this loop the way the computer looks at it, here's what you get...
; if it's true:
Lbl Hi
If 1=1:Then
     CLRIO:Goto Hi
     Lbl Hi
     If 1=1:Then
          CLRIO:Goto Hi
          Lbl Hi
          If 1=1:Then
               CLRIO:Goto Hi

(and so on, till the calc runs out of memory.)
The EndIf (or End on 83(+) basic) is only reached when the loop is false, and that just tells the program when to begin execution again.

; if it's false
Lbl Hi
If 1=0:Then
     CLRIO:Goto Hi:End
Disp "Poing"

the End just says when to start again...
     CLRIO:Goto Hi
is simply skipped...

To resolve the problem?  You've got two choices, one much more elegant than the other, but neither perfect...

You could force your program to be a perfectly obedient program and close the loop every time you went through the loop, but then you'd have to create an open loop before you ever got to the label to avoid a syntax error, making the resulting code look something like...

While 1=1
     Lbl Hi
End
If 1=1:Then
     CLRIO:Goto Hi
End
(the rest of the program code)

Or, you could just be nice, smart, and decent to the calculator and the world around you and just avoid putting the Goto in the loop, by doing something like:

Lbl Hi
If 1=1:Then:CLRIO:...(whatever else):End
If 1=1:Goto Hi
(the rest of the program code)

Yes, it's irritating, annoying, and dumb, but it's better than memory errors and slow runs every time you use the program.

(Ever met a program that slowed down the longer you played it?  This is why...  It has to keep jumping through every loop more and more times, which slows down the processor.)

An example of this can be seen very effectively if you do the following...  (it's an obvious remake of the loop problem)

Program ENDLESS:
prgmENDLESS

(the program that does absolutely nothing but call itself.  Over and Over and Over again.  it'll crash in about 30 seconds on an 83 with all the ram free.)

Nick