Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Language Lab 9.

Similar presentations


Presentation on theme: "Assembly Language Lab 9."— Presentation transcript:

1 Assembly Language Lab 9

2 Agenda Macros Extract Assembly Code to DLL Fibonacci What are macros?
How it works? Definition & Invoking Ex: WriteStr Date Definition Procs vs Macros Extract Assembly Code to DLL Fibonacci

3 Macros

4 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.

5 // 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; }

6 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 . inline int max(int a, int b) { return (a > b) ? a : b; } Macros Inline Fn. Type checking Return keyword Recursion

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

8 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.

9 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.

10 Macro definition macroname macro [parm1, parm2,…] statement-list endm

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

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

13 Exercise WriteStr macro
Write a macro that prints string arrays on screen

14 include irvine32.inc ;Macro Definition mWriteStr macro text push edx mov edx, offset text call writestring call CrLf pop edx endm

15 .data str1 byte 'Hello world 1', 0 str2 byte 'Hello world 2', 0 .code main proc mWriteStr str1 ;Macro Invokes mWriteStr str2 exit main endp end main

16 The generated code .. main proc ;mWriteStr str1 push edx mov edx, offset str1 call writestring call CrLf pop edx ;mWriteStr str2 mov edx, offset str2 ... main endp

17 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.

18 Example WriteTxt macro
Write a macro that prints string constants on screen

19 Endm include irvine32.inc mWriteText macro text str1 byte text, 0
;string is local variable used in macro so preprocessor will ;create a unique name each time the macro invoked local str1 .data ;define a data segment to store passed text ;allocate the string array named by the str1 label and ;initialized by the text parameter str1 byte text, 0 .code ;define a code segement to print the string push edx mov edx, offset str1 call writestring call CrLf pop edx Endm

20 .data .code main proc mWriteText 'hello world 1' mWriteText 'hello world 2' exit main endp end main

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

22 Procs vs Macros Procs Macros 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 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

23 Export Assembly Code to .dll

24 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 how to export our assembly code to a dynamic-linking library (.dll) to be used in any other high level language.

25 Modified Project Template
include irvine32.inc .data ;put your static data .code ;Write your functions here ;Do not forget to modify main.def file accordingly ; DllMain is required for any DLL DllMain PROC hInstance:DWORD, fdwReason:DWORD, lpReserved:DWORD mov eax, 1; Return true to caller. ret DllMain ENDP END DllMain

26 DllMain Comments… DllMain 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. DllMain 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 DllMain function has nothing to do.

27 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).

28 General Steps Lets See By Live Demo Take a Copy from
DLL Project Template Modify the main.asm by writing all needed PROCs 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 Open Export to DLL Folder found with this presentation DllProject_template Open it to see the modified skelton Create a copy project and named it DLLSampleTest Open the DLLSampleTest you will find the DLLMain in the .asm file and 3 PROCS Sum, SumArr and ToUpper explain it briefly Don’t forget to open main.def explicitly it won’t be appeared in the file tree by default Save a build the project Create a C# Project one is already exists DLLTestSampleCSharp Copy the Project.dll created in step 4 then past it in the bin directory in DLLTestSampleCSharp Open the DLLTestSampleCSharp go through the program.cs you will find the exported functions written in the high level (C#) style Ctrl + f5 and show the results Enjoy ;)

29 Recursive Fibonacci

30 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

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

32 Fib PROC USES EBX index:DWORD
CMP index, 0 ;base step is 0 and 1 JE baseStep CMP index, 1 ;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 baseStep: MOV EAX, index Fib ENDP

33 Assignment #6 (Bonus)

34 ReadInt32 Macro 1)  Write a macro with format ReadInt32 dest that reads an integer from user and returns the input integer in the dest parameter

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

36 Questions?

37 Thanks!


Download ppt "Assembly Language Lab 9."

Similar presentations


Ads by Google