A macro instruction in computer programming is a single instruction that, when a program is compiled, is replaced by a pre-defined block of code. The term was originally applied to instructions used in assembly language programming, in which long, repetitive code was sometimes needed for very simple operations. The development of macro instructions was one of the first steps toward creating a language based on a library of representative functions so minute actions could be collected together under a single command call in a separate program. Early macro instruction libraries were most frequently supplied by the manufacturers of hardware and software to help programmers use and access features in the correct way.
Programmers used macro instructions for a variety of reasons, but the most common were to reduce the amount of repetitive code in the program, eliminate the possibility of error within the macro code and provide commonly used instruction sets so several programmers would be able to use the same macro instruction. Under assembly language, every single step required to achieve a certain result has to be explicitly written. An example could be a program that added three numbers together.
The addition operator in higher-level languages is often taken for granted, but the operation of adding three numbers in assembly language requires at least six separate lines of code. The first two lines load the numbers into fields called registers that actually correspond to physical circuits inside the computer. The third line adds the two registers and a fourth line stores the result in another register. The fifth line loads the last number to be added into a register, and the final statement adds the previous result to the third number.
This sequence of operations can easily be turned into a macro instruction so only one line of code is needed, along with the three numbers to be added as parameters. When the program is finally compiled by an assembler and turned into machine code, actions called pre-processing are taken before the assembly takes placed. The pre-processor takes the macro instruction and parameters and expands it into the required lines of code, substituting the parameters for representative placeholders inside the actual macro code.
After macro instruction use became widespread, many commonly employed code blocks would be assembled into macro instruction libraries. These libraries contained several macros to help simplify programming in assembly language as well as ensuring that certain operations were performed in a consistent manner across different programs. The extensive use of macro instruction libraries eventually led to higher-level languages that relied on their own libraries to provide extensive functionality with much less repetitive programming requirements.