Presentation is loading. Please wait.

Presentation is loading. Please wait.

Assembly Language for x86 Processors 7th Edition

Similar presentations


Presentation on theme: "Assembly Language for x86 Processors 7th Edition"— Presentation transcript:

1 Assembly Language for x86 Processors 7th Edition
Kip R. Irvine Chapter 13: High-Level Language Interface Slide show prepared by the author Revised by Zuoliu Ding at Fullerton College, 09/2014 (c) Pearson Education, 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.

2 Chapter Overview Introduction Inline Assembly Code
Linking 32-Bit Assembly Language Code to C/C++ Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015.

3 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 x86 Processors 7/e, 2015.

4 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 x86 Processors 7/e, 2015.

5 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 x86 Processors 7/e, 2015.

6 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. _ ARRAYSUM model flat, Pascal C compiler preserves case and prepends an underscore to the external name Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

7 .MODEL Directive .MODEL directive specifies a program's memory model and model options (language-specifier). Syntax: .MODEL memorymodel [,modeloptions] memorymodel can be one of the following: tiny, small, medium, compact, large, huge, or flat modeloptions includes the language specifier: procedure naming scheme parameter passing conventions Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

8 Memory Models A program's memory model determines the number and sizes of code and data segments. Real-address mode supports tiny, small, medium, compact, large, and huge models. Protected mode supports only the flat model. Small model: code < 64 KB, data (including stack) < 64 KB. All offsets are 16 bits. Flat model: single segment for code and data, up to 4 GB. All offsets are 32 bits. Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

9 Memory Models Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

10 Language Specifiers C: STDCALL
procedure arguments pushed on stack in reverse order (right to left) calling program cleans up the stack Exported name decoration: _name. E.g.: _AddTwo STDCALL called procedure cleans up the stack Exported name decoration: E.g.: Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

11 Examining C Compiler Generated Code
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, size, or full Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

12 arraySum Example The following C++ function calculates the sum of integers in an array and returns the sum: int arraySum( int array[], int count ) { int i; int sum = 0; for(i = 0; i < count; i++) sum += array[i]; return sum; } Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

13 Code Produced by C++ Compiler
optimization switch turned off (1 of 3) _sum$ = -8 ; size = 4 _i$ = -4 ; size = 4 _array$ = 8 ; size = 4 _count$ = 12 ; size = 4 _arraySum PROC ; COMDAT ; 4 : { push ebp mov ebp, esp sub esp, 72; H push ebx push esi push edi [EBP +12] [EBP +8] [EBP +4] EBP => [EBP - 4] [EBP - 8] count array ret addr EBP i sum Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

14 Code Produced by C++ Compiler
(2 of 3) mov DWORD PTR _sum$[ebp], 0 ; 8 : for(i = 0; i < count; i++) mov DWORD PTR _i$[ebp], 0 jmp SHORT mov eax, DWORD PTR _i$[ebp] sdd eax, 1 mov DWORD PTR _i$[ebp], eax cmp eax, DWORD PTR _count$[ebp] jge SHORT Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

15 Code Produced by C++ Compiler
(3 of 3) ; 9 : sum += array[i]; mov eax, DWORD PTR _i$[ebp] mov ecx, DWORD PTR _array$[ebp] mov edx, DWORD PTR _sum$[ebp] add edx, DWORD PTR [ecx+eax*4] mov DWORD PTR _sum$[ebp], edx jmp SHORT ; 10 : ; 11 : return sum; mov eax, DWORD PTR _sum$[ebp] How about turning on an optimization switch? Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

16 What's Next Introduction Inline Assembly Code
Linking 32-Bit Assembly Language Code to C/C++ Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015.

17 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 x86 Processors 6/e, 2010.

18 _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 x86 Processors 6/e, 2010.

19 Commenting Styles All of the following comment styles are acceptable, but the latter two are preferred: mov esi,buf ; initialize index register mov esi,buf // initialize index register mov esi,buf /* initialize index register */ VC++: Avoid assembly-style comments. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

20 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, e.g.,0x2A or 2Ah 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 x86 Processors 6/e, 2010.

21 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 x86 Processors 6/e, 2010.

22 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. Using LENGTH, TYPE, and SIZE See the Inline Test demonstration program. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

23 LENGTH and LENGTHOF LENGTH returns the number of elements in the array specified by the DUP operator Compare LENGTH and LENGTHOF .data myArr DWORD 20 dup(?), 10, 20, 30 myArr2 DWORD 10, 20, 30, 20 dup(?) .code mov eax, length myArr mov ebx, length myArr2 mov ecx, lengthof myArr mov edx, lengthof myArr2 ; 20 ; 1 ; 23 Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

24 File Encryption Example
Reads a file, encrypts it, and writes the output to another file. Loop through a character array and XOR each character with a predefined value. Encode: The TranslateBuffer function uses an __asm block to define statements. TranslateBuffer is called in the loop. Encode_Inline: Omit function call. Directly insert assembly into the loop more efficiently. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

25 Encode_Inline while (!infile.eof() ) { infile.read(buffer, BUFSIZE );
count = infile.gcount(); __asm { lea esi, buffer ;mov esi, dword ptr buffer ? mov ecx,count mov al, encryptCode L1: xor [esi],al inc esi Loop L1 } // asm outfile.write(buffer, count); } Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

26 Encode void TranslateBuffer( char * buf,
unsigned count, unsigned char ch) { __asm { mov esi,buf mov ecx,count mov al,ch L1: xor [esi],al inc esi Loop L1 } // asm } ; Open files... while (!infile.eof() ) { infile.read(buffer, BUFSIZE ); count = infile.gcount(); TranslateBuffer(buffer, count, encryptCode); outfile.write(buffer, count); } ; Close files... Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

27 What's Next Introduction Inline Assembly Code
Linking 32-Bit Assembly Language Code to C/C++ Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015.

28 Linking to C/C++ in Protected Mode
C++ code calls assembly procedures Assembly procedure calls C/C++ functions Multiplication Table Example Calling C++ Lib functions Optimizing assembly code Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

29 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 ); Examples: C: extern bool AsmFindArray(long n, long a[ ], long count); C++: extern “C” bool AsmFindArray(long n, long a[ ], long count); Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

30 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. E.g.: 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. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

31 Creating the IndexOf_ASM Project
(using Microsoft Visual Studio) Run Visual Studio and create a Win32 console project named IndexOf_ASM. Add a CPP source file to the project named asmMain.cpp. This file should contain the C++ main() function that calls assembly IndexOf(). Add a new header file named asmIndexof.h to the project. This file contains the function prototype for IndexOf(). Add the asmIndexOf.asm file to your project. This file contains the assembly source code for the IndexOf() procedure. Build and run the project. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

32 Performance: C++ and ASM
Example: Run asmIndexOf.asm and cppIndexOf.cpp Compare Non optimized and optimized C++ code Demo: Check cppIndexOf.cpp lsting and disassembly How about Maximize Speed (/O2) in C++? Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

33 Assembly Procedure Calls C++ Functions
Why asm calls C++ code? C++ input/output is more flexible while asm so hard Working with C++ floating-point numbers is easy Extensive Math Lib in C++ Programming Consideration: Must start the program from C/C++ to allow initialization. Function prototypes Asm module with .MODEL directive Function return value in AL, AX, EAX, EDX:EAX or stack VS project properties: Lib input, dependency Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

34 Ex: Multiplication Table, main.cpp (1 of 2)
extern "C" { // external ASM procedures: void DisplayTable(); void SetTextOutColor( unsigned color ); // local C++ functions: int askForInteger(); void showInt( int value, int width ); } int main() { SetTextOutColor( 0x1E ); // yellow on blue DisplayTable(); // call ASM procedure return 0; Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

35 Ex: Multiplication Table, main.cpp (2 of 2)
// Prompt the user for an integer. int askForInteger() { int n; cout << "Enter an integer between 1 and 90,000: "; cin >> n; return n; } // Display a signed integer with a specified width. void showInt( int value, int width ) cout << setw(width) << value; Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

36 Ex: Multiplication Table, subr.asm
SetTextOutColor PROC C, color:DWORD mov eax,color call SetTextColor ; call Irvine32.lib call Clrscr ret SetTextOutColor ENDP DisplayTable PROC C INVOKE askForInteger ; call C++ function mov intVal,eax ; save the integer mov ecx,ENDING_POWER ; loop counter L1: push ecx ; save loop counter shl intVal, ; multiply by 2 INVOKE showInt,intVal,OUT_WIDTH ; call C++ function call Crlf pop ecx ; restore loop counter loop L1 DisplayTable ENDP Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

37 Ex: Multiplication Table, Output
Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

38 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 Similarly: scanf PROTO C, format:PTR BYTE, args:VARARG Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

39 Example: Calling printf (1 of 2)
C/C++ Program: extern "C" void asmMain( ); int main( ) { asmMain( ); return 0; } Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

40 Example: Calling printf (2 of 2)
ASM Program: TITLE asmMain.asm .386 .model flat,stdcall .stack 2000 .data double1 REAL formatStr BYTE "%.3f",0dh,0ah,0 .code asmMain PROC C INVOKE printf, ADDR formatStr, double1 ret asmMain ENDP END Output: Demo: Printf_Example Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

41 Example: DirectoryListing
Calls the following Standard C library functions: system PROTO, pCommand:PTR BYTE printf PROTO, pString:PTR BYTE, args:VARARG scanf PROTO, pFormat:PTR BYTE, args:VARARG fopen PROTO, filename:PTR BYTE, mode:PTR BYTE fclose PROTO, pFile:DWORD Demo see asmMain.asm Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

42 Example: addem.asm Manually use C naming decoration:
How about std call? .386P .model flat public _addem .code _addem proc near      push   ebp      mov    ebp,esp      mov    eax,[ebp+16] ; first argument     add    eax,[ebp+12] ; second argument    add    eax,[ebp+8]  ; third argument     pop    ebp      ret                    _addem endp end Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 2007.

43 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 SCASB and MOVSD. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

44 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 x86 Processors 6/e, 2010.

45 Sample Program Output View the complete source code.
Daily total minutes: +1440 Daily total minutes: +2880 Daily total minutes: +4320 Daily total minutes: +5760 Daily total minutes: +7200 Daily total minutes: +8640 Daily total minutes: Daily total minutes: . Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: View the complete source code. Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

46 Version 1 No optimization. mov days,0 mov totalMinutes,0
L1: ; loop contains 15 instructions mov eax,24 ; minutesInDay = 24 * 60 mov ebx,60 mul ebx mov minutesInDay,eax mov edx,totalMinutes ; totalMinutes += minutesInDay add edx,minutesInDay mov totalMinutes,edx mov edx,OFFSET str1 ; "Daily total minutes: " call WriteString mov eax,totalMinutes ; display totalMinutes call WriteInt call Crlf inc days ; days++ cmp days,50 ; if days < 50, jb L1 ; repeat the loop Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

47 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 ; totalMinutes += minutesInDay add edx,minutesInDay mov totalMinutes,edx call WriteString ; display str1 (offset in EDX) mov eax,totalMinutes ; display totalMinutes call WriteInt call Crlf inc days ; days++ cmp days,50 ; if days < 50, jb L1 ; repeat the loop Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

48 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 ; totalMinutes += minutesInDay call WriteString ; display str1 (offset in EDX) call WriteInt ; display totalMinutes (EAX) call Crlf inc days ; days++ cmp days,50 ; if days < 50, jb L1 ; repeat the loop mov totalMinutes,eax ; update variable Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

49 Version 4 Substitute ECX for the days variable. Remove initial assignments to days and totalMinutes. C_minutesInDay = 24 * 60 ; constant expression mov eax,0 ; EAX = totalMinutes mov ecx,0 ; ECX = days mov edx,OFFSET str1 ; "Daily total minutes: " L1: ; loop contains 7 instructions add eax,C_minutesInDay ; totalMinutes += minutesInDay call WriteString ; display str1 (offset in EDX) call WriteInt ; display totalMinutes (EAX) call Crlf inc ecx ; days (ECX)++ cmp ecx,50 ; if days < 50, jb L1 ; repeat the loop mov totalMinutes,eax ; update variable mov days,ecx ; update variable Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.

50 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 x86 Processors 7/e, 2015.

51 The End Irvine, Kip R. Assembly Language for x86 Processors 7/e, 2015.


Download ppt "Assembly Language for x86 Processors 7th Edition"

Similar presentations


Ads by Google