- Why goto statements exist in languages.
- How to create logical (or boolean) variables in assembly.
- The basic control structures used in structured programming, and how to translate them into assembly code. The basic control structures covered are:
- if statements
- if-else statements
- if-elseif-else statements
- sentinel control loops
- counter control loops.
- How to calculate branch offsets.
The structured programming paradigm is built on the concept that all programs can be built using just 3 types of program control structures. These structures are:
- Sequences that allow programs to execute statements in order one after another.
- Branches that allow programs to jump to other points in a program.
- Loops that allow a program to execute a fragment of code multiple times.
Most modern HLLs are implemented based on these 3 program control structures, with some notable extensions such as Java exception handling, continue, and break statements.
But as was pointed out in Chapter 5 on simple subprogram execution, the only way to control program execution sequence in assembly language is through the
$pc register. Therefore in assembly there are no native structured program constructs. This does not mean that an assembly language programmer should abandon the principals of structured programming. What the lack of language based structured programming constructs means is that the assembler programmer is responsible for writing code which aligns with these principals. Not following structured programming principals in assembly is a sure way to create spaghetti code, or code where control is passed uncontrolled around the program, much like spaghetti noodles intertwine and following individual strands becomes difficult.
This chapter will introduce into pseudo code structure programming control structures similar to those in Java/C/C++/C#. Programmers familiar with those languages should be able follow the programs with no problems. The text will then show how to translate each control structure from pseudo code into assembly.
All programs in this chapter will be preceded by a pseudo code implementation of the algorithm. Translation from the pseudo code into MIPS assembly will be shown, with the program always following the translations. No programs will be developed directly into assembly. The reason for this is though the direct implementation of the programs in assembly allows more flexibility, programmers in assembly language programming often implement these programs in very unstructured fashions which often result in poor programs and can develop into poor understanding and practices.
To reemphasize the point, it has been the experience of the author that although many new assembly language programmers often try to avoid the structured programming paradigm and reason through an assembly language program, the results are seldom satisfactory. The reader is strongly advised to follow the principals outlined in this chapter, and not attempt to develop the programs directly in assembly language.