Lecture Computer organization and assembly language: Chapter 31 - Dr. Safdar Hussain Bouk

pptx
Số trang Lecture Computer organization and assembly language: Chapter 31 - Dr. Safdar Hussain Bouk 27 Cỡ tệp Lecture Computer organization and assembly language: Chapter 31 - Dr. Safdar Hussain Bouk 656 KB Lượt tải Lecture Computer organization and assembly language: Chapter 31 - Dr. Safdar Hussain Bouk 0 Lượt đọc Lecture Computer organization and assembly language: Chapter 31 - Dr. Safdar Hussain Bouk 0
Đánh giá Lecture Computer organization and assembly language: Chapter 31 - Dr. Safdar Hussain Bouk
5 ( 12 lượt)
Nhấn vào bên dưới để tải tài liệu
Để tải xuống xem đầy đủ hãy nhấn vào bên trên
Chủ đề liên quan

Nội dung

CSC 221 Computer Organization and Assembly Language Lecture 31: High-Level Language Interface (2/2) Lecture 30: Review • Why Link ASM and HLL Programs? – HLL • Hides low-level details, Less time, Slow Speed and Large Size – Assembly • Speed up critical sections of code, Access nonstandard hardware devices, Write platform-specific code, Extend the HLL's capabilities • External Identifier: • is a name that has been placed in a module’s object file in such a way that the linker can make the name available to other program modules. • Naming convention: • rules or characteristics regarding the naming of variables and procedures Lecture 30: Review (cont.) • Calling Convention: • Refers to the low-level details about how procedures are called • Registers that must be preserved by procedures. • How arguments are passed to procedures? • The order in which arguments are passed by calling programs to procedures. • Whether arguments are passed by value or by reference. • How the stack pointer is restored after a procedure call? • How functions return values? Lecture 30: Review • .MODEL Directive – stdcall • AddTwo(5, push push call 6) last to 1st. 6 5 AddTwo • Saves EBP, ESP and access parameters using EBP. • ret 8 ; clean up the stack • _name@nn – C specifier: push 6 ; Second Argument push 5 ; First Argument call AddTwo add esp,8 ; clean up the stack • _AddTwo (cont.) Lecture 30: Review • _asm Directive in Microsoft Visual C++stdcall – Syntax: __asm statement __asm { statement-1 statement-2 ... statement-n } (cont.) Lecture Outline • Function Call Overhead • Linking to Visual C++ Programs – Linking to Visual C++ • Optimizing Your Code – Loop Optimization Example – FindArray Example – Creating the FindArray Project Function Call Overhead using namespace std; int main( ) { const int BUFSIZE = 2000; char buffer[BUFSIZE]; unsigned int count; unsigned char encryptCode; cout << "Encryption code [0-255]? "; eax,byte ptr [ebp+FFFFF7FBh] cin >> encryptCode; movzx push eax ifstream infile( "plain1.txt", ios::binary ); mov ecx,dword ptr ); [ebp+FFFFF804h] ofstream outfile( "cipher1.txt", ios::binary cout << “ENCODING“<< endl; ecx while (!infile.eof()push ) lea edx,[ebp+FFFFF810h] { pushBUFSIZE); edx infile.read(buffer, call TranslateBuffer (00C813E8) count = infile.gcount(); add esp,0Ch TranslateBuffer(buffer, count, encryptCode); outfile.write(buffer, count); } return 0; } Function Call Overhead void TranslateBuffer( char * buf, unsigned count, unsigned char eChar ) { push ebp __asm { mov ebp,esp mov esi,buf sub esp,0C0h mov ecx,count push ebx mov al,eChar push esi L1: push edi xor [esi],al lea edi,[ebp+FFFFFF40h] inc esi mov ecx,30h loop L1 mov eax,0CCCCCCCCh } // asm rep stos dword ptr es:[edi] } pop edi pop esi pop ebx add esp,0C0h cmp ebp,esp call 00C8147E • Automatically # of statements inserted by the compiler to set up EBP. mov esp,ebp • Save standard set of registers whether or not they are actually pop ebp modified by the procedure. ret Function Call Overhead • Thousands of function calls will cause considerable overhead/delay. • To avoid this overhead, inserting the inline code will create a more efficient program. while (!infile.eof() ) { infile.read(buffer, BUFSIZE ); count = infile.gcount(); __asm { lea esi,buffer mov ecx,count mov al,encryptCode L1: xor [esi],al inc esi Loop L1 } // asm outfile.write(buffer, count); } Linking Assembly Language to C++ • Basic Structure - Two Modules – The first module, written in assembly language, contains the external procedure – The second module contains the C/C++ code that starts and ends the program • The C++ module adds the extern qualifier to the external assembly language function prototype. • The "C" specifier must be included to prevent name decoration by the C++ compiler: extern "C" functionName( parameterList ); Name Decoration Also known as name mangling. HLL compilers do this to uniquely identify overloaded functions. A function such as: int ArraySum( int * p, int count ) would be exported as a decorated name that encodes the return type, function name, and parameter types. For example: int_ArraySum_pInt_int The problem with name decoration is that the C++ compiler assumes that your assembly language function's name is decorated. The C++ compiler tells the linker to look for a decorated name. C++ compilers vary in the way they decorate function names. ; AddTwo.asm file .486 .model flat, stdcall option casemap :none AddTwo .code AddTwo mov add ret AddTwo END PROTO, Arg1:DWORD, Arg2:DWORD PROC, Arg1:DWORD, Arg2:DWORD eax, Arg1 eax, Arg2 ENDP #include #include using namespace std; extern "C" int __stdcall AddTwo (int, int); int main() { int a=2; int b=3;int c=0; c = AddTwo(a,b); cout<> searchVal; cout << "Please wait. This will take between 10 and 30 seconds...\n"; ………… Creating the FindArray Project //findarr_main.cpp ……… // Test the C++ function: time( &startTime ); bool found = false; for( int n = 0; n < LOOP_SIZE; n++) found = FindArray( searchVal, array, ARRAY_SIZE ); time( &endTime ); cout << "Elapsed CPP time:" << long(endTime - startTime) << " seconds. Found = " << found << endl; ………… Creating the FindArray Project //findarr_main.cpp ……… // Test the Assembly language procedure: time( &startTime ); found = false; for( int n = 0; n < LOOP_SIZE; n++) found = AsmFindArray( searchVal, array, ARRAY_SIZE ); time( &endTime ); cout << "Elapsed ASM time: " << long(endTime - startTime) << " seconds. Found = " << found << endl; ………… Creating the FindArray Project Let’s Enjoy Assembly in C/C++ Summary • Function Call Overhead • Linking to Visual C++ Programs – Linking to Visual C++ • Optimizing Your Code – Loop Optimization Example – FindArray Example – Creating the FindArray Project Reference Most of the Slides are taken from Presentation: Chapter 13 Assembly Language for Intel-Based Computers, 4th Edition Kip R. Irvine (c) Pearson Education, 2002. All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.