Assembly Language Lab 9 Agenda Macros What are

Assembly Language Lab 9

Agenda • Macros • • • What are macros? How it works? Definition & Invoking Ex: Write. Str Date Definition Procs vs Macros • Extract Assembly Code to DLL • Fibonacci

Macros

Preprocessor directives (C++) �are lines included in the code of programs preceded by a hash sign (#). These lines are not program statements but directives for the preprocessor. �The preprocessor examines the code before actual compilation of code begins and resolves all these directives before any code is actually generated by regular statements.

// function macro #include <iostream> using namespace std; #define getmax(a, b) ((a)>(b)? (a): (b)) int main() { int x=5, y; y= getmax(x, 2); cout << y << endl; cout << getmax(7, x) << endl; return 0; }

Inline functions � is a function upon which the compiler has been requested to perform inline expansion. �Inline expansion is used to eliminate the time overhead(excess time) when a function is called. Macros Inline Fn. Type checking Return keyword Recursion

What are Macros? �A macro is a named block of assembly language statements. Once defined, it can be called many times in the program.

How it works? �When you invoke a macro, a copy of its code is inserted directly into the program at the location where it was invoked. �This type of automatic code insertion is also known as inline expansion.

How it works? � Macros are defined directly at the beginning of a source program, or they are placed in a separate file and copied into a program by an INCLUDE directive. � Macros are expanded during the assembler’s preprocessing step. In this step, the preprocessor reads a macro definition and scans the remaining source code in the program. � At every point where the macro is called, the assembler inserts a copy of the macro’s source code into the program. � A macro definition must be found by the assembler before trying to assemble any calls of the macro.
![Macro definition macroname macro [parm 1, parm 2, …] statement-list endm Macro definition macroname macro [parm 1, parm 2, …] statement-list endm](http://slidetodoc.com/presentation_image/88ff7e54651205a0f0108386879a7dde/image-10.jpg)
Macro definition macroname macro [parm 1, parm 2, …] statement-list endm

Invoking Macros �Just write its name followed by parameters values if any.

Example m. Put. Char macro char push EAX mov AL, char call write. Char pop EAX Definition Endm. code main proc m. Put. Char ‘a’ ; Macro Invokes Invoke exit (calling) main endp end main

Exercise Write. Str macro �Write a macro that prints string arrays on screen

include irvine 32. inc ; Macro Definition m. Write. Str macro text push edx mov edx, offset text call writestring call Cr. Lf pop edx endm

. data str 1 byte 'Hello world 1', 0 str 2 byte 'Hello world 2', 0. code main proc m. Write. Str str 1 ; Macro Invokes m. Write. Str str 2 exit main endp end main

The generated code. . main proc ; m. Write. Str str 1 push edx mov edx, offset str 1 call writestring call Cr. Lf pop edx ; m. Write. Str str 2 push edx mov edx, offset str 2 call writestring call Cr. Lf pop edx. . . main endp

Data definition in macros �A macro can include even data variables in its definition beside its code. �But, it should be preceded by LOCAL directive to tell the preprocessor to create a unique label each time the macro invoked, otherwise this label will be redefined each time the macro invoked causing assembling error.

Example Write. Txt macro �Write a macro that prints string constants on screen

include irvine 32. inc m. Write. Text macro text ; string is local variable used in macro so preprocessor will ; create a unique name each time the macro invoked local str 1. data ; define a data segment to store passed text ; allocate the string array named by the str 1 label and ; initialized by the text parameter str 1 byte text, 0. code ; define a push edx mov edx, offset str 1 call writestring call Cr. Lf pop edx Endm code segement to print the string

. data. code main proc m. Write. Text 'hello world 1' m. Write. Text 'hello world 2' exit main endp end main

The generated code. . main proc ; m. Write. Text 'hello world 1'. data ? ? 0000 byte 'hello world 1', 0. code push edx mov edx, offset ? ? 0000 call writestring pop edx ; m. Write. Text 'hello world 2'. data ? ? 0001 byte 'hello world 2', 0. code push edx mov edx, offset ? ? 0001 call writestring pop edx. . . main endp

Procs vs Macros Procs ØMake your code easier to read than macros ØThere are jump costs ØLarge chunk of code; you'll be better off with the procedure as the cost of the jump will be very small in such a case Macros ØThere are NO jump ØSmall chunk of code; you just want to do one or two things within the body of code), use a macro since what you're doing with the code is not worth the cost of the jump/procedure call

Export Assembly Code to. dll

Introduction �As we know, we will not use assembly code to build large project. It is too hard to do so. �Therefore, we have to know how to use assembly code in well known high-level languages to take the advantages of both. In next two sections, �We will show to export our assembly code to a dynamic-linking library (. dll) to be used in any other high level language.

Modified Project Template include irvine 32. inc. data ; put your static data. code ; Write your functions here ; Do not forget to modify main. def file accordingly ; Dll. Main is required for any DLL Dll. Main PROC h. Instance: DWORD, fdw. Reason: DWORD, lp. Reserved: DWORD mov eax, 1; Return true to caller. ret Dll. Main ENDP END Dll. Main

Dll. Main Comments… �Dll. Main procedure must be defined exactly as specified to be able to link this code correctly. This constraint is required as Windows assumes the existing of this function in any DLL library to be able load it and so we defined it as Windows needs. �Dll. Main function does not do anything except necessary initialization for loading the library; it does not call any other procedures like normal applications. This comes from the concept of the library. Library is just a collection of functions to be called from other applications and so the Dll. Main function has nothing to do.

Definition file (. def) � The assembler should know what procedures to export. Consequently, you should write a separate definition file (. def) which contains procedures names to be exported. � The definition file (main. def) contains a list of names of exported procedures combined with an ordinal number which specify the order of the procedure in the library. Note that, if you did not fill this file, no function will be exported to the DLL library. � You must write names of procedures exactly the same as defined in the. asm file (and matching the letter case too).

General Steps Take a Copy from DLL Project Template Modify the main. asm by writing all needed PROCs Lets See By Live Demo Modify the main. def by using “exports” followed by PROCS names & order Build the Assm Project Create C# Project Copy assembly. dll into C# project bin directory Use You Imported Funcs

Recursive Fibonacci
![Fibonacci Series �[0], 1, 1, 2, 3, 5, 8, 13 … �So we need Fibonacci Series �[0], 1, 1, 2, 3, 5, 8, 13 … �So we need](http://slidetodoc.com/presentation_image/88ff7e54651205a0f0108386879a7dde/image-30.jpg)
Fibonacci Series �[0], 1, 1, 2, 3, 5, 8, 13 … �So we need to write an assembly program to calculate Fibonacci value given an index �Example � 3 2 � 4 3

. code Fib PROTO index: DWORD main PROC call readdec ; eax = index: INVOKE Fib, EAX call writedec exit main ENDP

Fib PROC USES EBX index: DWORD CMP index, 0 ; base step is 0 and 1 JE base. Step CMP index, 1 JE base. Step ; Recursion step DEC index ; n-1 INVOKE Fib, index ; Fib(n-1) PUSH EAX ; EAX = Fib(n - 1) DEC index; n-2 INVOKE Fib, index ; Fib(n-2) POP EBX; EBX = Fib(n-1) ADD EAX, EBX ; Fib(n-1) + Fib(n-2) ret base. Step: MOV EAX, index ret Fib ENDP

Assignment #6 (Bonus)

Read. Int 32 Macro 1) Write a macro with format Read. Int 32 dest that reads an integer from user and returns the input integer in the dest parameter

MULT Macro 2) Write a macro with format MULT dest, src that multiplies 32 -bit source and destination operands and put result in destination

Questions?

Thanks!
- Slides: 37