This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:multiasm:papc:chapter_6_13 [2026/02/20 10:15] – [Macro functions] ktokarz | en:multiasm:papc:chapter_6_13 [2026/06/22 17:10] (current) – pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Macros ====== | ====== Macros ====== | ||
| - | Macros are elements of language that enable the replacement of one, usually shorter text, with another. In different assemblers (NASM, FASM), macros can be implemented differently. | + | Macros are elements of language that enable the replacement of one, usually shorter text, with another. In different assemblers (e.g., NASM and FASM), macros can be implemented differently. |
| * Text macros, | * Text macros, | ||
| * Macro procedures, | * Macro procedures, | ||
| Line 10: | Line 10: | ||
| ===== Text macros ===== | ===== Text macros ===== | ||
| - | Text macros are a simple | + | Text macros are a simple |
| <code asm> | <code asm> | ||
| Line 38: | Line 38: | ||
| </ | </ | ||
| ===== Macro procedures ===== | ===== Macro procedures ===== | ||
| - | Macro procedure is a definition of a fragment of code that can be used later in the resulting program. In some assemblers, they are named multi-line macros. The use of the name of a macro procedure later in the source program causes the so-called expansion of the macro. It is worth noting that for every usage of the macro, the assembler will place in the resulting program all elements specified in the statements | + | Macro procedure is a definition of a fragment of code that can be used later in the resulting program. In some assemblers, they are named multi-line macros. The use of the name of a macro procedure later in the source program causes the so-called expansion of the macro. It is worth noting that for each invocation |
| * instructions, | * instructions, | ||
| * directives, | * directives, | ||
| Line 61: | Line 61: | ||
| </ | </ | ||
| - | An example of a simple macro which makes a copy of data from the source to the destination | + | An example of a simple macro that copies |
| <code asm> | <code asm> | ||
| copy MACRO arg1, arg2 | copy MACRO arg1, arg2 | ||
| Line 68: | Line 68: | ||
| ENDM | ENDM | ||
| </ | </ | ||
| - | Note that the arguments of the macro must fit the instructions used inside it. They can be previously defined variables, the first argument can be a constant, or you can even pass the names of other registers. | + | Note that the macro's arguments |
| <code asm> | <code asm> | ||
| Line 97: | Line 97: | ||
| </ | </ | ||
| - | The solution to avoid such a situation is to assign the default | + | The solution to avoid such a situation is to assign |
| <code asm> | <code asm> | ||
| ; The " | ; The " | ||
| Line 113: | Line 113: | ||
| </ | </ | ||
| - | In the second case, the error will be signalled, but at the line with a macro use, not inside the macro, which makes it easier to localise the error. | + | In the second case, the error will be signalled at the line where a macro is used, not inside the macro, which makes it easier to localise the error. |
| <code asm> | <code asm> | ||
| ; The " | ; The " | ||
| Line 125: | Line 125: | ||
| </ | </ | ||
| - | If a label needs to be defined inside the macro, it must be declared as local to avoid ambiguity. In general, any symbol can be declared as local. In such a case, it will only be visible | + | If a label needs to be defined inside the macro, it must be declared as local to avoid ambiguity. In general, any symbol can be declared as local. In that case, it will only be visible |
| <code asm> | <code asm> | ||
| Line 176: | Line 176: | ||
| </ | </ | ||
| - | Now, let's examine four repeat | + | Now, let's examine four repeated |
| <code asm> | <code asm> | ||
| i = 1 | i = 1 | ||
| Line 201: | Line 201: | ||
| ===== Macro functions ===== | ===== Macro functions ===== | ||
| - | A macro function is a macro which returns a value. As all macros are texts, macro functions always return text. Returning | + | A macro function is a macro which returns a value. As all macros are texts, macro functions always return text. Returning |
| ===== Predefined macro functions ===== | ===== Predefined macro functions ===== | ||
| - | Masm offers a set of predefined macro functions, usually used to process texts. There are two versions of them. First is the macro function, which returns the resulting text. Second is the directive, which returns the same text and additionally creates a new symbol. The macro function **@SubStr** returns part of a source string. The **SUBSTR** directive assigns part of a source string to a new symbol. The macro function **@InStr** searches for one string within another and returns its position. The **INSTR** creates a new symbol containing the position of one string in another. The macro function **@SizeStr** determines the string | + | Masm offers a set of predefined macro functions, usually used to process texts. There are two versions of them. First is the macro function, which returns the resulting text. Second is the directive, which returns the same text and additionally creates a new symbol. The macro function **@SubStr** returns part of a source string. The **SUBSTR** directive assigns part of a source string to a new symbol. The macro function **@InStr** searches for one string within another and returns its position. The **INSTR** creates a new symbol containing the position of one string in another. The macro function **@SizeStr** determines the size of the string. The **SIZESTR** creates a new item and assigns to it the size of a string. The macro function **@CatStr** concatenates one or more strings to a single string and returns a concatenated string. The **CATSTR** directive concatenates one or more strings to a newly defined string. The following code demonstrates the equivalent use of both versions. |
| <code asm> | <code asm> | ||
| name SUBSTR string, start [, length ] | name SUBSTR string, start [, length ] | ||