Regularity
From Maze
Contents[hide] |
Iteration in 10 Print
The 10 Print Loop (GOTO)
As described elsewhere in this book (tk where specifically), one of the key aspects to 10 Print's functionality is its repetition of the print statement – repetition that will continue until either the user manually breaks out of the program or the Commodore 64 it is running on shuts off. (tk finish based on how the descriptions are handled elsewhere.)
Looping and Iterating
Programming languages contain two basic types of loops: finite and infinite. 10 Print is an example of an infinite loop because there is no programmatic condition under which it can be exited. As an intentionally infinite loop it is one of a relatively rare breed; though there are a few tasks that are best handled by writing an infinite loop, when a programmer typically thinks about infinite loops they are something to avoid at all costs. Infinite loops not only prevent a program from executing any code outside of the loop but also can occupy a processor and prevent other programs from running.
On the Commodore 64 and most other computers of its era the latter was not a problem as the processor was not capable of running multiple programs simultaneously in any case. The lack of any way out of an infinite loop was also not really a problem on the Commodore 64 since it had a dedicated 'break' key that allowed the user to interrupt any program it was attempting to run. In a one-line program like 10 Print there is neither space nor need for the kind of flow control found in longer programs, and without the other downsides normally associated with an infinite loop there is no reason use a more complex structure. An intentionally infinite loop is usually written when a program is intended to repeat the same action over and over without any significant change to that action being made over time.
Finite loops are exactly what the name would indicate: loops that are designed to end under certain conditions. The exact conditions vary; some will continue until the program reaches a pre-defined exit state while others execute a specific number of times no matter what the result. No matter what the exit conditions are for a specific loop, the fact that there are exit conditions at all implies the programmer is expecting some kind of change in the program to be introduced as a result of the loop's execution.
A common use of finite loops is to create an iterative process. Iteration is a special type of loop where the result of one pass through the loop influences the result of succeeding passes. The simplest example of an iterative process is nothing more than counting. Beginning with an initial value, 1, and adding 1 to it to produces 2. Taking the output from that operation, 2, and performing the same operation again, adding 1, results in 3, and so on until the sequence 1, 2, 3… emerges. Counting to 10, 100, or for one hour no matter what number you end up at are all finite iterative processes; counting until you pass out is an infinite iterative process, even if you never quite make it to infinity.
10 Print Iterates? (POS)
Based on this description of iteration, it may not be entirely clear that 10 Print is an example of iteration. There are no variables in the code that change value from one pass to the next–there are no variables at all–and the functionality of the code inside the loop never changes. Watching the program execute on screen shows hints that there is something changing beneath the surface, though: the position of the cursor changes every time the loop executes, moving one position to the right.
The position of the cursor is not being managed by 10 Print itself; it is a function of the Commodore 64's kernal, a layer of software that lies between BASIC and the machine code executed by the processor to handle input/output functions. Since 10 Print's BASIC code is not controlling the iterative aspect of its function it would be accurate to say that 10 Print is operationally iterative, but the BASIC code is not. This should not preclude examination of 10 Print's iterative nature, however; many iterative processes are dependent upon external or implicit factors influencing their output, and 10 Print's simplicity exposes the power of using simple iterative steps to create a complex construction.
(tk not sure if it would be confusing here to describe RND as an iterative process since it is deterministic...RND(1) is little different than iterating through a stored list of numbers...)
FOR…TO…STEP
(tk add variant that rewrites 10 Print to manually manage the cursor position by poking specific grid locations using BASIC's built-in iteration structure.)
Iteration Permeating Computing
Iteration – transistors and processors
If you zoom in far enough all a computer ever does is run one iterative loop after another, up to five hundred thousand times per second in the Commodore 64's MOS Technologies 6510 processor and billions of times per second in a modern chip. The iterations are not just restricted to processors though: almost every component in the computer, including memory, input/output controllers, and video adapters are all constantly iterating, no matter what code the programmer writes or the user runs. Digital logic is based on the idea that general-purpose instructions can be modeled using nothing more than ones and zeros, but the ones and zeros must themselves be modeled somehow before they can turn into digits, logic, or characters on a screen.
Computers do one thing well…
At a literally nanoscopic level, a computer is little more than a collection of switches. Lots of switches, really: a few hundred per square millimeter in the Commodore 64's day, and millions per square millimeter using today's most advanced lithographic techniques. (tk footnote Moore's Law?) But unlike a light switch, the transistors used as switches in digital devices are not operated by physically flipping a conductor to complete a circuit. Transistors are solid-state electronics made of semiconducting materials that, depending on an input signal, either allow or prevent the flow of current through a junction. The difference between no current and full current is thought of as the difference between a zero and a one, a false and a true, or an off and an on.
As they are built largely from transistors, computers are devices that do one thing very, very well: they can detect the difference between no current and some current. Despite the power represented by making millions of simple decisions per second, this principle of operation also presents a problem. With no physical components to move and create or break a circuit, a single transistor will not "remember" whether it should be allowing current to flow or not without a constant input signal. Without that bit of memory, a transistor can only ever allow a signal to pass through it; if it is to be useful in logic, it must somehow be made to capture the output of that signal and combine it with the signals coming from other transistors on the chip to perform complex operations.
Memory and CPUs
Depending on which is best for the application at hand, there are several ways to construct a circuit around a transistor that will allow it to behave as a useful logical switch. The most basic is to simply offload that responsibility to another part of the integrated circuit. This approach is often taken in parts of a microprocessor that are intended to carry out the actual work of computing, such as the arithmetic logic unit (ALU). An ALU is a series of transistors organized into logic gates – simple circuits that compare two input voltages and produce an output based on comparing them to see if they are the same or different. (tk footnote Boolean logic) The simple logic gates are ganged together to allow multiple inputs to undergo relatively complex operations and produce multiple outputs. Within the gates, though, the data is simply used and then lost, transformed into an output.
In practice, an ALU is rarely complex enough to accomplish large tasks in a single pass. Since the logic gates must be reused to calculate the results of subsequent passes, storing the results of the first pass is relegated to an accumulator. The accumulator in the Commodore 64's 6510 chip is a memory register that holds the results of a previous calculation until they are fed back into the ALU as one of its inputs for a future calculation. This process, which takes place entirely at the level of transistors, is an iterative function: a process that repeats continuously, using the output of previous passes as an input for the next one.
Pushing storage off into a register does not truly solve the problem of how to make transistors remember states though; it only moves it to another part of the chip. Inside the registers is where the problem must be truly solved, and again the answer uses an iterative loop. The same logic gates that perform calculations in the ALU can be reconfigured into a circuit called a flip-flop. There are many different types of flip-flops, but what they have in common is that they use several transistors to create a positive feedback loop where the state of each transistor switch is reset every clock cycle to be equal to what it was in the previous cycle. An external voltage drives the entire loop and the state of the output is only changed when the state of an input changes. Though it appears from the outside that the state of the flip-flop is constant, the iterative cycle of setting a high or low output voltage based on whether the voltage was high or low in the previous cycle is what gets around the lack of a physical switch. (tk need to specify latches?)
Further away from the heart of the processor, another type of memory also uses iterative cycles to simulate a constant state. Dynamic Random Access Memory (DRAM) is a simpler but slower solution that uses far fewer transistors than the flip-flops in the processors. Instead, DRAM fills a capacitor with electrons to represent a high state. The capacitor quickly discharges though, returning it to a low state. Before it can discharge completely, control circuitry checks to see if it was full on the previous clock cycle and immediately refills the capacitor, effectively creating a loop that holds a state until the memory loses power.
Example: Adding with an Accumulator
(This section will be done with a diagram.)
Iteration – code and structures
"Go To Statement Considered Harmful"
(tk introduce the letter)
In the case of 10 Print the GOTO statement avoids most of Dijkstra's complaints. Putting a GOTO statement at the end of a one-line program that simply references itself does not leave much room for unexpected entrances and exits. However, expanding the program to take more than one line demonstrates the potential problems that GOTO introduces:
5 CHARCODE%=205.5+RND(1) 10 PRINT CHR$(CHARCODE%); 20 GOTO 5
This code is functionally identical to the one-line version of 10 Print, but is a bit more representative of how developers create more complex programs. Line 5 creates a variable called CHARCODE% that stores a character code determined by the same formula used in the original 10 Print, but the character code is not used until line 10 when it is printed to the screen. Line 20 has the GOTO statement that makes the program iterate, but it now points to the new beginning of the program at line 5.
Already there is one potential problem: what if the programmer had forgotten to change GOTO's target from line 10 to line 5? The program would still function, but since the loop only returns to line 10 CHARCODE% never changes and the program only prints the same character over and over again with no randomization. Nowhere in this code does it say that the computer must select a random character code every time it prints a character, it is only implied by the fact that line 10 comes after line 5. GOTO allows the programmer to reference any line of code, however; it is just as valid to say GOTO 10 as it is GOTO 5, regardless of the intention that line 10 always executes right after line 5. GOTO's ability to target arbitrary line numbers means that sections of code that are intended to be monolithic can be entered at any point instead of just the beginning of the block.
GOTO's potential problems only get worse when it is combined with other flow control statements. Consider this variant of 10 Print:
1 FOR I FROM 0 TO 999 5 CHARCODE%=205.5+RND(1) 10 POKE 1024+I,CHARCODE% 15 NEXT I 20 GOTO 1
This version uses a FOR loop to fill the entire screen with the two 10 Print characters, beginning at the top-left corner. Immediately after finishing the FOR loop the program hits line 20 and GOTO sends execution back to line 1, restarting the loop from the beginning. If the forgetful programmer again fails to update line 20 though, leaving it as:
20 GOTO 5
The program will now not only fail to work as intended, it will fail to work at all and throw an error:
? NEXT WITHOUT FOR ERROR IN 15
Unlike the previous version where lines 5 and 10 were only implied to be a block, lines 1-15 are now explicitly a block of code defined as a FOR loop. NEXT relies on FOR, but since the block was re-entered on line 5 FOR never executes and NEXT is no longer valid.
