Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.

Similar presentations


Presentation on theme: "CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University."— Presentation transcript:

1 CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University

2 CS2422 Assembly Language and System Programming Assembly Language for Intel- Based Computers, 5 th Edition Chapter 12: High-Level Language Interface (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. Slides prepared by the author Revision date: June 4, 2006 Kip Irvine

3 2 Chapter Overview  Introduction  Inline Assembly Code  Linking to C/C++ in Protected Mode Examples using Visual C++  Linking to C/C++ in Real-Address Mode

4 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

5 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

6 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

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

8 7 Linking Assembly to C/C++  Basic structure: two modules (1) an assembly contains the procedure (2) C/C++ code that calls the external procedure Parameters are pushed from right to left to stack External names in ASM: use C calling convention, create prototype for procedures called from C/C++.model flat,C AsmFindArray PROTO, srcVal:DWORD, arrayPtr: PTR DWORD Function declare in C/C++: use extern qualifier extern bool AsmFindArr(long n, long array[]);

9 8 Note: Linking Assembly to C++  For linking to C++: "C" specifier must be included to prevent name decoration by the C++ compiler: Because C++ compilers use name decoration to allow uniquely identifying overloaded functions, e.g. int ArraySum( int * p, int count ) would be encoded with the return type, function name, and parameter types as: int_ArraySum_pInt_int extern "C" bool AsmFindArray(long n, long array[]);

10 9 Using Assembly to Optimize C++  Make your C++ compiler produce an assembly language source listing of your program e.g. /FAs command-line option in Visual C++ See textbook 12.3.1 (page 410, Table 12-1) for the Visual C++ command-line options for ASM code generation  Look through the assembly code and Optimize loops for speed Use hardware-level I/O for optimum speed Use BIOS-level I/O for medium speed

11 10 FindArray Example  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 not: #include "findarr.h" bool FindArray(long searchVal, long array[], long count ) { for(int i = 0; i < count; i++) if( searchVal == array[i] ) return true; return false; }

12 11 Code Produced by C++ Compiler  Optimization switch turned off _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 # i = 0 jmp SHORT $L174 $L175: mov eax, DWORD PTR _i$[ebp] add eax, 1 mov DWORD PTR _i$[ebp], eax

13 12 Code Produced by C++ Compiler $L174: mov ecx, DWORD PTR _i$[ebp] cmp ecx, DWORD PTR _count$[ebp] jge SHORT $L176 ; 31 : if( searchVal == array[i] ) mov edx, DWORD PTR _i$[ebp] mov eax, DWORD PTR _array$[ebp] mov ecx, DWORD PTR _searchVal$[ebp] cmp ecx, DWORD PTR [eax+edx*4] jne SHORT $L177 ; 32 : return true; mov al, 1 jmp SHORT $L172 $L177: ; 33 : ; 34 : return false; jmp SHORT $L175

14 13 Code Produced by C++ Compiler $L176: xor al, al; AL = 0 $L172: ; 35 : } mov esp, ebp; restore stack pointer pop ebp ret 0 _FindArray ENDP

15 14 Hand-Coded Assembly Language true = 1 false = 0 ; Stack parameters: srchVal equ [ebp+08] arrayPtr equ [ebp+12] count equ [ebp+16].code _FindArray PROC near push ebp mov ebp,esp push edi mov eax, srchVal; search value mov ecx, count ; number of items mov edi, arrayPtr ; pointer to array

16 15 Hand-Coded Assembly Language repne scasd ; do the search jz returnTrue ; ZF = 1 if found returnFalse: mov al, false jmp short exit returnTrue: mov al, true exit: pop edi pop ebp ret _FindArray ENDP Check pp. 412-413 of textbook for C++ and ASM code

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

18 17 Example: Calling printf (1/2)  C/C++ Program: extern "C" void asmMain( ); int main( ) { double d = 3.5; asmMain( ); return 0; } workaround to force the compiler to load the floating-point library

19 18 Example: Calling printf (2/2)  ASM Program: TITLE asmMain.asm.386.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 Output: 1234567.890

20 19 Summary  Use assembly language to 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


Download ppt "CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University."

Similar presentations


Ads by Google