1 / 23

High-Level Language Interface

High-Level Language Interface. Chapter 17 S. Dandamudi. 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

denzel
Télécharger la présentation

High-Level Language Interface

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. High-Level Language Interface Chapter 17 S. Dandamudi

  2. 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 Outline S. Dandamudi

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

  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 S. Dandamudi

  5. Compiling Mixed-Mode Programs S. Dandamudi

  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 S. Dandamudi

  7. Calling Assembly Procedures from C (cont’d) Example: sum(a,b,c,d) S. Dandamudi

  8. Calling Assembly Procedures from C (cont’d) 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 in the next chapter S. Dandamudi

  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 S. Dandamudi

  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 S. Dandamudi

  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 S. Dandamudi

  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 S. Dandamudi

  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 . . . ); S. Dandamudi

  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 S. Dandamudi

  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 S. Dandamudi

  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”); S. Dandamudi

  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 S. Dandamudi

  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 S. Dandamudi

  19. Inline Assembly (cont’d) • Register letters Letter Register set a EAX register b EBX register c ECX register d EDX register S ESI register D EDI register r Any of the 8 general registers (EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP) S. Dandamudi

  20. Inline Assembly (cont’d) • Register letters Letter Register set q Any of four data registers (EAX, EBX, ECX, EDX) A A 64-bit value in EAX and EDX f Floating-point registers t Top floating-point register u Second top floating-point register S. Dandamudi

  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 S. Dandamudi

  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 */ ); S. Dandamudi

  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 S. Dandamudi

More Related