CS2422 Assembly Language & System Programming November 7, 2006.

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 12: High-Level Language Interface (c) Pearson Education, All rights reserved.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 12: High-Level Language Interface (c) Pearson Education, All rights reserved.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Accessing parameters from the stack and calling functions.
CS2422 Assembly Language & System Programming October 26, 2006.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Semantics of Calls and Returns
Position Independent Code self sufficiency of combining program.
CS2422 Assembly Language & System Programming October 24, 2006.
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 12: High-Level Language Interface (c) Pearson Education, All rights reserved. You.
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
CS2422 Assembly Language & System Programming September 26, 2006.
Assembly Language for x86 Processors 6th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
High-Level Language Interface Chapter 17 S. Dandamudi.
6.828: PC hardware and x86 Frans Kaashoek
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Lecture 18 Structures and Macros Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
High-Level Language Interface Computer Organization and Assembly Languages Yung-Yu Chuang 2005/12/15 with slides by Kip Irvine.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 12: High-Level Language Interface (c) Pearson Education, All rights reserved.
1 Modular Programming, Stack Frames, and High-Level Language Interfacing Read Chapters 8 and 12 of textbook.
Chapter 12: High-Level Language Interface. 2 Why Link ASM and HLL Programs? Use high-level language for overall project development Relieves programmer.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
CSC 221 Computer Organization and Assembly Language
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Assembly Language for x86 Processors 7th Edition
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
C function call conventions and the stack
CS-401 Computer Architecture & Assembly Language Programming
Assembly Language for x86 Processors 6th Edition
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
CS 301 Fall 2002 Control Structures
Discussion Section – 11/3/2012
Stack Frames and Advanced Procedures
Assembly Language Programming II: C Compiler Calling Sequences
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Multi-modules programming
EECE.3170 Microprocessor Systems Design I
Miscellaneous Topics.
Assembly Language for Intel-Based Computers, 4th Edition
Computer Organization and Assembly Language
CSC 497/583 Advanced Topics in Computer Security
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

CS2422 Assembly Language & System Programming November 7, 2006

Announcement Midterm exam next Tuesday (11/14). Scope: All covered sections in Chapters 1 to 6, 8, and 10.

Today’s Topics High-Level Language Interface –Section 12.1: Introduction –Section 12.3: Examples using Visual C++

Why Link ASM and HLL Programs? Use high-level language for overall project development –Relieves programmer from low-level details Use assembly language code to: –Speed up critical sections of code –Access nonstandard hardware devices –Write platform-specific code –Extend the HLL's capabilities

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 –Both must use the same calling convention

Calling Convention Identifies specific registers that must be preserved by procedures How arguments are passed to procedures: in registers, on the stack, in shared memory? The order in which arguments are passed by calling programs to procedures Arguments passed by value or by reference? Who restores the stack pointer? Calling or called procedure? Determines how functions return values

.MODEL Directive See Section (pp ) for details. AddTwo( 5, 6 ) becomes: Push 6 Push 5 Call AddTwo STDCALL convention Push 6 Push 5 Call AddTwo Add ESP, 8 C convention

Passing Parameters by Reference How to pass an array? Can the called procedure change the parameter? Answer: store the address, not the value of your variables.

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.

How to Link? Resolving the naming issues by EXTERN and PUBLIC Using stack for passing parameters. Follow the C naming and calling conventions –Add underscore (_) prefix to procedure names. –Parameters are pushed from right to left to the stack.

Linking Assembly Language to 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 );

Name Decoration (to answer why extern “ C ” ) Also known as name mangling. C++ compilers do this to uniquely identify overloaded functions. A function such as: int ArraySum( int * p, int count ) would be exported as a decorated name that encodes the return type, function name, and parameter types. For example: int_ArraySum_pInt_int The problem with name decoration is that the C++ compiler assumes that your assembly language function's name is decorated.

FindArray Example #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; } 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:

Generating ASM Code from C Programs See textbook (page 410, Table 12-1) for the Visual C++ command-line options for ASM code generation. The /FAs option is used to generate the code in the next slide. You may need to add this to the Visual C++ Project Options string manually.

Code Produced by C++ Compiler _ 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 jmp SHORT $L174 ; See the handout for the rest optimization switch turned off

Hand-Coded Assembly Language (1 of 2) true = 1 false = 0 ; Stack parameters: srchVal equ [ebp+08] arrayPtr equ [ebp+12] count equ [ebp+16].code _AsmFindArray PROC near push ebp mov ebp,esp push edi mov eax, srchVal; search value mov ecx, count ; number of items mov edi, arrayPtr ; pointer to array

Hand-Coded Assembly Language (2 of 2) repne scasd ; do the search jz returnTrue ; ZF = 1 if found returnFalse: mov al, false jmp short exit returnTrue: mov al, true exit: pop edi pop ebp ret _AsmFindArray ENDP

If you’re using the 5 th ed. of Irvine’s book and Visual Studio 2005, then the FindArray example is in: C:\Irvine\examples\ch12\VisualCPP If you’re using the 4 th ed. or Visual C++ 6.0, then see the next two slides.

Creating the FindArray Project Run Visual C++ and create a project named FindArray. Add a CPP source file to the project named main.cpp. This file should contain the C++ main() function that calls FindArray. Add a new header file named FindArr.h to the project. This file contains the function prototype for FindArray. Create a file named Scasd.asm and place it in the project directory. This file contains the source code for the FindArray procedure. Use ML.EXE /c /Cx /coff to assemble the Scasd.asm file, producing Scasd.obj. Do not try to link the program. Insert Scasd.obj into your C++ project. (Select Add Files... from the Project menu.) Build and run the project. (using Microsoft Visual Studio 6.0)

A Visual C++ Workspace is already in: \Examples\ch12\VisualCPP\VisualCPP.dsw Open the above in Visuall C++ and try out the FindArray Project there. –You may need to copy the folder \Examples\ch12\VisualCPP to your hard disk. –Remember to use “ML /c /Cx /coff” to assemble scasd.asm into scasd.obj first.