Chapter 7
Chapter 7
Chapter 7
– CALL NameOfTheProcedure
• The parts which make a procedure are:
– Declaration of the procedure
– Code of the procedure
– Return directive
– Termination of the procedure
proc_name PROC
statement1
statement2
. . .
Statementk
RET
proc_name ENDP
• For example, if we want a routine which adds two bytes stored
in AH and AL each one, and keep the addition in the BX
registers:
Adding Proc Near ; Declaration of the procedure
Mov Bx, 0 ; Content of the procedure
Mov Bl, Ah
Mov Ah, 00
Add Bx, Ax
Ret ; Return directive
Adding Endp ; End of procedure declaration
• On the declaration the first word, Adding, corresponds to the
name of out procedure, Proc declares it as such and the word
Near indicates to the TASM that the procedure is intra-segment.
The Ret directive loads the IP address stored on the stack to
return to the original program, lastly, the Adding Endp directive
indicates the end of the procedure.
• To declare an inter-segment procedure we substitute the word
NEAR for the word FAR.
• The calling of this procedure is done the following way:
Call Adding
2. Macros
• A macro is a repetitive instruction in a program which is codified
only once and can be used as many times as necessary.
• It is also a symbolic name given to one or more assembly language
statements. A macro may be used to generate instructions or data
definitions.
Syntax of macro definition (for MASM and TASM):
• The parts which make a macro are:
– Declaration of the macro
– Code of the macro
– Macro termination directive
The declaration of the macro is done the following way:
macro_name MACRO d1, d2, . . . , dn
statement1
statement2
ENDM
• Where d1, d2, . . . , dn is an optional list of dummy
parameters (it is possible to create a macro which has no
parameter). Dummy parameters are temporary variables;
they are not declared by data definition directives (DB, DW,
DD, . . .). They can be used as input as well as output
parameters.
• A macro definition can appear anywhere in an assembly
language program before the END directive; however for
MASM and TASM the definition of a macro must appear
before a call to that macro and a macro definition cannot
appear inside another macro definition.
It is usual to place all macro definitions at the beginning of a
program before the segment definitions.
A macro definition is not assembled until the macro is called.
Each macro call is replaced by the statements in the macro
definition (i.e., each call expands the macro). The directive for
the termination of the macro is: ENDM
• Syntax of a macro call:
macro_name a1, a2, . . . , aN
Where a1, a2, . . . , aN is an optional list of actual arguments. An actual argument can be
an immediate value (i.e., a constant), a variable, or a register name. Argument names may
be duplicates of other names (labels, variables, etc.).
DISPLAY_CHAR MACRO CHAR
PUSH AX
PUSH DX
MOV AH , 02H
MOV DL , CHAR
INT 21H
POP DX
POP AX
ENDM
. . .
.CODE
...
DISPLAY_CHAR ‘A’
...
MOV BL , ‘X’
DISPLAY_CHAR BL
...
READ_STRING MACRO BUFFER_NAME
PUSH AX
PUSH DX
MOV DX , OFFSET BUFFER_NAME
MOV AH, 0AH
INT 21H
POP DX
POP AX
ENDM
. . .
.DATA
INPUT_BUFFER DB 71, 72 DUP(?)
...
.CODE
...
READ_STRING INPUT_BUFFER
...
SWAP MACRO WORD1 , WORD2
;;Exchanges two word memory operands
PUSH AX
MOV AX , WORD1
XCHG AX , WORD2
MOV WORD1 , AX
POP AX
ENDM
. . .
.DATA
VAR1 DW 2FE4H
VAR2 DW 5000H
. . .
.CODE
. . .
SWAP VAR1 , VAR2
DISPLAY_STRING MACRO STRING
PUSH AX
PUSH DX
MOV AH , 09H
MOV DX , OFFSET STRING
INT 21H
POP DX
POP AX
ENDM
. . .
.DATA
PROMPT1 DB ‘ID# ?’, 0DH , 0AH , ‘$’
PROMPT2 DB ‘NAME ?’ , 0DH , 0AH , ‘$’
. . .
. CODE
. . .
DISPLAY_STRING PROMPT1
. . .
DISPLAY_STRING PROMPT2
Invalid macro calls:
A macro call is invalid if the macro expansion for that call produces an invalid assembly
language statement.
Example, consider:
MOVW MACRO WORD1 , WORD2
;; Copies one word to another
MOV WORD1 , WORD2
ENDM
.DATA
VAR1 DW 12ACH
VAR2 DW 6200H
. . .
The call:
MOVW VAR1, VAR2
Generates the code:
MOV VAR1, VAR2
This is invalid, because it is a direct memory to memory transfer.
The call:
MOVW VAR2, BL
Generates the code:
MOV VAR2, BL
This is invalid because the operands are of different sizes
A macro calling another macro
• For MASM and TASM a macro can contain calls to other previously defined
macros. A macro X may contain calls to another macro Y defined later in the
program provided that the call to the macro X appears after both macro
definitions. Thus, provided all macro definitions appear before the segment
definitions, the macros may be written in any order.
• Example:
DISPLAY_CHAR MACRO CHAR
PUSH AX
PUSH DX
MOV AH , 02H
MOV DL , CHAR
INT 21H
POP DX
POP AX
ENDM
CRLF MACRO
DIPLAY_CHAR 0DH
DISPLAY_CHAR 0AH
ENDM
A macro calling a procedure
A macro can contain calls to one or more procedures.
Example:
MDSPLY_STRING MACRO STRING
PUSH DX
MOV DX , OFFSET STRING
CALL DSPLY_STRING
POP DX
ENDM
. . .
.DATA
MESSAGE1 DB ‘Hello Ethiopia ’, 0DH, 0AH, ‘$’
MESSAGE2 DB ‘Hello Hossana ’, 0DH, 0AH, ‘$’
. . .
.CODE
MAIN PROC
. . .
MDSPLY_STRING MESSAGE1
MDSPLY_STRING MESSAGE2
. . .
MAIN ENDP
DSPLY_STRING PROC
PUSH AX
MOV AH, 09H
INT 21H
POP AX
RET
DSPLY_STRING ENDP
END MAIN
The LOCAL directive for generating unique
labels in a macro expansion
• A macro with loops or jumps contains one or more labels.
If such a macro is called more than once in a program,
duplicate labels appear when the macro calls are
expanded, resulting in an assembly error. Similarly a macro
containing a data definition, if called more than once, will
result in an assembly error. This problem can be avoided by
using local labels or local identifiers in the macro. To
declare them, we use the LOCAL directive, whose syntax is:
• LOCAL list-of-labels
Where list-of-labels is a list of labels and/or identifiers
separated by commas. The LOCAL directive must be the first
statement after the MACRO directive. Not even a comment
can separate a MACRO directive and a LOCAL directive.
• Every time a macro is expanded, MASM or TASM assigns a unique label to each
local label or identifier in each expansion. These labels are of the form:
??d1d2d3d4
• Where di is a hexadecimal digit. Thus the labels range from ??0000 to ??FFFF.
Since a local label or identifier is assigned the
• next unique label from the list ??0000 - ??FFFF in each macro expansion, a
local label or identifier may have the same name as a global label or a global
identifier (i.e. a label or identifier defined outside a macro).
Example:
MIN2 MACRO FIRST, SECOND
LOCAL END_IF
;; Puts minimum of two signed words in the AX register
MOV AX, FIRST
CMP AX, SECOND
JLE END_IF
MOV AX, SECOND
END_IF:
ENDM
• This macro returns, in the AX register, the minimum of
arguments corresponding to the parameters FIRST and
SECOND. The contents of the register AX are compared to the
parameter SECOND, if AX is less or equal to SECOND a jump is
taken to the label END_IF; otherwise the instruction MOV AX
, SECOND is executed.
Assuming that call:
MIN2 VAR1, VAR2
To the above macro is the first macro call in the program, it will
expand to:
MOV AX , VAR1
CMP AX , VAR2
JLE ??0000
MOV AX , VAR2
??0000:
And assuming that the call:
MIN2 VAR4, VAR2
To the above macro is the third macro call in the
program, after a second macro call to a different
macro in which four local labels where defined, the
call will expand to:
TOWNS MACRO
LOCAL TOWN, DONE
PUSH AX
PUSH DX
PUSH DS
MOV AX, CS
MOV DS, AX
MOV DX, OFFSET TOWN ; local TOWN
CALL DISPLAY_STRING ; DISPLAY_STRING is a global identifier
POP DS
POP AX
MOV DX, OFFSET COUNTRY ; COUNTRY is a global identifier
CALL DISPLAY_STRING
POP DX
JMP DONE
TOWN DB ‘Hossana’, 0DH, 0AH, '$'
DONE:
ENDM
.MODEL SMALL
.STACK 0400H
.DATA
TOWN DB 'ADDIS ABABA', 0DH, 0AH, '$'
COUNTRY DB ‘ETHIOPIA', 0DH, 0AH, '$'
.CODE
MAIN PROC
MOV AX, @DATA
MOV DS, AX
MOV DX, OFFSET TOWN ; global TOWN
CALL DISPLAY_STRING
TOWNS ; a macro call
MOV AX, 4C00H
INT 21H
MAIN ENDP
DISPLAY_STRING PROC
PUSH AX
MOV AH, 09H
INT 21H
POP AX
RET
DISPLAY_STRING ENDP
END MAIN
• INCLUDE C:\MACROS.H
• PURGE PROMPT, DIVIDE
• The macros file was saved with the name of
MACROS.TXT, the
instruction Include would be used the following way:
• ;Beginning of the program
Include MACROS.TXT
.MODEL SMALL
.DATA
;The data goes here
.CODE
Beginning:
;The code of the program is inserted here
.STACK
;The stack is defined
End beginning
;Our program ends
MACROS versus PROCEDURES
• Both procedures and macros can improve a program when, if they
are not used, the program would contain repeated occurrences of a
group of instructions.
• Macros offer a greater flexibility in programming compared to the
procedures, nonetheless, these last ones will still be used.
• The main difference between a macro and a procedure is that in the
macro the passage of parameters is possible and in the procedure it is
not, this is only applicable for the TASM - there are other
programming languages which do allow it. At the moment the macro
is executed each parameter is substituted by the name or value
specified at the time of the call.
• We can say then that a procedure is an extension of a determined
program, while the macro is a module with specific functions which
can be used by different programs. Another difference between a
macro and a procedure is the way of calling each one, to call a
procedure the use of a directive is required, on the other hand the
call of macros is done as if it were an assembler instruction.
The decision of whether to use a macro or procedure is often based on
one or more of the following considerations:
1. Assembly time
A program containing macros usually takes longer to assemble than
a similar program containing procedures, because it takes time to
expand the macros.
2. Execution time
The code generated by a macro expansion generally executes faster
than a procedure call, because there is no overhead of the CALL and
RET instructions.
3. Program size
A program with macros is generally larger than a similar program
with procedures, because each macro call inserts a new copy of its
code in the program.
4. Parameter passing
Parameter passing mechanism of macros is more convenient than
parameter passing mechanisms of procedures.
1. Write an assembly program that displays the complement of the
number?
00110011 =11001100
; EXAMPLE1.ASM – Calculate complement of a number
.MODEL small
.STACK 100h
.DATA
.CODE
mov ax,@data
mov ds,ax ;set DS to point to the data segment
mov bl, 11001111b
not bl
mov ah,02h
mov dl,bl
int 21h
mov ah,4ch ;DOS terminate program function
int 21h ;terminate the program
END
Write assembly programs that calculate the XOR operation of the
number?
00110000 XOR 00000011=00110010
; EXAMPLE2.ASM – Calculate XOR operation
.MODEL small
.STACK 100h
.DATA
.CODE
mov ax,@data
mov ds,ax ;set DS to point to the data segment
mov bl, 00110000b
mov cl, 00000010b
xor bl,cl
mov ah,02h
mov dl,bl
int 21h
mov ah,4ch ;DOS terminate program function
int 21h ;terminate the program
END
1. Write an assembly program that calculate the AND operation of the
number?
00110000 AND 00110000=00110000
; EXAMPLE3.ASM - Calculate AND operation
.MODEL small
.STACK 100h
.DATA
.CODE
mov ax,@data
mov ds,ax ;set DS to point to the data segment
mov bl, 00110000b
mov cl, 00000010b
AND bl,cl
mov ah,02h
mov dl,bl
int 21h
mov ah,4ch ;DOS terminate program function
int 21h ;terminate the program
END
• Write an assembly program that calculate the AND operation of the
number?
00110000 OR 00110011=00110011
; EXAMPLE4.ASM - Calculate OR operation "
.MODEL small
.STACK 100h
.DATA
.CODE
mov ax,@data
mov ds,ax ;set DS to point to the data segment
mov bl, 00110000b
mov cl, 00000010b
OR bl,cl
mov ah,02h
mov dl,bl
int 21h
mov ah,4ch ;DOS terminate program function
int 21h ;terminate the program
END
Write an assembly program that prints the following output?
Output:
Hello Ethiopia
Hello Ethiopia
Hello Ethiopia
Hello Ethiopia
; EXAMPLE5.ASM - Display the given output
.MODEL small
.STACK 100h
.DATA
HelloMessage DB 'Hello, Ethiopia',13,10,'$'
.CODE
mov ax,@data
mov ds,ax
mov cx,4
hello:
mov ah,9
mov dx,OFFSET HelloMessage
int 21h
loop hello
mov ah,08h
int 21h
mov ah,4ch
int 21h
Write an assembly program that take a character from the user and check whether the character is 1 or not?
Output: 1
1… Equal to 1
Output: 2
0… Not Equal to 1
; EXAMPLE6.ASM - Display the given output
.MODEL small
.STACK 100h
.DATA
msg1 db "...EQUAL to 1",'$'
msg2 db "...NOT EQUAL to 1",'$'
.CODE
mov ax,@data
mov ds,ax
mov ah,01h
int 21h
mov bl,al
sub bl,48
cmp bl,1
je equal
jne Nequal
equal:
mov ah,09h
mov dx,offset msg1
int 21h
jmp exit
Nequal:
mov ah,09h
mov dx,offset msg2
int 21h
exit:
mov ah,08h
int 21h
mov ah,4ch
int 21h
END
• Write an assembly program that find maximum of two numbers?
Output1:
Enter the first number: 4
Enter the second number: 2
The maximum is: 4
Output2:
Enter the first number: 3
Enter the second number: 3
The number are equal
; EXAMPLE7.ASM - Display the given output
.MODEL small
.STACK 100h
.DATA
msg1 db "Enter the first number: ",'$'
msg2 db "Enter the second number:",'$'
msg3 db "The maximum is: ",'$'
msg4 db "The number are equal ",'$'
nw db 13,10,'$'
.CODE
mov ax,@data
mov ds,ax
mov ah,09h
mov dx,offset msg1
int 21h
mov ah,01h
int 21h
mov bl,al
•
mov ah,09h
mov dx,offset nw
int 21h
mov ah,09h
mov dx,offset msg2
int 21h
mov ah,01h
int 21h
mov cl,al
mov ah,09h
mov dx,offset nw
int 21h
cmp bl,cl
ja above
je equal
jb below
jmp exit
above:
mov ah,09h
mov dx,offset msg3
int 21h
mov ah,02h
mov dx,bx
int 21h
jmp exit
equal:
mov ah,09h
mov dx,offset msg4
int 21h
jmp exit
below:
mov ah,09h
mov dx,offset msg3
int 21h
mov ah,02h
mov dx,cx
int 21h
exit:
mov ah,08h
int 21h
mov ah,4ch
int 21h
END