Docsity

High-level Language Interface - Computer Organization and Assembly Language - Lecture Slides, Slides of Assembly Language Programming

Partial preview of the text

Download High-level Language Interface - Computer Organization and Assembly Language - Lecture Slides and more Slides Assembly Language Programming in PDF only on Docsity!

Computer Organization &

Assembly Languages

High-level Language Interface

Chapter Overview

 Introduction

 Inline Assembly Code

 Linking to C/C++ Program

 Code Optimization

Why Link ASM and HLL Programs?

 Pros and cons of assembly language programming

 Advantages:  Access to hardware  Time-efficiency  Space-efficiency  Problems:  Low productivity  High maintenance cost  Lack of portability

 As a result, some programs are written in mixed-

modem (e.g., system software)

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

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.

Calling Assembly from HLL

Parameter Passing

 Stack is used for parameter passing

 Two ways of pushing arguments onto the stack

 Left-to-right  Most languages including Basic, Fortran, Pascal use this method  These languages are called left-pusher languages  Right-to-left  C uses this method  These languages are called right-pusher languages

Calling Assembly from HLL

Returning Values

 Registers are used to return values

Return value type Register used

8-, 16-, 32-bit value EAX

64-bit value EDX:EAX

 Floating-point values are discussed later

Calling Assembly from HLL

Preserving Registers

 In general, the following registers must be

preserved

EBP, ESI, and EDI

 Other registers

 If needed, should be preserved by the calling function

Calling C Functions from Assembly

 Stack is used to pass parameters (as in our

previous discussion)

 Similar mechanism is used to pass parameters and to return values

 Since C makes the calling procedure responsible

for clearing the stack of the parameters, make sure

to clear the parameters after the call instruction

What's Next

 Introduction

 Inline Assembly Code

 Linking to C/C++ Program

 Code Optimization

_asm Directive in MS 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

 Syntax:

__asm statement __asm { statement- statement- ... statement-n }

Commenting Styles

mov esi,buf ; initialize index register mov esi,buf // initialize index register mov esi,buf / initialize index register /

All of the following comment styles are acceptable, but the latter two are preferred:

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

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.