Assembly Language Lab 9.

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

Computer Architecture CSCE 350
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
CS2422 Assembly Language & System Programming October 31, 2006.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
CS2422 Assembly Language & System Programming September 26, 2006.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
ICS312 Set 14 MACROS. Macros The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions.
Python Functions.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
CT215: Assembly Language Programming Chapter 10: Macros (c) Pearson Education, All rights reserved. You may modify and copy this slide show for your.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Chapter 15 - C++ As A "Better C"
C++ Lesson 1.
Assembly language programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Format of Assembly language
Chapter 1.2 Introduction to C++ Programming
Topic Pre-processor cout To output a message.
1st prog! Q: Read a char – from a keyboard & display it at the beginning of the next line! ====== A.
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Assembly Lab 3.
Introduction to C++ Systems Programming.
Computer Architecture and Assembly Language
Additional Assembly Programming Concepts
Chapter 5 Conclusion CIS 61.
Functions CIS 40 – Introduction to Programming in Python
CSC113: Computer Programming (Theory = 03, Lab = 01)
(The Stack and Procedures)
MACRO Processors CSCI/CMPE 3334 David Egle.
Programming 8086 – Part IV Stacks, Macros
Assembly Language Programming I: Introduction
Stack Frames and Advanced Procedures
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
More About Data Types & Functions
C Preprocessor(CPP).
(The Stack and Procedures)
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Introduction to C++ Programming
Multi-modules programming
C++ Programming Lecture 3 C++ Basics – Part I
Programs written in C and C++ can run on many different computers
Lecture 2 Fall 2011 September 13-15, 2011 Ghufran Ahmed
C++ Programming Basics
(The Stack and Procedures)
Computer Organization and Assembly Language
Chapter 1 c++ structure C++ Input / Output
Computer Organization and Assembly Language
Computer Architecture and System Programming Laboratory
Procedures & Macros Introduction Syntax Difference.
Procedures and Macros.
Presentation transcript:

Assembly Language Lab 9

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

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 . inline int max(int a, int b) { return (a > b) ? a : b; } 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 [parm1, parm2,…] statement-list endm

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

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)

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

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

.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

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

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 WriteTxt macro Write a macro that prints string constants on screen

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

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

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 ??0001 ... main endp

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

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

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

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.

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 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 ;)

Recursive Fibonacci

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

Assignment #6 (Bonus)

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

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!