Page 127 - ARM 64 Bit Assembly Language
P. 127

CHAPTER 5

                     Structured programming




                     Before IBM released FORTRAN in 1957, almost all programming was done in assembly
                     language. Part of the reason for this is that nobody knew how to design a good high-level lan-
                     guage, nor did they know how to write a compiler to generate efficient code. Early attempts at
                     high-level languages resulted in languages that were not well structured, difficult to read, and
                     difficult to debug. The first release of FORTRAN was not a particularly elegant language by
                     today’s standards, but it did generate efficient machine code.

                     In the 1960s, a new paradigm for designing high-level languages emerged. This new paradigm
                     emphasized grouping program statements into blocks of code that execute from beginning
                     to end. These basic blocks have only one entry point and one exit point. Control of which
                     basic blocks are executed, and in what order, is accomplished with highly structured flow con-
                     trol statements. The structured program theorem provides the theoretical basis of structured
                     programming. It states that there are three ways of combining basic blocks: sequencing, selec-
                     tion, and iteration. These three mechanisms are sufficient to express any computable function.
                     It has been proven that all programs can be written using only basic blocks, the pre-test loop,
                     and if-then-else structure. Although most high level languages provide additional flow con-
                     trol statements for the convenience of the programmer, they are just “syntactical sugar.” Other
                     structured programming concepts include well-formed functions and procedures, pass-by-
                     reference and pass-by-value, separate compilation, and information hiding.
                     These structured programming languages enabled programmers to become much more pro-
                     ductive. Well-written programs that adhere to structured programming principles are much
                     easier to write, understand, debug, and maintain. Most successful high-level languages are de-
                     signed to enforce, or at least facilitate, good programming techniques. This is not generally
                     true for assembly language. The burden of writing well-structured code lies with the program-
                     mer, and not the language.

                     The best assembly programmers rely heavily on structured programming concepts. Failure to
                     do so results in code that contains unnecessary branch instructions and, in the worst cases, re-
                     sults in something called spaghetti code. Consider a code listing where a line has been drawn
                     from each branch instruction to its destination. If the result looks like someone spilled a plate
                     of spaghetti on the page, then the listing is spaghetti code. If a program is spaghetti code, then
                     the flow of control is difficult to follow. Spaghetti code is much more likely to have bugs, and


                     ARM 64-Bit Assembly Language
                     https://doi.org/10.1016/B978-0-12-819221-4.00012-2  113
                     Copyright © 2020 Elsevier Inc. All rights reserved.
   122   123   124   125   126   127   128   129   130   131   132