Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Assembly Language for Intel-Based Computers, 5th Edition Kip R. Irvine Chapter 12: High-Level Language Interface Slide show prepared by the author Revision date: June 4, 2006 (c) Pearson Education, 2006-2007. 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. Chapter Overview • • • • Introduction Inline Assembly Code Linking to C/C++ in Protected Mode Linking to C/C++ in Real-Address Mode Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 2 Why Link ASM and HLL Programs? • Use high-level language for overall project development • Relieves programmer from low-level details • Use assembly language code • • • • Speed up critical sections of code Access nonstandard hardware devices Write platform-specific code Extend the HLL's capabilities Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 3 General Conventions • Considerations when calling assembly language procedures from high-level languages: • Both must use the same naming convention (rules regarding the naming of variables and procedures) • Both must use the same memory model, with compatible segment names • Both must use the same calling convention Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 4 Calling Convention • Identifies specific registers that must be preserved by procedures • Determines how arguments are passed to procedures: in registers, on the stack, in shared memory, etc. • Determines the order in which arguments are passed by calling programs to procedures • Determines whether arguments are passed by value or by reference • Determines how the stack pointer is restored after a procedure call • Determines how functions return values Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 5 External Identifiers • An 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. • The linker resolves references to external identifiers, but can only do so if the same naming convention is used in all program modules. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 6 What's Next • • • • Introduction Inline Assembly Code Linking to C/C++ in Protected Mode Linking to C/C++ in Real-Address Mode Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 7 Inline Assembly Code • Assembly language source code that is inserted directly into a HLL program. • Compilers such as Microsoft Visual C++ and Borland C++ have compiler-specific directives that identify inline ASM code. • Efficient inline code executes quickly because CALL and RET instructions are not required. • Simple to code because there are no external names, memory models, or naming conventions involved. • Decidedly not portable because it is written for a single platform. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 8 _asm Directive in Microsoft Visual C++ • Can be placed at the beginning of a single statement • Or, It can mark the beginning of a block of assembly language statements • Syntax: __asm statement __asm { statement-1 statement-2 ... statement-n } Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 9 Commenting Styles All of the following comment styles are acceptable, but the latter two are preferred: mov mov mov esi,buf esi,buf esi,buf ; initialize index register // initialize index register /* initialize index register */ Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 10 You Can Do the Following . . . • • • • • • • • Use any instruction from the Intel instruction set Use register names as operands Reference function parameters by name Reference code labels and variables that were declared outside the asm block Use numeric literals that incorporate either assembler-style or C-style radix notation Use the PTR operator in statements such as inc BYTE PTR [esi] Use the EVEN and ALIGN directives Use LENGTH, TYPE, and SIZE directives Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 11 You Cannot Do the Following . . . • Use data definition directives such as DB, DW, or BYTE • Use assembler operators other than PTR • Use STRUCT, RECORD, WIDTH, and MASK • Use the OFFSET operator (but LEA is ok) • Use macro directives such as MACRO, REPT, IRC, IRP • Reference segments by name. • (You can, however, use segment register names as operands.) Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 12 Register Usage • In general, you can modify EAX, EBX, ECX, and EDX in your inline code because the compiler does not expect these values to be preserved between statements • Conversely, always save and restore ESI, EDI, and EBP. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 13 // test1.cpp - Test Inline Operators #pragma warning (disable:4101) // disable warning about unreferenced local variables #include <iostream> int main() { std::cout << "(this program generates no output)\n\n"; struct Package { long originZip; // 4 long destinationZip; // 4 float shippingPrice; // 4 }; char myChar; bool myBool; short myShort; int myInt; long myLong; float myFloat; double myDouble; Package myPackage; long double myLongDouble; long myLongArray[10]; Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 14 __asm { mov eax,myPackage.destinationZip; mov eax,LENGTH myInt; // 1 mov eax,LENGTH myLongArray; // 10 mov mov mov mov mov mov mov mov mov mov eax,TYPE myChar; // 1 eax,TYPE myBool; // 1 eax,TYPE myShort; // 2 eax,TYPE myInt; // 4 eax,TYPE myLong; // 4 eax,TYPE myFloat; // 4 eax,TYPE myDouble; // 8 eax,TYPE myPackage; // 12 eax,TYPE myLongDouble; // 8 eax,TYPE myLongArray; // 4 mov eax,SIZE myLong; // 4 mov eax,SIZE myPackage; // 12 mov eax,SIZE myLongArray; // 40 } return 0; } Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 15 File Encryption Example • Reads a file, encrypts it, and writes the output to another file. • The TranslateBuffer function uses an __asm block to define statements that loop through a character array and XOR each character with a predefined value. View the Encode2.cpp program listing Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 16 // ENCODE2.CPP #include <iostream.h> #include <fstream.h> // Translate a buffer of <count> bytes, using an encryption // character <eChar>. Uses an XOR operation (ASM function). const int BUFSIZE = 200; char buffer[BUFSIZE]; int main() { unsigned count; // character count unsigned short encryptCode; cout << "Encryption code [0-255]? "; cin >> encryptCode; unsigned char encryptChar = (unsigned char) encryptCode; ifstream infile( "infile.txt", ios::binary ); ofstream outfile( "outfile.txt", ios::binary ); cout << "Reading INFILE.TXT and creating OUTFILE.TXT...\n"; Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 17 while (!infile.eof() ) { infile.read(buffer, BUFSIZE ); count = infile.gcount(); __asm { lea esi,buffer mov ecx,count mov al, encryptChar L1: xor [esi],al inc esi Loop L1 } // asm outfile.write(buffer, count); } return 0; } Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 18 What's Next • • • • Introduction Inline Assembly Code Linking to C/C++ in Protected Mode Linking to C/C++ in Real-Address Mode Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 19 Linking Assembly Language to Visual 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 ); Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 20 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 vary with inname decoration is that the C++ C++ compilers the way they decorate function compiler names. assumes that your assembly language function's name is decorated. The C++ compiler tells the linker to look for a decorated name. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 21 What's Next • • • • Introduction Inline Assembly Code Linking to C/C++ in Protected Mode Linking to C/C++ in Real-Address Mode Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 22 Linking to Borland C++ • We will look at a C++ program that calls an external assembly language procedure named ReadSector • Reads a range of sectors from a disk drive • Not possible with pure C++ code • ASM code uses 16-bit MS-DOS functions • Tools: • 16-bit version of Borland C++ 5.01 • Borland TASM 4.0 assembler (included with Borland C++) Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 23 ReadSector: Sample Output Sector display program. Enter drive number [1=A, 2=B, 3=C, 4=D, 5=E,...]: 1 Starting sector number to read: 0 Number of sectors to read: 20 Reading sectors 0 - 20 from Drive 1 Sector 0 -------------------------------------------------------.<.(P3j2IHC........@..................)Y...MYDISK FAT12 .3. ....{...x..v..V.U."..~..N..........|.E...F..E.8N$}"....w.r...:f.. |f;..W.u.....V....s.3..F...f..F..V..F....v.`.F..V.. ....^...H...F ..N.a....#.r98-t.`....}..at9Nt... ;.r.....}.......t.<.t.......... ..}....}.....^.f......}.}..E..N....F..V......r....p..B.-`fj.RP.Sj [email protected].^.Iuw....'..I nvalid system disk...Disk I/O error...Replace the disk, and then press any key....IOSYSMSDOS [email protected]. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 24 ReadSector: Source Code Main C++ program source code ASM ReadSector procedure source code Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 25 Special Section: Optimizing Your Code • The 90/10 rule: 90% of a program's CPU time is spent executing 10% of the program's code • We will concentrate on optimizing ASM code for speed of execution • Loops are the most effective place to optimize code • Two simple ways to optimize a loop: • Move invariant code out of the loop • Substitute registers for variables to reduce the number of memory accesses • Take advantage of high-level instructions such as XLAT, SCASB, and MOVSD. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 26 Loop Optimization Example • We will write a short program that calculates and displays the number of elapsed minutes, over a period of n days. • The following variables are used: .data days DWORD ? minutesInDay DWORD ? totalMinutes DWORD ? str1 BYTE "Daily total minutes: ",0 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 27 Sample Program Output Daily Daily Daily Daily Daily Daily Daily Daily . . Daily Daily Daily Daily total total total total total total total total minutes: minutes: minutes: minutes: minutes: minutes: minutes: minutes: +1440 +2880 +4320 +5760 +7200 +8640 +10080 +11520 total total total total minutes: minutes: minutes: minutes: +67680 +69120 +70560 +72000 View the complete source code. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 28 TITLE Loop Optimization Example (Optimize.asm) ; This program calculates and displays the number of ; elapsed minutes per day, up to n days. ; Last update: 07/08/2002 INCLUDE Irvine32.inc .data days DWORD ? minutesInDay DWORD ? totalMinutes DWORD ? str1 BYTE "Daily total minutes: ",0 .code main PROC call Version_1 call Version_2 call Version_3 call Version_4 exit main ENDP Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 29 Version 1 No optimization. mov days,0 mov totalMinutes,0 L1: mov eax,24 mov ebx,60 mul ebx mov minutesInDay,eax mov edx,totalMinutes add edx,minutesInDay mov totalMinutes,edx mov edx,OFFSET str1 call WriteString mov eax,totalMinutes call WriteInt call Crlf inc days cmp days,50 jb L1 ; loop contains 15 instructions ; minutesInDay = 24 * 60 ; totalMinutes += minutesInDay ; "Daily total minutes: " ; display totalMinutes ; days++ ; if days < 50, ; repeat the loop Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 30 Version 2 Move calculation of minutesInDay outside the loop, and assign EDX before the loop. The loop now contains 10 instructions. mov days,0 mov totalMinutes,0 mov eax,24 ; minutesInDay = 24 * 60 mov ebx,60 mul ebx mov minutesInDay,eax mov edx,OFFSET str1 ; "Daily total minutes: " L1: mov edx,totalMinutes add edx,minutesInDay mov totalMinutes,edx call WriteString mov eax,totalMinutes call WriteInt call Crlf inc days cmp days,50 jb L1 ; totalMinutes += minutesInDay ; display str1 (offset in EDX) ; display totalMinutes ; days++ ; if days < 50, ; repeat the loop Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 31 Version 3 Move totalMinutes to EAX, use EAX throughout loop. Use constant expresion for minutesInDay calculation. The loop now contains 7 instructions. C_minutesInDay = 24 * 60 ; constant expression mov days,0 mov totalMinutes,0 mov eax,totalMinutes mov edx,OFFSET str1 ; "Daily total minutes: " L1: add eax,C_minutesInDay call WriteString call WriteInt call Crlf inc days cmp days,50 jb L1 mov totalMinutes,eax ; totalMinutes += minutesInDay ; display str1 (offset in EDX) ; display totalMinutes (EAX) ; days++ ; if days < 50, ; repeat the loop ; update variable Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 32 Version 4 Substitute ECX for the days variable. Remove initial assignments to days and totalMinutes. C_minutesInDay = 24 * 60 mov eax,0 mov ecx,0 mov edx,OFFSET str1 L1: add eax,C_minutesInDay call WriteString call WriteInt call Crlf inc ecx cmp ecx,50 jb L1 mov totalMinutes,eax mov days,ecx ; ; ; ; constant expression EAX = totalMinutes ECX = days "Daily total minutes: " ; ; ; ; loop contains 7 instructions totalMinutes += minutesInDay display str1 (offset in EDX) display totalMinutes (EAX) ; days (ECX)++ ; if days < 50, ; repeat the loop ; update variable ; update variable Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 33 Using Assembly Language to Optimize C++ • Find out how to make your C++ compiler produce an assembly language source listing • /FAs command-line option in Visual C++, for example • Optimize loops for speed • Use hardware-level I/O for optimum speed • Use BIOS-level I/O for medium speed Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 34 FindArray Example Let's write a C++ function that searches for the first matching integer in an array. The function returns true if the integer is found, and false if it is not: #include "findarr.h" bool FindArray( long long { for(int i = 0; i < if( searchVal == return true; return false; } searchVal, long array[], count ) count; i++) array[i] ) Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 35 Code Produced by C++ Compiler optimization switch turned off (1 of 3) _searchVal$ = 8 _array$ = 12 _count$ = 16 _i$ = -4 _FindArray PROC NEAR ; 29 : { push ebp mov ebp, esp push ecx ; 30 : for(int i = 0; i < count; i++) mov DWORD PTR _i$[ebp], 0 jmp SHORT $L174 $L175: mov eax, DWORD PTR _i$[ebp] add eax, 1 mov DWORD PTR _i$[ebp], eax Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 36 Code Produced by C++ Compiler (2 of 3) $L174: mov cmp jge ; 31 : mov mov mov cmp jne ; 32 : mov jmp $L177: ; 33 : ; 34 : jmp ecx, DWORD PTR _i$[ebp] ecx, DWORD PTR _count$[ebp] SHORT $L176 if( searchVal == array[i] ) edx, DWORD PTR _i$[ebp] eax, DWORD PTR _array$[ebp] ecx, DWORD PTR _searchVal$[ebp] ecx, DWORD PTR [eax+edx*4] SHORT $L177 return true; al, 1 SHORT $L172 return false; SHORT $L175 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 37 Code Produced by C++ Compiler (3 of 3) $L176: xor al, al $L172: ; 35 : } mov esp, ebp pop ebp ret 0 _FindArray ENDP Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. ; AL = 0 ; restore stack pointer Web site Examples 38 Hand-Coded Assembly Language (1 of 2) true = 1 false = 0 ; Stack parameters: srchVal equ [ebp+08] arrayPtr equ [ebp+12] count equ [ebp+16] .code _FindArray push mov push mov mov mov PROC near ebp ebp,esp edi eax, srchVal ecx, count edi, arrayPtr Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. ; search value ; number of items ; pointer to array Web site Examples 39 Hand-Coded Assembly Language repne scasd jz returnTrue (2 of 2) ; do the search ; ZF = 1 if found returnFalse: mov al, false jmp short exit returnTrue: mov al, true exit: pop edi pop ebp ret _FindArray ENDP Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 40 Calling C Library Functions • Use the "C" calling convention • Rewrite C function prototype in MASM format. • Example: int printf( const char *format [ , argument]... becomes printf PROTO C, pString:PTR BYTE, args:VARARG Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 41 Example: Calling printf (1 of 2) • C/C++ Program: extern "C" void asmMain( ); int main( ) { double d = 3.5; asmMain( ); return 0; } Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. workaround to force the compiler to load the floatingpoint library Web site Examples 42 Example: Calling printf (2 of 2) • ASM Program: Output: TITLE asmMain.asm .386 1234567.890 .model flat,stdcall .stack 2000 .data double1 REAL8 1234567.890123 formatStr BYTE "%.3f",0dh,0ah,0 .code asmMain PROC C INVOKE printf, ADDR formatStr, double1 ret asmMain ENDP END Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 43 Creating the FindArray Project (using Microsoft Visual Studio) • Run Visual C++.Net and create a new project named FindArray. • Add a blank C++ source file to the project named main.cpp. Type the main() function that calls FindArray. View a sample. • Add a new header file named FindArr.h to the project. This file contains the function prototype for FindArray. View a sample. • Create a file named Scasd.asm and place it in the project directory. This file contains the source code for the FindArray procedure. View a sample. • Use ML.EXE to assemble the Scasd.asm file, producing Scasd.obj. Do not try to link the program. • Insert Scasd.obj into your C++ project. • Build and run the project. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 44 // main.cpp - Testing the FindArray subroutine. #include <stdlib.h> #include <iostream.h> #include "findarr.h" int main() { const unsigned ARRAY_SIZE = 10000; long array[ARRAY_SIZE]; // Fill the array with pseudorandom integers. for(unsigned i = 0; i < ARRAY_SIZE; i++) array[i] = rand(); cout << "\n\n"; long searchVal; cout << "Enter an integer to find [0 to " << RAND_MAX << ": "; cin >> searchVal; if( FindArray( searchVal, array, ARRAY_SIZE )) cout << "Your number was found.\n"; else cout << "Your number was not found.\n"; Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. return 0; Web site Examples 45 Summary • Use assembly language top optimize sections of applications written in high-level languages • inline asm code • linked procedures • Naming conventions, name decoration • Calling convention determined by HLL program • OK to call C functions from assembly language Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 46 The End Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007. Web site Examples 47