9 - Macros, Recursion, and Digital Logic Flashcards
How is a procedure translated into assembly code and executed?
During assembly, procedure code is translated ONLY once.
During execution, control is transferred to the procedure at EACH call (activation record, etc.).
How is a macro translated into assembly code and executed?
During assembly, the ENTIRE macro code is substituted for each call (expansion).
During execution, it can be invoked by NAME only (not CALL),
How do you define macros?
macroname MACRO [param1, param2, …]
statement-list
ENDM
Parameters are optional. There’s also no theoretical limit to the number of parameters, but stylistically there is.
How do you invoke a macro?
Just give the name and arguments (if any).
Each argument matches a declared parameter, and each parameter is replaced by its corresponding argument when the macro is expanded.
How do you define WriteString as a macro?
mWriteStr MACRO buffer push edx mov edx, OFFSET buffer call WriteString pop edx ENDM
.. .code mWriteStr str1 mWriteStr str2 ...
How do you define ReadString as a macro?
mReadStr MACRO varName push ecx push edx mov edx, OFFSET varName mov ecx, (SIZEOF varName) - 1 pop edx pop ecx ENDM
…
mReadStr firstName
…
Notice that you set up the expected length of the string (minus 1 for the null character).
How would you create a macro that prints a sequence from a to b?
seq MACRO a, b LOCAL test LOCAL quit mov eax, a mov ebx, b test: cmp eax, ebx ;if a
What do you need to do when writing macros with jumps?
The problem: there will be the same labels through the code, and it might jump to the wrong ones.
Therefore, you can specify the labels as being LOCAL. MASM handles the problem by appending a unique number to the label (substitution is based on how many times you’ve called the macro).
Is there parameter checking for MACROS?
As long as there’s no size mismatch, etc., they’ll accept anything!
The problem: the macro can be called with conflicting register parameters!!
What are the advantages of macros over procedures?
- They are convenient and easy to understand.
- They execute faster than procedures (no return address, stack manipulation).
- They are invoked by name
- They do NOT need a ret statement!!
What are the advantages of procedures over macros?
If the macro is called many times, the assembler procedures “fat code”, which will be invisible to the programmer.
Therefore, use a macro only for short code that is called a “few” times and uses few registers.
What is recursion?
A procedure can call itself.
A procedure A calls procedure B, which in turn calls procedure A.
A good recursive definition must ALWAYS approach a base case (not so with getData, so that should be repetition structure).
What is the psuedocode for a summation formula?
if (a == b)
return a
else return a + summation (a+1, b)
What is MASM code for a summation procedure to calculate the integers from x to y?
summation PROC push ebp mov ebp, esp mov eax, [ebp+16] ;eax = x mov ebx, [ebp+12] ;eax = y mov edx, [ebp+8] ;@sum in edx add [edx], eax cmp eax, ebx je quit ;base case if x = y recurse: inc eax push eax push ebx push edx call summation quit: pop ebp ret 12 summation ENDP
Why is stack overflow so common in recursion?
If you don’t use a stack frame for each recursive call, etc, your recursion might never reach its base case again.