Class Notes
CS 250
Part IV: Macros and Procedures
1
1. Macro
1.1 Definition
A macro is a user defined instruction op code. The source code for a macro begins with
MACRO directive and ends with ENDM directive.
macroname MACRO { param {{, param}}}
…
…
macroname ENDM
where macroname is the name (op code) to be assigned to the macro, and each specified
param is a parameter of that macro. A parameter can represent any operand or part of an
operand in any instruction within the macro. A parameter could be a register name, a
memory address, an assembler identifier, or an immediate value.
A macro can be invoked in a program by using its name as an instruction op code:
macroname {arg {{, arg}}}
where arg is a macro reference argument. There must be exactly the same number of
arguments as the number of parameters defined in the macro. The assembler will copy
the text of the named macro into the source code at the which it is referenced, and replace
each parameter with the corresponding argument.
For example,
a. _Begin MACRO
mov ax, @data
mov dx, ax
_Begin ENDM
can be used to replace
mov ax, @data
mov dx, ax
by _Begin
b. _Set_seg_value MACRO, SREG, VALUE
2
mov ax, VALUE
mov SREG, ax
_Set_seg_value ENDM
The above macro will set any given segment register equal to any given value. If the
following instruction
_Set_seg_value es, alpha
appeared in a program, then the assembler would process that instruction as if it read
mov ax, alpha
mov es, ax
1.2 Placement of Macros
1) positioning macros within a source file
Macro definitions can be positioned anywhere within the source code of a
program (module).
2) positioning macros into a separate file (e.g. PCMAC.INC)
It is convenient to put all useful macros into a file. In this case, the INCLUDE
directive must be inserted at the beginning of the program.
INCLUDE filename
where the filename is the file for macros
The INCLUDE directive instructs the assembler to lift the text of an assembly
language source file off the disk and to logically insert its contents into the source
code of the file.
PCMAC.INC includes a number of macros: _Begin, _Putstr Message, _Exit …
The first example can be rewritten as
3
The following is some macros included in the PCMAC.INC
; _Begin ; Generates boilerplate code to start
execution of a program.
; _PutStr Label ; Display string at label terminated
by the ‘$’ character (decimal 36)
Destroys AX and DX
; _PutCh ; Display a sequence of characters
Destroys AX and DX
; _Exit ; Return to DOS with return code.
; _GetDate ; After the call;
; al = weekday (0 to 6)
; ah = month (1 to 12)
; dl = day (1 to 31)
; cx = year (e.g. 1984)
2. Procedure (subroutine)
4
A procedure is defined by
Procname PROC
….
ret
Procname ENDP
where ret is the last instruction in the procedure, PROC and ENDP is a pair of directives
to define a procedure (subroutine).
2.1 The call instruction
The call instruction is used to invoke the code in a subroutine. The general format is:
Call procname
where procname is the name of the procedure to be called.
The CPU does several things in the execution of a call instruction. First, it records (push)
the current content of IP register into program stack. Then it set the IP to the location of
the first instruction in the procedure.
2.2 The ret instruction
The last instruction in a procedure must be ret instruction. A ret instruction transfer the
program flow back from a subroutine to its parent. When the CPU executes the ret
instruction, it POP the address from the program stack into IP register.
2.3 Placement of Procedures
1) Place procedures within a program. The main routine should normally come first,
the other subroutines can be placed in anywhere.
2) Place all subroutines into a separate file called library as external procedures. An
external procedure is a procedure that is not defined within the program that calls
it. Instead, it is written as separate file and is then assembled to obtain an object
file. A program which uses these procedures must include the following EXTRN
directive immediately after .CODE:
.CODE
EXTRN GetDec: NEAR, PutDec: NEAR…
5
Procname PRO
…
where GetDec and PutDec are two procedures included in the library. Note that
all procedures in the library are object (OBJ) files (have been assembled). The
EXTRN will tell the linker how to find the already assembled procedures in
another file and link it with the current program.
Some number I/O procedures in the library are listed below.
Example: write a program to display today’s date in the form: Today is mm/dd/yyyy
INCLUDE PCMAC.INC ; including macros
.MODEL SMALL
.586 ; allow using Pentium instructions
.STACK 100h ; set 100h words for stack
.DATA
Message DB ‘Today is $ ‘
.CODE
Display_today PROC
EXTRN PutDec: NEAR ; PutDec is an external procedure
_Begin
_PutStr Message
_GetDate
push cx ; push year
6
push dx ; push month and day
_PutStr Message
pop ax
mov bx, ax
mov al, bh
mov ah, 0 ; extend to word
call PutDec ; display month number
_Putch ‘/‘ ; put ‘/’ after mm
mov al, bl ; select day number
mov ah, 0 ; extend to word
call PutDec ;display day number
_Putch ‘/‘ ; put’/’ after dd
pop ax ; po
call PutDec ; display year
_Putch 13,10
_Exit 0 ; return to Dos
Display_today ENDP
END Display_today ; tell where to start excution
Note: when using external procedures, until the program must be linked using
link filename, , , util; or
tlink filename, , , util or
ml filename.asm util.lib
7