Assembly Language for Intel-Based Computers, 5 th Edition Chapter 12: High-Level Language Interface (c) Pearson Education, 2006-2007. All rights reserved.

Slides:



Advertisements
Similar presentations
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Conditional Loop Instructions LOOPZ and LOOPE LOOPNZ.
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, 4 th Edition Chapter 6: Conditional Processing (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
Runtime Stack Managed by the CPU, using two registers
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Accessing parameters from the stack and calling functions.
CS2422 Assembly Language and System Programming Procedures Department of Computer Science National Tsing Hua University.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Assembly Language Basic Concepts IA-32 Processor Architecture.
Assembly Language for Intel-Based Computers Chapter 3: Assembly Language Fundamentals Kip Irvine.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
CS2422 Assembly Language & System Programming October 24, 2006.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 3: Assembly Language Fundamentals (c) Pearson Education, All rights reserved. You.
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.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
CS2422 Assembly Language & System Programming November 7, 2006.
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.
Chapter 2 Software Tools and Assembly Language Syntax.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 2: IA-32 Processor Architecture (c) Pearson Education, All rights reserved. You.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
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.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
Assembly Language for Intel-Based Computers, 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
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.
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.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
CSC 221 Computer Organization and Assembly Language
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You.
Assembly Language for x86 Processors 7th Edition
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 18 Linking to External Library The Book’s Link Library Stack Operations.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy.
NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
CSC 221 Computer Organization and Assembly Language
Assembly Language for x86 Processors 6th Edition
High-Level Language Interface
Stack Frames and Advanced Procedures
Assembly Language for Intel-Based Computers, 5th Edition
Multi-modules programming
Miscellaneous Topics.
Assembly Language for Intel-Based Computers, 4th Edition
Presentation transcript:

Assembly Language for Intel-Based Computers, 5 th Edition Chapter 12: High-Level Language Interface (c) Pearson Education, 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. Kip R. Irvine

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Chapter Overview Introduction Inline Assembly Code Linking to C/C++ in Protected Mode Linking to C/C++ in Real-Address Mode

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, What's Next Introduction Inline Assembly Code Linking to C/C++ in Protected Mode Linking to C/C++ in Real-Address Mode

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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. A high-level language program must be compiled for different target platforms.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, _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 Syntax: __asm statement __asm { statement-1 statement-2... statement-n }

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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 For example, OA26h or 0xA26 Use the PTR operator in statements such as inc BYTE PTR [esi] Use the EVEN and ALIGN directives Use LENGTH, TYPE, and SIZE directives

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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.)

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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 programSee the Inline Test demonstration program.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, File Encryption Example Reads a file, encrypts it, and writes the output to another file. The TranslateBuffer function uses an __asm block to define statements that loop through a character array and XOR each character with a predefined value. View the Encode2.cpp program listing

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, What's Next Introduction Inline Assembly Code Linking to C/C++ in Protected Mode Linking to C/C++ in Real-Address Mode

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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 );

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Name Decoration Also known as name mangling. HLL 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. The C++ compiler tells the linker to look for a decorated name. C++ compilers vary in the way they decorate function names.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, What's Next Introduction Inline Assembly Code Linking to C/C++ in Protected Mode Linking to C/C++ in Real-Address Mode

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Linking to Borland C++ We will look at a C++ program that calls an external assembly language procedure named ReadSector Reads a range of sectors from a disk drive Not possible with pure C++ code ASM code uses 16-bit MS-DOS functions Tools: 16-bit version of Borland C Borland TASM 4.0 assembler (included with Borland C++)

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, ReadSector: Sample Output Sector display program. Enter drive number [1=A, 2=B, 3=C, 4=D, 5=E,...]: 1 Starting sector number to read: 0 Number of sectors to read: 20 Reading sectors from Drive 1 Sector 0 FAT {...x..v..V.U."..~..N |.E...F..E.8N$}"....w.r...:f.. |f;..W.u.....V....s.3..F...f..F..V..F....v.`.F..V......^...H...F..N.a....#.r98-t.`....}..at9Nt... nvalid system disk...Disk I/O error...Replace the disk, and then press any key....IOSYSMSDOS

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, ReadSector: Source Code Main C++ program source code ASM ReadSector procedure source code

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Special Section: Optimizing Your Code The 90/10 rule: 90% of a program's CPU time is spent executing 10% of the program's code We will concentrate on optimizing ASM code for speed of execution Loops are the most effective place to optimize code Two simple ways to optimize a loop: Move invariant code out of the loop Substitute registers for variables to reduce the number of memory accesses Take advantage of high-level instructions such as XLAT, SCASB, and MOVSD.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Loop Optimization Example.data days DWORD ? minutesInDay DWORD ? totalMinutes DWORD ? str1 BYTE "Daily total minutes: ",0 We will write a short program that calculates and displays the number of elapsed minutes, over a period of n days. The following variables are used:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Sample Program Output Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: Daily total minutes: View the complete source codeView the complete source code.

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, No optimization. mov days,0 mov totalMinutes,0 L1:; loop contains 15 instructions mov eax,24; minutesInDay = 24 * 60 mov ebx,60 mul ebx mov minutesInDay,eax mov edx,totalMinutes; totalMinutes += minutesInDay add edx,minutesInDay mov totalMinutes,edx mov edx,OFFSET str1; "Daily total minutes: " call WriteString mov eax,totalMinutes; display totalMinutes call WriteInt call Crlf inc days; days++ cmp days,50; if days < 50, jb L1; repeat the loop Version 1

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Move calculation of minutesInDay outside the loop, and assign EDX before the loop. The loop now contains 10 instructions. mov days,0 mov totalMinutes,0 mov eax,24; minutesInDay = 24 * 60 mov ebx,60 mul ebx mov minutesInDay,eax mov edx,OFFSET str1; "Daily total minutes: " L1:mov edx,totalMinutes; totalMinutes += minutesInDay add edx,minutesInDay mov totalMinutes,edx call WriteString; display str1 (offset in EDX) mov eax,totalMinutes; display totalMinutes call WriteInt call Crlf inc days; days++ cmp days,50; if days < 50, jb L1; repeat the loop Version 2

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Move totalMinutes to EAX, use EAX throughout loop. Use constant expresion for minutesInDay calculation. The loop now contains 7 instructions. C_minutesInDay = 24 * 60; constant expression mov days,0 mov totalMinutes,0 mov eax,totalMinutes mov edx,OFFSET str1; "Daily total minutes: " L1:add eax,C_minutesInDay; totalMinutes += minutesInDay call WriteString; display str1 (offset in EDX) call WriteInt; display totalMinutes (EAX) call Crlf inc days; days++ cmp days,50; if days < 50, jb L1; repeat the loop mov totalMinutes,eax; update variable Version 3

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Substitute ECX for the days variable. Remove initial assignments to days and totalMinutes. C_minutesInDay = 24 * 60; constant expression mov eax,0; EAX = totalMinutes mov ecx,0; ECX = days mov edx,OFFSET str1; "Daily total minutes: " L1:; loop contains 7 instructions add eax,C_minutesInDay; totalMinutes += minutesInDay call WriteString; display str1 (offset in EDX) call WriteInt; display totalMinutes (EAX) call Crlf inc ecx; days (ECX)++ cmp ecx,50; if days < 50, jb L1; repeat the loop mov totalMinutes,eax; update variable mov days,ecx; update variable Version 4

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Using Assembly Language to Optimize C++ Find out how to make your C++ compiler produce an assembly language source listing /FAs command-line option in Visual C++, for example Optimize loops for speed Use hardware-level I/O for optimum speed Use BIOS-level I/O for medium speed

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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; } Let's write 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 it is not:

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, _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 $L175: mov eax, DWORD PTR _i$[ebp] add eax, 1 mov DWORD PTR _i$[ebp], eax array address Code Produced by C++ Compiler optimization switch turned off (1 of 3) searcrVal ESP,EBP ret addr EBP i count [EBP − 04] [EBP + 04] [EBP + 08] [EBP + 12] [EBP + 16]

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Code Produced by C++ Compiler $L174: mov ecx, DWORD PTR _i$[ebp] cmp ecx, DWORD PTR _count$[ebp] jge SHORT $L176 ; 31 : if( searchVal == array[i] ) mov edx, DWORD PTR _i$[ebp] mov eax, DWORD PTR _array$[ebp] mov ecx, DWORD PTR _searchVal$[ebp] cmp ecx, DWORD PTR [eax+edx*4] jne SHORT $L177 ; 32 : return true; mov al, 1 jmp SHORT $L172 $L177: ; 33 : ; 34 : return false; jmp SHORT $L175 (2 of 3)

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Code Produced by C++ Compiler $L176: xor al, al; AL = 0 $L172: ; 35 : } mov esp, ebp; restore stack pointer pop ebp ret 0 _FindArray ENDP (3 of 3)

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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 _FindArray 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 array address searcrVal ESP,EBP ret addr EBP i count [EBP − 04] [EBP + 04] [EBP + 08] [EBP + 12] [EBP + 16]

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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 _FindArray ENDP

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, 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

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example: Calling printf (1 of 2) C/C++ Program: extern "C" void asmMain( ); int main( ) { double d = 3.5; asmMain( ); return 0; } workaround to force the compiler to load the floating- point library

Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers 5/e, Example: Calling printf (2 of 2) ASM Program: TITLE asmMain.asm.386.model flat,stdcall.stack 2000.data double1 REAL formatStr BYTE "%.3f",0dh,0ah,0.code asmMain PROC C INVOKE printf, ADDR formatStr, double1 ret asmMain ENDP END Output: