Sunday, July 24, 2022

Essay06 The Core of Computer Programming (part 2)

Essay06 The Core of Computer Programming (part 2)

The usage of IF-GOTO


In part one, I talk about the fundamental hardware necessary to enable IF-GOTO Conditional Branching. In part two, I will show the different way IF-GOTO can be used. Notwithstanding the exhortation "GOTO considered harmful!", let's see how Conditional Branching is to be used in a computer program.


Remember that computer program lies in Memory, and that is in reality just a sequence of numbers in sequential memory addresses. Computers don't really distinguish code and data, unless the hardware store each part separately. We're not concerned about that, though.


The simplest GOTO is Unconditional Branching. This is true if the Conditional evaluates a constant. In other words, either it is always True or always False. Or maybe, the Conditional isn't even checked. It has very limited usage, mainly to connect to a subroutine somewhere else. Reason maybe to simplify the main code, try to fit in the code more than what relative jump can cover, or provide hooks to be modified to point to updated code later. It can also be used to provide Infinite Loop. Lastly, it can also be abused to provide "Spaghetti Code" style of coding.


Therefore, Conditional Branching imply variables in the Conditional. This can be True or False, dependent on the variables. There's a whole section of Boolean algebra regarding this, but let's not go there for now. The main consideration here is that we can skip Forward, or Loop back.


Skipping Forward is usually used to provide interactive process. IF Something THEN Do This Else Do That. 


IF (TRUE) THEN DO THIS. 
IF (FALSE THEN DO THIS.
IF (TRUE) THEN DO THIS ELSE DO THAT

IF (TRUE) THEN DO THIS
ELSE IF (TRUE) THEN DO THIS
ELSE IF (TRUE) THEN DO THIS
ELSE DO THAT


The second example is called "IF-ELSE" ladder, and you can chain them for quite a long chain. There is something similar to this structure: ON...GOTO


ON (var) GOTO 1,2,3,...,N
ON (var) GOSUB 1,2,3,...,N


In C, we use switch() { case 1: ... default; } construct. The idea is that we execute different code depending on the value of variable, instead of TRUE/FALSE. Generally speaking, this is a concept of Multiplexer/Demultiplexer. 


Next, consider IF-GOTO to the preceeding address. This is called a LOOP. Easy way is Infinite LOOP.


LOOP start:
...
...
GOTO start:


You can use the loop to repeat N times:


REPEAT
...
...
UNTIL N=10


In C, we use do { } while () construct.


You can loop as long as a condition remains True


WHILE (cond)
...
...
WEND


In C, we use while () { } construct.


There is also FOR LOOP, but it's not interesting for the discussion. What is interesting are the keywords *break* and *continue*. CONTINUE is used to skip the rest of the loop and go to start immediately, while BREAK is used to BREAK out of the loop immediately. You can think of it as GOTO Start and GOTO End.


LOOP Start:
...
IF (cond) GOTO End: #break
...
IF (cond) GOTO Start #continue
...
GOTO Start
End:  #End of loop


And that should be it. There is also the Spaghetti Code style where you just go everywhere whenever you want. Such pattern are confusing and should be avoided whenever possible, but if you ever try to build a parser, maybe by using lex and yacc, then it can't be helped. State based or Functional coding is like that, too.


No comments:

Post a Comment