Macros and Subroutines

Macros and Subroutines

Copyright: © 2017 |Pages: 23
DOI: 10.4018/978-1-68318-000-5.ch004
OnDemand PDF Download:
$30.00
List Price: $37.50

Chapter Preview

Top

Subroutines

A subroutine is a set of instructions designed to perform a specific and usually repetitive task. The user’s program may execute the subroutine simply by “calling” its name. Upon termination of the subroutine’s chore, the CPU resumes execution of remaining jobs. The main advantages of subroutines in programming languages are:

  • Staying away from re-inventing the wheel. In fact many useful and elaborate routines have been written by other programmers and are available to “cut-and-paste” free of charge. This speeds up software development and frees the programmer from the burden of re-designing a piece of code from scratch.

  • The utilization of subroutines makes programs more compact since an instruction sequence does not have to be reproduced over and over if one intends to perform the same task repetitively. The logical thing to do is to design a subroutine and invoke it as needed in a program.

  • Subroutines make programs more readable for they divide big tasks into smaller ones that the user can easily trace. For instance, someone interested in designing a digital thermostat would have to perform the following steps:

    • o

      Read the temperature from a sensor.

    • o

      Display it on a Liquid Crystal Display (LCD).

    • o

      Compare it against a set point for HVAC control.

The programmer may then design three autonomous subroutines namely: ReadTemp, DispTemp and Control. This way if a fault is detected in one of these software modules, it can be corrected independently of the other modules. Thus programmers can concentrate on debugging one subroutine at a time, reminiscent of how humans normally tackle life difficulties: “I will cross that bridge when I get to it”.

A macro is a set of instructions framed between the two assembler directives: macro and endm. It allows programmers to extend the instruction set to handle more complex operations such as 16-bit manipulations, BCD arithmetic, etc. When the assembler encounters a macro call, it inserts the instructions constituting the macro into the program. This is not the case for subroutines; instructions forming a subroutine have only one occurrence in the program and are executed upon demand.

Macros allow programmers to hide certain intricacies inside software blocks and hence they give programs a compact appearance and improved readability. In fact, tracing and debugging a long program is rather overwhelming whereas a concise and structured program attracts its reader and is easier to trace. In upcoming sections, macros and subroutines will be developed with the following goals in mind: instruction set extension, program compactness and improved readability.

Top

Basic Macros

MPASM2 assembler macros have the following generic syntax:

where <macroname> is a valid assembler label used to invoke the macro, and <argi> is any number of optional arguments supplied to the macro. Hence a macro allows the user to pass parameters as in high level languages. The endm directive terminates the macro. Note that macros must be defined before the org directive in order for the program to invoke them when needed.

Complete Chapter List

Search this Book:
Reset