CS 2422 Assembly Language and System Programming HighLevel
CS 2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University
Assembly Language for Intel. Based Computers, 5 th Edition CS 2422 Assembly Language and System Programming Kip Irvine Chapter 12: High-Level Language Interface Slides 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 u u u Introduction Inline Assembly Code Linking to C/C++ in Protected Mode l u Examples using Visual C++ Linking to C/C++ in Real-Address Mode 2
Why Link ASM and HLL Programs? u Use high-level language for overall project development l u Relieves programmer from low-level details Use assembly language code l l Speed up critical sections of code Access nonstandard hardware devices Write platform-specific code Extend the HLL's capabilities 3
General Conventions u Considerations when calling assembly language procedures from high-level languages: l l l 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 4
Calling Convention u u u 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 5
External Identifiers u u 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. 6
Linking Assembly to C/C++ u Basic structure: two modules (1) an assembly contains the procedure (2) C/C++ code that calls the external procedure l Parameters are pushed from right to left to stack l External names in ASM: use C calling convention, create prototype for procedures called from C/C++. model flat, C Asm. Find. Array PROTO, src. Val: DWORD, array. Ptr: PTR DWORD l Function declare in C/C++: use extern qualifier extern bool Asm. Find. Arr(long n, long array[]); 7
Note: Linking Assembly to C++ u For linking to C++: l "C" specifier must be included to prevent name decoration by the C++ compiler: extern "C" bool Asm. Find. Array(long n, long array[]); l Because C++ compilers use name decoration to allow uniquely identifying overloaded functions, e. g. int Array. Sum( int * p, int count ) would be encoded with the return type, function name, and parameter types as: int_Array. Sum_p. Int_int 8
Using Assembly to Optimize C++ u Make your C++ compiler produce an assembly language source listing of your program l l u 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 l l l Optimize loops for speed Use hardware-level I/O for optimum speed Use BIOS-level I/O for medium speed 9
Find. Array Example u 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 Find. Array(long search. Val, long array[], long count ) { for(int i = 0; i < count; i++) if( search. Val == array[i] ) return true; return false; } 10
Code Produced by C++ Compiler u Optimization switch turned off _search. Val$ = 8 _array$ = 12 _count$ = 16 _i$ = -4 _Find. Array 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 $L 174 $L 175: mov eax, DWORD PTR _i$[ebp] add eax, 1 mov DWORD PTR _i$[ebp], eax 11
Code Produced by C++ Compiler $L 174: mov cmp jge ; 31 mov mov cmp jne ; 32 mov jmp $L 177: ; 33 ; 34 jmp ecx, DWORD PTR SHORT $L 176 : if( search. Val edx, DWORD PTR eax, DWORD PTR ecx, DWORD PTR SHORT $L 177 : return true; al, 1 SHORT $L 172 : : return false; SHORT $L 175 _i$[ebp] _count$[ebp] == array[i] ) _i$[ebp] _array$[ebp] _search. Val$[ebp] [eax+edx*4] 12
Code Produced by C++ Compiler $L 176: xor al, al $L 172: ; 35 : } mov esp, ebp pop ebp ret 0 _Find. Array ENDP ; AL = 0 ; restore stack pointer 13
Hand-Coded Assembly Language true = 1 false = 0 ; Stack parameters: srch. Val equ [ebp+08] array. Ptr equ [ebp+12] count equ [ebp+16]. code _Find. Array push mov mov PROC near ebp, esp edi eax, srch. Val ecx, count edi, array. Ptr ; search value ; number of items ; pointer to array 14
Hand-Coded Assembly Language repne scasd jz return. True ; do the search ; ZF = 1 if found return. False: mov al, false jmp short exit return. True: mov al, true exit: pop edi pop ebp ret _Find. Array ENDP Check pp. 412 -413 of textbook for C++ and ASM code 15
Calling C Library Functions u u u Use the "C" calling convention Rewrite C function prototype in MASM format. Example: int printf(const char *format [ , argument]. . . becomes printf PROTO C, p. String: PTR BYTE, args: VARARG 16
Example: Calling printf u (1/2) C/C++ Program: extern "C" void asm. Main( ); int main( ) workaround to force the { compiler to load the double d = 3. 5; floating-point library asm. Main( ); return 0; } 17
Example: Calling printf u (2/2) ASM Program: Output: TITLE asm. Main. asm 1234567. 890. 386. model flat, stdcall. stack 2000. data double 1 REAL 8 1234567. 890123 format. Str BYTE "%. 3 f", 0 dh, 0 ah, 0. code asm. Main PROC C INVOKE printf, ADDR format. Str, double 1 ret asm. Main ENDP END 18
Summary u Use assembly language to optimize sections of applications written in high-level languages l l u u u inline asm code linked procedures Naming conventions, name decoration Calling convention determined by HLL program OK to call C functions from assembly language 19
- Slides: 20