CS2422 Assembly Language and System Programming
High-Level Language
Interface
Department of Computer Science
National Tsing Hua University
Assembly Language for IntelBased Computers, 5th Edition
CS2422 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



Introduction
Inline Assembly Code
Linking to C/C++ in Protected Mode


Examples using Visual C++
Linking to C/C++ in Real-Address Mode
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
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
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
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.
6
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[]);
7
Note: Linking Assembly to C++

For linking to C++:

"C" specifier must be included to prevent name
decoration by the C++ compiler:
extern "C" bool AsmFindArray(long n,
long array[]);

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
8
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
9
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;
}
10
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
11
Code Produced by C++ Compiler
$L174:
mov
cmp
jge
; 31
mov
mov
mov
cmp
jne
; 32
mov
jmp
$L177:
; 33
; 34
jmp
ecx, DWORD PTR
ecx, DWORD PTR
SHORT $L176
: if( searchVal
edx, DWORD PTR
eax, DWORD PTR
ecx, DWORD PTR
ecx, DWORD PTR
SHORT $L177
: return true;
al, 1
SHORT $L172
:
: return false;
SHORT $L175
_i$[ebp]
_count$[ebp]
== array[i] )
_i$[ebp]
_array$[ebp]
_searchVal$[ebp]
[eax+edx*4]
12
Code Produced by C++ Compiler
$L176:
xor
al, al
$L172:
; 35
: }
mov esp, ebp
pop ebp
ret 0
_FindArray ENDP
; AL = 0
; restore stack pointer
13
Hand-Coded Assembly Language
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
; search value
; number of items
; pointer to array
14
Hand-Coded Assembly Language
repne scasd
jz
returnTrue
; 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
Check pp. 412-413
of textbook for C++
and ASM code
15
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
16
Example: Calling printf

(1/2)
C/C++ Program:
extern "C" void asmMain( );
int main( )
workaround to force the
{
compiler to load the
double d = 3.5;
floating-point library
asmMain( );
return 0;
}
17
Example: Calling printf

(2/2)
ASM Program:
Output:
TITLE asmMain.asm
1234567.890
.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
18
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
19
Descargar

Chapter 1