Page 139 - ARM 64 Bit Assembly Language
P. 139

Structured programming 125

                     them. It is up to the programmer to decide how to pass arguments to the subroutines and how
                     to pass return values back to the section of code that called the subroutine. Once again, the ex-
                     pert assembly programmer will use these structured programming concepts to write efficient,
                     readable, debugable, and maintainable code.


                     5.4.1 Advantages of subroutines

                     Subroutines help programmers to design reliable programs by decomposing a large problem
                     into a set of smaller problems. It is much easier to write and debug a set of small code pieces
                     than it is to work on one large piece of code. Careful use of subroutines will often substan-
                     tially reduce the cost of developing and maintaining a large program, while increasing its
                     quality and reliability. The advantages of breaking a program into subroutines include:
                     •   enabling reuse of code across multiple programs,
                     •   reducing duplicate code within a program,
                     •   enabling the programming task to be divided between several programmers or teams,
                     •   decomposing a complex programming task into simpler steps that are easier to write, un-
                         derstand, and maintain,
                     •   enabling the programming task to be divided into stages of development, to match various
                         stages of a project, and
                     •   hiding implementation details from users of the subroutine (a programming principle
                         known as information hiding).


                     5.4.2 Disadvantages of subroutines


                     There are two minor disadvantages to using subroutines. First, invoking a subroutine (versus
                     using in-line code) imposes overhead. The arguments to the subroutine must be put into some
                     known location where the subroutine can find them. If the subroutine is a function, then the
                     return value must be put into a known location where the caller can find it. Also, a subroutine
                     typically requires some standard entry and exit code to manage the stack and save and restore
                     the return address.

                     In most languages, the cost of using subroutines is hidden from the programmer. In assem-
                     bly, however, the programmer is often painfully aware of the cost, since they have to explicitly
                     write the entry and exit code for each subroutine, and must explicitly write the instructions
                     to pass the data into the subroutine. However, the advantages far outweigh the costs. Assem-
                     bly programs can get very large and failure to modularize the code by using subroutines will
                     result in code that cannot be understood or debugged, much less maintained and extended.
   134   135   136   137   138   139   140   141   142   143   144