0% found this document useful (0 votes)
13 views20 pages

Week13 High Level Interfacing

Uploaded by

zainbakra1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views20 pages

Week13 High Level Interfacing

Uploaded by

zainbakra1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Computer Organization

& Assembly Language


- EE2003
Lecture 17
Week 13
Chapter Overview
▸ Introduction
▸ Inline Assembly Code
▸ Linking to C/C++ in Protected Mode
▸ Linking to C/C++ in Real-Address Mode

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

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 */

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.

See the Inline Test demonstration program.


13
Sample Code – inline assembly
▸ #include <stdio.h>

▸ int main() {

▸ system("cls");

▸ int abc;

▸ printf("welcome from from C! \n");

▸ // inline Assembly block

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

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 );
16
Sample Code – C++ code
▸ // Addem Main Program (AddMain.cpp)

▸ #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)

▸ ; This subroutine links to Visual C++.

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

18
Thank You So Much
Good Luck…………………!!!
Special Thanks to Class
Representatives
CR & GR for Wonderful Course Collaboration

You might also like