High-Level Language Interface Chapter 17 S. Dandamudi.

Slides:



Advertisements
Similar presentations
University of Washington Procedures and Stacks II The Hardware/Software Interface CSE351 Winter 2013.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Inline Assembly Section 1: Recitation 7. In the early days of computing, most programs were written in assembly code. –Unmanageable because No type checking,
1 Assemblers and Linkers Professor Jennifer Rexford COS 217.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Accessing parameters from the stack and calling functions.
1 Assemblers and Linkers CS Goals of This Lecture Compilation process  Compile, assemble, archive, link, execute Assembling  Representing instructions.
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.
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.
High-Level Language Interface Chapter 13 S. Dandamudi.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
Ithaca College 1 Machine-Level Programming X: inline Assembly Comp 21000: Introduction to Computer Systems & Assembly Lang On-Line resources* * See see.
Introduction Chapter 1 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
6.828: PC hardware and x86 Frans Kaashoek
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
1 GCC Inline Assembly (asm) Example 1 : assignment System oriented Programming, B. Hirsbrunner, diuf.unifr.ch/pai/spSession 14 – 26 May 2015 Ref: B. Hirsbrunner:
5-1 Chapter 5 - Languages and the Machine Department of Information Technology, Radford University ITEC 352 Computer Organization Principles of Computer.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Introduction Chapter 1 S. Dandamudi. Outline A user’s view of computer systems What is assembly language? – Relationship to machine language Advantages.
5-1 Chapter 5 - Languages and the Machine Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Principles.
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
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
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Other Processors. Having learnt MIPS, we can learn other major processors. Not going to be able to cover everything; will pick on the interesting aspects.
The x86 Instruction Set Lecture 16 Mon, Mar 14, 2005.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Overview of Assembly Language Chapter 4 S. Dandamudi.
Compiler Construction Code Generation Activation Records
Logical and Bit Operations Chapter 9 S. Dandamudi.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
1 Assembly Language: Function Calls Jennifer Rexford.
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.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
Recursion Chapter 16 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.
NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter
Credits and Disclaimers
Assembly language.
Credits and Disclaimers
CS-401 Computer Architecture & Assembly Language Programming
Other Processors.
Homework In-line Assembly Code Machine Language
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
Assembly Language Programming V: In-line Assembly Code
Introduction to Intel x86-64 Assembly, Architecture, Applications, & Alliteration Xeno Kovah – 2014 xkovah at gmail.
Assembly Language Programming II: C Compiler Calling Sequences
MIPS Procedure Calls CSE 378 – Section 3.
Multi-modules programming
Computer Organization and Assembly Language
Credits and Disclaimers
Credits and Disclaimers
Computer Architecture and System Programming Laboratory
The ‘asm’ construct An introduction to the GNU C/C++ compiler’s obscure syntax for doing inline assembly language.
Presentation transcript:

High-Level Language Interface Chapter 17 S. Dandamudi

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 2 Outline Why program in mixed- mode?  Focus on C and assembly Overview of compiling mixed-mode programs Calling assembly procedures from C  Parameter passing  Returning values  Preserving registers  Globals and externals Illustrative examples Calling C functions from assembly Inline assembly code  AT&T syntax  Simple inline statements  Extended inline statements  Inline examples

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 3 Why Program in Mixed-Mode? 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)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 4 Compiling Mixed-Mode Programs We use C and assembly mixed-mode programming Our emphasis is on the principles Can be generalized to any type of mixed-mode programming To compile nasm –f elf sample2.asm »Creates sample2.o gcc –o sample1.out sample1.c sample2.o »Creates sample1.out executable file

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 5 Compiling Mixed-Mode Programs

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 6 Calling Assembly Procedures from C 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

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 7 Calling Assembly Procedures from C (cont’d) Example: sum(a,b,c,d)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 8 Calling Assembly Procedures from C (cont’d) Returning Values Registers are used to return values Return value typeRegister used 8-, 16-, 32-bit valueEAX 64-bit valueEDX:EAX Floating-point values are discussed in the next chapter

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 9 Calling Assembly Procedures from C (cont’d) Preserving Registers The following registers must be preserved EBP, EBX, ESI, and EDI Other registers  If needed, should be preserved by the calling function

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 10 Calling Assembly Procedures from C (cont’d) Globals and Externals Mixed-mode programming involves at least two program modules »One C module and one assembly module We have to declare those functions and procedures that are not defined in the same module as external »See Section 5.10 Those procedures that are accessed by another modules as global

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 11 Illustrative Examples Example 1  hll_ex1c.c  hll_test.asm Example 2  hll_minmaxc.c  hll_minmaxa.asm Example 3  hll_arraysumc.c  hll_arraysuma.asm

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 12 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 as in add ESP,4 on line 31 in the example program on page 494

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 13 Inline Assembly Assembly language statements are embedded into the C code »Separate assembly module is not necessary Assembly statements are identified by placing the keyword asm We can use ( ) to compound several assembly statements asm( assembly statement assembly statement... );

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 14 Inline Assembly (cont’d) AT&T Syntax  GCC uses this syntax  Register naming »Prefix with % as in %eax  Source and destination order »Reversed mov eax,ebx is written as movl %ebx,%eax  Operand size »Explicit using b, w, l for byte, word, and longword operands

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 15 Inline Assembly (cont’d) AT&T Syntax (cont’d)  Immediate and constant operands »Prefix the operands with $ movb $255,%al movl $0xFFFFFFFF,%eax  Addressing »Use ( ) rather then [ ] mov eax,[ebx] is written as movl (%ebx),%eax »Full protected-mode addressing format imm32(base,index,scale) Computed as imm32 + base + index*scale

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 16 Inline Assembly (cont’d) Simple Inline Statements asm(”incl %eax”); Multiple statements asm(”pushl %eax”); asm(”incl %eax”); asm(”popl %eax”);  Can be Written as asm(”pushl %eax; incl %eax; popl %eax”);  Or as (to add structure) asm(”pushl %eax”; ”incl %eax”; ”popl %eax”);

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 17 Inline Assembly (cont’d) Extended Inline Statements Format asm(assembly code :outputs :inputs :clobber list); Assembly code  Add keyword volatile after asm »if no compiler optimizations are needed

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 18 Inline Assembly (cont’d) Outputs »Format ”=op-constraint” (C-expression) »Example ”=r”(sum) »Output constraints r = register m = memory i = immediate rm = register or memory, ri = register or immediate g = general

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 19 Inline Assembly (cont’d) Register letters LetterRegister set aEAX register bEBX register cECX register dEDX register SESI register DEDI register rAny of the 8 general registers (EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP)

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 20 Inline Assembly (cont’d) Register letters LetterRegister set qAny of four data registers (EAX, EBX, ECX, EDX) AA 64-bit value in EAX and EDX fFloating-point registers tTop floating-point register uSecond top floating-point register

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 21 Inline Assembly (cont’d) Inputs  Similar to how the outputs are specified (with no = sign)  Operands specified in the output and input parts are assigned sequence numbers 0, 1, 2, … »Can be a total of 10 operands  Example asm(“movl %1,%0” :”=r” (sum) /* output */ :”r” (number1) /* input */ ); %0 refers to sum %1 to number1

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 22 Inline Assembly (cont’d) Clobber list  List of registers modified by the assembly code  Lets gcc know of this  Example asm(“movl %0,%eax” : /*no output */ :”r” (number1) /* inputs */ :”%eax” /* clobber list */ );

2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,  S. Dandamudi Chapter 17: Page 23 Inline Assembly (cont’d) Inline examples  Example 1 »hll_ex1_inline.c  Example 2 »Array sum example »hll_arraysum_inline.c  Example 3 »Second version of the last example »hll_arraysum_inline2.c Last slide