branching (original) (raw)


music

| OSdata.com: programming text book | | OSdata.com | | ---------------------------------------------------------------------------------------------- | | ---------- |

summary

This subchapter looks at branching.

branching

This subchapter looks at branching. Don’t do anything you see in this subchapter!

Direct branching was a huge nightmare in early programming. This nightmare was often called “spaghetti programming”, because attempting to follow the logic of a program was often like trying to follow strands of spaghetti in a bowl.

This problem led directly to the introduction of structured programming and the loop control structures you have just learned.

So, why am I telling you about this?

You might use an older programming language, such as APL, which requires that you use direct branching to create the looping structures. You might program at machine or assembly level, where again you have to use unconditional or conditional branching to create your own control structures. Or you might encounter old software where you have actually trace through the spaghetti code for yourself.

The most common version of this command is the classic GOTO. In some languages you have the option as to whether you write it as GOTO or GO TO.

B — APL monadic function of the form is →B that transfers control to the statement numbered B.

GOTO label — BASIC command that transfers control to the labelled statement. Labels in BASIC are numbers.

goto label — C statement that transfers control to the labelled statement. The goto must transfer control to a label that is in the current function.

GOTO label — FORTRAN command that transfers control to the labelled statement. Labels in FORTRAN are numbers.

goto label — Pascal command that transfers control to the labelled statement. Labels in Pascal are numbers from 0 to 9999 inclusive, which must be declared.

GO TO label — PL/I command that transfers control to the labelled statement. May be abbreviated GOTO.

assembly language instructions

control registers

Control registers control some aspect of processor operation. The most universal control register is the program counter.

program counter

Almost every digital computer ever made uses a program counter. The program counter points to the memory location that stores the next executable instruction. Branching is implemented by making changes to the program counter. Some processor designs allow software to directly change the program counter, but usually software only indirectly changes the program counter (for example, a JUMP instruction will insert the operand into the program counter). An assembler has a location counter, which is an internal pointer to the address (first byte) of the next location in storage (for instructions, data areas, constants, etc.) while the source code is being converted into object code.

The VAX uses the 16th of 16 general purpose registers as the program counter (PC). Almost the entire instruction set can directly manipulate the program counter, allowing a very rich set of possible kinds of branching.

The program counter in System/360 and 370 machines is contained in bits 40-63 of the program status word (PSW), which is directly accessible by some instructions.

processor flags

Processor flags store information about specific processor functions. The processor flags are usually kept in a flag register or a general status register. This can include result flags that record the results of certain kinds of testing, information about data that is moved, certain kinds of information about the results of compations or transformations, and information about some processor states. Closely related and often stored in the same processor word or status register (although often in a privileged portion) are control flags that control processor actions or processor states or the actions of certain instructions.

A few typical result flags (with processors that include them):

Some conditions are determined by combining multiple flags. For example, if a processor has a negative flag and a zero flag, the equivalent of a positive flag is the case of both the negative and zero flags both simultaneously being cleared.

A few typical control flags (with processors that include them):

See also Registers

program control instructions

Program control instructions change or modify the flow of a program.

The most basic kind of program control is the unconditional branch or unconditional jump. Branch is usually an indication of a short change relative to the current program counter. Jump is usually an indication of a change in program counter that is not directly related to the current program counter (such as a jump to an absolute memory location or a jump using a dynamic or static table), and is often free of distance limits from the current program counter.

The pentultimate kind of program control is the conditional branch or conditional jump. This gives computers their ability to make decisions and implement both loops and algorithms beyond simple formulas.

See also Program Control Instructions in Assembly Language


free music player coding example

Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

View music player in action: www.musicinpublic.com/.

Create your own copy from the original source code/ (presented for learning programming).

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.