Week13 High Level Interfacing.pptx
Week13 High Level Interfacing.pptx
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
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
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 6
What's Next
▸ Introduction
▸ Inline Assembly Code
▸ Linking to C/C++ in Protected Mode
▸ Linking to C/C++ in Real-Address Mode
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.
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
__asm statement
▸ Syntax:
__asm {
statement-1
statement-2
...
statement-n
}
9
Commenting Styles
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
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.)
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.
▸ int main() {
▸ system("cls");
▸ int abc;
▸ __asm{
▸ mov eax,10
▸ add eax,20
▸ mov abc,eax
▸ } 14
What's Next
▸ Introduction
▸ Inline Assembly Code
▸ Linking to C/C++ in Protected Mode
▸ Linking to C/C++ in Real-Address Mode
15
Irvine, Kip R. Assembly Language for x86 Processors 6/e, 2010.
▸ #include <iostream>
▸ using namespace std;
▸ extern "C" int addem (int p1, int p2, int p3);
▸ int main()
▸ {
▸ int total = addem( 10, 15, 25 );
▸ cout << "Total = " << total << endl;
▸ return 0; 17
Sample Code – Assembly code
▸ ; The addem Subroutine (addem.asm)
▸ .386P
▸ .model flat
▸ public _addem
▸ .code
▸ push ebp
▸ mov ebp,esp
18
Thank You So Much
Good Luck…………………!!!
Special Thanks to Class
Representatives
CR & GR for Wonderful Course Collaboration