Download presentation
Presentation is loading. Please wait.
Published byΧριστόφορος Γιαννόπουλος Modified over 5 years ago
1
Procedures & Macros Introduction Syntax Difference
2
An Overview A macro is similar to a procedure
A macro name represents a group of instructions A procedure is called at execution time control transfers to the procedure returns after executing the procedure's statements A macro is invoked at assembly time Assembler copies macro's statements into the program at the point of invocation
3
Procedures Procedures or subroutines are very important in assembly language, as the assembly language programs tend to be large in size. Procedures are identified by a name. Following this name, the body of the procedure is described which performs a well-defined job. End of the procedure is indicated by a return statement.
4
Procedure/Subroutines(Syntax)
The PROC and ENDP directives indicate the start and end of a procedure (subroutine). Both the PROC and ENDP directives require a label to indicate the name of the procedure. Syntax: <Procedure-name> PROC ;starts procedure ;body of the procedure .. RET <Procedure-name>ENDP ;close of the procedure When returning from procedures, use RET instruction.
5
Procedure/Subroutines(Example)
.MODEL SMALL .DATA msg DB “Hello World$” .STACK .CODE myProc PROC MOV DX, offset msg MOV AH, 09H INT 21H RET myProc ENDP .STARTUP CALL myProc .EXIT END Procedures can be declared within the .CODE (code segment). However, ensure that a procedure is not executed without explicit invocation.. Use CALL instruction to call the procedure. Eg. CALL procedure-name
6
Macros A macro is a group of instructions that perform one task, just as a procedure performs one task. The difference is that a procedure is accessed via a CALL instruction, whereas a macro, and all the instructions defined in the macro, is inserted in the program at the point of usage. Creating a macro is very similar to creating a new opcode, which is actually a sequence of instructions, in this case, that can be used in the program. You type the name of the macro and any parameters associated with it, and the assembler then inserts them into the program. Macro sequences execute faster than procedures because there is no CALL or RET instruction to execute. The instructions of the macro are placed in your program by the assembler at the point where they are invoked.
7
Macros (Syntax) The MACRO and ENDM directives define a macro sequence.
The first statement of a macro is the MACRO instruction, which contains the name of the macro and any parameters associated with it. An example is MOVE MACRO A,B which defines the macro name as MOVE. This new pseudo opcode uses two parameters: A and B. The last statement of a macro is the ENDM instruction, which is placed on a line by itself. Never place a label in front of the ENDM statement. If a label appears before ENDM, the macro will not assemble. Syntax: <Macro-name> MACRO [<Arg 1> <,Arg 2>…<,Arg n>] .. ENDM
8
Macros (Example) Example; Adding two numbers using Macro .MODEL SMALL ;define Memory Model SUM MACRO X,Y ;Macro definition SUM, X and Y are parameters MOV AX, X ;Move value of X in AX MOV BX, Y ;Move Value of Y in BX ADD AX, BX ;Add values of X and Y ENDM ;end of macro .DATA ;start of data segment .STACK ;start of stack segment .CODE ;start of code segment .STARTUP ;start of execution SUM 5,10 ;call Macro with Parameters 5 and 10 .EXIT ;return to DOS END ;end of program file
10
Macros vs. Procedures Assembly Time Execution Time Program Size
a program containing macros usually takes longer to assemble Execution Time a program containing macros is usually faster Program Size a program with macros is generally larger, since each macro call causes the code to be inserted Other Considerations macros are suitable for small tasks procedures are more suited for large tasks
11
Comparison Macros and Procedures
A big advantage of using procedures is that the machine codes for the group of instruction in the procedures needs to be loaded in to main memory only once.. \ Disadvantage using the procedures is the need for the stack. A macro is the group of instruction we bracket and give a name to at the start of the program. Using macro avoids the overhead time involved in calling and returning from a procedures. Disadvantage is that this will make the program take up more memory than using a procedure.
12
Uses of Macros and Procedures
Macros and procedures are useful for the simplification of program. Group of instruction to be treated as a single entities.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.