Presentation is loading. Please wait.

Presentation is loading. Please wait.

High-Level Language Interface Chapter 17 S. Dandamudi.

Similar presentations


Presentation on theme: "High-Level Language Interface Chapter 17 S. Dandamudi."— Presentation transcript:

1 High-Level Language Interface Chapter 17 S. Dandamudi

2 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

3 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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)

4 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

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

6 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

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

8 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

9 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

10 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

11 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

12 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

13 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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... );

14 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

15 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

16 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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”);

17 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

18 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

19 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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)

20 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

21 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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

22 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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 */ );

23 2005 To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.  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


Download ppt "High-Level Language Interface Chapter 17 S. Dandamudi."

Similar presentations


Ads by Google