procedures and macros in assembly language lecture9.pdf

malnaham 73 views 26 slides Nov 25, 2024
Slide 1
Slide 1 of 26
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26

About This Presentation

procedures and macros lecture for third year of computer engineering


Slide Content

Lecture 9
Procedures and Macros
By
Dr. Mohammed Y.M Alnaham
Dr. Mohammed Y.M Alnaham 1

PROCEDURES AND MACROS
•When we need to use a group of instructions
several times throughout a program there are two
ways we can avoid having to write the group of
instructions each time we want to use them.
•1 One way is to write the group of instructions as a
separate procedure.
•Another way we can use macros.
Dr. Mohammed Y.M Alnaham 2

Procedures
•The procedure is a group of instructions stored as a separate program in
the memory and it is called from the main program whenever required.
•Procedures are also called as ‘Sub-programs’ or ‘Sub-routines’.
•A Procedure is a separate group of instructions apart from the main
program.
•Procedures are accessed by CALL Instruction during the execution of the
program.
•A RET Instruction at the end of the procedure returns execution to the
main program.
•So, Procedures represent Top-Down Approach.
Dr. Mohammed Y.M Alnaham 3

Procedure Block Syntax
•<procedure name> PROC (FAR |NEAR)
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
RET
<procedure name> ENDP
Dr. Mohammed Y.M Alnaham 4

Near Procedure Example
• CODE_SEGSEGMENT
::
CALLA
::
APROCNEAR
::
::
RET
AENDP
::
CODE_SEGENDS
Dr. Mohammed Y.M Alnaham 5

Far Procedure Example
•CODE_SEGSEGMENT
::
CALLB
::
CODE_SEGENDS
CODE_SEG1 SEGMENT
BPROCFAR
::
RET
BENDP
CODE_SEG1 ENDS
Dr. Mohammed Y.M Alnaham 6

CALL Instruction
•CALL Instruction is used to call a procedure from the main
program.
•The CALL instructions take the same forms as the JMP
instructions.
•There are two types of CALL Instructions.
i) Far CALL : A procedure is known as FAR procedure if it is
written (defined) in the different code segment than the
calling segment. In this case both Instruction Pointer(IP) and
the Code Segment(CS) register content will be changed.
Dr. Mohammed Y.M Alnaham 7

CALL Instruction
•ii) Near CALL :-Procedure in same Code Segment
•Pushes the 16-bit offset of the next instruction
following the call onto the stack.
•Copies the 16-bit effective address of procedure
into the IP register.
•Execution continues at the first instruction of the
procedure.
•Only Instruction Pointer(IP register) contents will be
changed in NEAR procedure.
Dr. Mohammed Y.M Alnaham 8

Difference between FAR CALL and NEAR CALL.
Near Call Far Call
A near call refers a procedure
which is in the same code
segment.
A Far call refers a procedure
which is in different code
segment
It is also called Intra-segment
call.
It is also called Inter-segment
call
A Near Call replaces the old IP
with new IP
A FAR replaces CS & IP with
new CS & IP.
It uses keyword near for
calling procedure.
It uses keyword far for calling
procedure.
Less stack locations are
required
More stack locations are
required.
Dr. Mohammed Y.M Alnaham 9

Passing Parameters to and from Procedures
•There are four major ways of passing parameters to
and from a procedure.
1)In register
2)In dedicated memory locations accessed by name
3)With pointer passed in register.
4)With the stack Dr. Mohammed Y.M Alnaham 10

Dr. Mohammed Y.M Alnaham 11

Dr. Mohammed Y.M Alnaham 12

Dr. Mohammed Y.M Alnaham 13

Dr. Mohammed Y.M Alnaham 14

Re-entrant Procedures
•A Procedure which can be Interrupted, Used
and Re-entered is known as Re-entrant
Procedure.
•Push the values used in procedure into stack.
•Passing parameters through registers and stack
are only allowed in re-entrant procedures.
Dr. Mohammed Y.M Alnaham 15

Reentrant Procedure
•Diagram
Dr. Mohammed Y.M Alnaham 16

Recursive Procedures
•A Recursive procedure calls the procedure itself.
•Recursive procedures are used in complex data
structures like TREES.
•Ex: Factorial procedure
Dr. Mohammed Y.M Alnaham 17

Recursive Procedure
•A recursive procedure is procedure which calls itself.
Dr. Mohammed Y.M Alnaham 18

Recursive Procedure
•Flow diagram •Pseudo code
Dr. Mohammed Y.M Alnaham 19

Factorial program
Program for Factorial of number using
recursive procedures
CODE SEGMENT
ASSUME CS:CODE
START: MOV AX,7
CALL FACT
MOV AH,4CH
INT 21H
FACT PROC NEAR
MOV BX,AX
DEC BX
BACK: MUL BX
DEC BX
JNZ BACK
RET
ENDP
CODE ENDS
END START
Dr. Mohammed Y.M Alnaham 20

Macros
•Macro is a group of Instructions, which
when Called, inserts those group of
Instructions in the place of CALL.
•Macro should have less no. of instructions.
•There is no need of transferring the
execution like a procedure.
Dr. Mohammed Y.M Alnaham 21

Macro Block without Parameters
•<Macro name> MACRO
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
ENDM
Code Segment
<Macro name>
Code ends
Dr. Mohammed Y.M Alnaham 22

Macro Block with Parameters
•<Macro name> MACRO (arg1,arg2,....)
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
………………..Instructions……………
ENDM
Code Segment
<Macro name> (arg1,arg2,..)
Code ends Dr. Mohammed Y.M Alnaham 23

Advantage of Procedure
Procedures: Advantages
•The machine codes for the group of instructions in
the procedure only have to be put once.
Disadvantages
•Need for stack
•Overhead time required to call the procedure and
return to the calling program.
Dr. Mohammed Y.M Alnaham 24

Advantage of Macros
Macros: Advantages
•Macro avoids overhead time involving in calling and
returning from a procedure.
Disadvantages
•Generating in line code each time a macro is called is
that this will make the program take up more memory
than using a procedure.
Dr. Mohammed Y.M Alnaham 25

Procedures and Macros -Comparison
•Code of procedure is
once loaded in memory.
•Procedures will use CALL
instruction for accessing.
•More Time taken for
CALL and RET.
•Stack is needed.
•More no. of Instructions
•Code of macro have to
be loaded repeatedly in
memory.
•No CALL Instruction
•No-over head time for
CALL and RET.
•No Need of Stack.
•Less no. of Instructions.
Dr. Mohammed Y.M Alnaham 26