Al2ed chapter17

515 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
515
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Al2ed chapter17

  1. 1. High-Level Language Interface Chapter 17 S. Dandamudi
  2. 2. Outline <ul><li>Why program in mixed-mode? </li></ul><ul><ul><li>Focus on C and assembly </li></ul></ul><ul><li>Overview of compiling mixed-mode programs </li></ul><ul><li>Calling assembly procedures from C </li></ul><ul><ul><li>Parameter passing </li></ul></ul><ul><ul><li>Returning values </li></ul></ul><ul><ul><li>Preserving registers </li></ul></ul><ul><ul><li>Globals and externals </li></ul></ul><ul><li>Illustrative examples </li></ul><ul><li>Calling C functions from assembly </li></ul><ul><li>Inline assembly code </li></ul><ul><ul><li>AT&T syntax </li></ul></ul><ul><ul><li>Simple inline statements </li></ul></ul><ul><ul><li>Extended inline statements </li></ul></ul><ul><ul><li>Inline examples </li></ul></ul>
  3. 3. Why Program in Mixed-Mode? <ul><li>Pros and cons of assembly language programming </li></ul><ul><ul><li>Advantages: </li></ul></ul><ul><ul><ul><li>Access to hardware </li></ul></ul></ul><ul><ul><ul><li>Time-efficiency </li></ul></ul></ul><ul><ul><ul><li>Space-efficiency </li></ul></ul></ul><ul><ul><li>Problems: </li></ul></ul><ul><ul><ul><li>Low productivity </li></ul></ul></ul><ul><ul><ul><li>High maintenance cost </li></ul></ul></ul><ul><ul><ul><li>Lack of portability </li></ul></ul></ul><ul><li>As a result, some programs are written in mixed-modem (e.g., system software) </li></ul>
  4. 4. Compiling Mixed-Mode Programs <ul><li>We use C and assembly mixed-mode programming </li></ul><ul><li>Our emphasis is on the principles </li></ul><ul><li>Can be generalized to any type of mixed-mode programming </li></ul><ul><li>To compile </li></ul><ul><ul><li>nasm –f elf sample2.asm </li></ul></ul><ul><ul><ul><li>Creates sample2.o </li></ul></ul></ul><ul><ul><li>gcc –o sample1.out sample1.c sample2.o </li></ul></ul><ul><ul><ul><li>Creates sample1.out executable file </li></ul></ul></ul>
  5. 5. Compiling Mixed-Mode Programs
  6. 6. Calling Assembly Procedures from C <ul><li>Parameter Passing </li></ul><ul><li>Stack is used for parameter passing </li></ul><ul><li>Two ways of pushing arguments onto the stack </li></ul><ul><ul><li>Left-to-right </li></ul></ul><ul><ul><ul><li>Most languages including Basic, Fortran, Pascal use this method </li></ul></ul></ul><ul><ul><ul><li>These languages are called left-pusher languages </li></ul></ul></ul><ul><ul><li>Right-to-left </li></ul></ul><ul><ul><ul><li>C uses this method </li></ul></ul></ul><ul><ul><ul><li>These languages are called right-pusher languages </li></ul></ul></ul>
  7. 7. Calling Assembly Procedures from C (cont’d) <ul><li>Example: </li></ul><ul><li>sum(a,b,c,d) </li></ul>
  8. 8. Calling Assembly Procedures from C (cont’d) <ul><li>Returning Values </li></ul><ul><li>Registers are used to return values </li></ul><ul><li>Return value type Register used </li></ul><ul><li>8-, 16-, 32-bit value EAX </li></ul><ul><li>64-bit value EDX:EAX </li></ul><ul><li>Floating-point values are discussed in the next chapter </li></ul>
  9. 9. Calling Assembly Procedures from C (cont’d) <ul><li>Preserving Registers </li></ul><ul><li>The following registers must be preserved </li></ul><ul><li>EBP, EBX, ESI, and EDI </li></ul><ul><li>Other registers </li></ul><ul><ul><li>If needed, should be preserved by the calling function </li></ul></ul>
  10. 10. Calling Assembly Procedures from C (cont’d) <ul><li>Globals and Externals </li></ul><ul><li>Mixed-mode programming involves at least two program modules </li></ul><ul><ul><ul><li>One C module and one assembly module </li></ul></ul></ul><ul><li>We have to declare those functions and procedures that are not defined in the same module as external </li></ul><ul><ul><ul><li>See Section 5.10 </li></ul></ul></ul><ul><li>Those procedures that are accessed by another modules as global </li></ul>
  11. 11. Illustrative Examples <ul><li>Example 1 </li></ul><ul><ul><li>hll_ex1c.c </li></ul></ul><ul><ul><li>hll_test.asm </li></ul></ul><ul><li>Example 2 </li></ul><ul><ul><li>hll_minmaxc.c </li></ul></ul><ul><ul><li>hll_minmaxa.asm </li></ul></ul><ul><li>Example 3 </li></ul><ul><ul><li>hll_arraysumc.c </li></ul></ul><ul><ul><li>hll_arraysuma.asm </li></ul></ul>
  12. 12. Calling C Functions from Assembly <ul><li>Stack is used to pass parameters (as in our previous discussion) </li></ul><ul><ul><li>Similar mechanism is used to pass parameters and to return values </li></ul></ul><ul><li>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 </li></ul><ul><li>add ESP,4 </li></ul><ul><li>on line 31 in the example program on page 494 </li></ul>
  13. 13. Inline Assembly <ul><li>Assembly language statements are embedded into the C code </li></ul><ul><ul><ul><li>Separate assembly module is not necessary </li></ul></ul></ul><ul><li>Assembly statements are identified by placing the keyword asm </li></ul><ul><li>We can use ( ) to compound several assembly statements </li></ul><ul><li>asm( </li></ul><ul><li> assembly statement assembly statement . . . </li></ul><ul><li> ); </li></ul>
  14. 14. Inline Assembly (cont’d) <ul><li>AT&T Syntax </li></ul><ul><ul><li>GCC uses this syntax </li></ul></ul><ul><ul><li>Register naming </li></ul></ul><ul><ul><ul><li>Prefix with % as in %eax </li></ul></ul></ul><ul><ul><li>Source and destination order </li></ul></ul><ul><ul><ul><li>Reversed </li></ul></ul></ul><ul><ul><ul><ul><li>mov eax,ebx </li></ul></ul></ul></ul><ul><ul><ul><ul><li>is written as </li></ul></ul></ul></ul><ul><ul><ul><ul><li>movl %ebx,%eax </li></ul></ul></ul></ul><ul><ul><li>Operand size </li></ul></ul><ul><ul><ul><li>Explicit using b , w , l for byte, word, and longword operands </li></ul></ul></ul>
  15. 15. Inline Assembly (cont’d) <ul><li>AT&T Syntax (cont’d) </li></ul><ul><ul><li>Immediate and constant operands </li></ul></ul><ul><ul><ul><li>Prefix the operands with $ </li></ul></ul></ul><ul><ul><ul><ul><li>movb $255,%al </li></ul></ul></ul></ul><ul><ul><ul><ul><li>movl $0xFFFFFFFF,%eax </li></ul></ul></ul></ul><ul><ul><li>Addressing </li></ul></ul><ul><ul><ul><li>Use ( ) rather then [ ] </li></ul></ul></ul><ul><ul><ul><ul><li>mov eax,[ebx] </li></ul></ul></ul></ul><ul><ul><ul><ul><li>is written as </li></ul></ul></ul></ul><ul><ul><ul><ul><li>movl (%ebx),%eax </li></ul></ul></ul></ul><ul><ul><ul><li>Full protected-mode addressing format </li></ul></ul></ul><ul><ul><ul><ul><li>imm32(base,index,scale) </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Computed as imm32 + base + index*scale </li></ul></ul></ul></ul>
  16. 16. Inline Assembly (cont’d) <ul><li>Simple Inline Statements </li></ul><ul><ul><li>asm(”incl %eax”); </li></ul></ul><ul><li>Multiple statements </li></ul><ul><ul><ul><li>asm(”pushl %eax”); </li></ul></ul></ul><ul><ul><ul><li>asm(”incl %eax”); </li></ul></ul></ul><ul><ul><ul><li>asm(”popl %eax”); </li></ul></ul></ul><ul><ul><li>Can be Written as </li></ul></ul><ul><ul><ul><li>asm(”pushl %eax; incl %eax; popl %eax”); </li></ul></ul></ul><ul><ul><li>Or as (to add structure) </li></ul></ul><ul><ul><ul><li>asm(”pushl %eax”; </li></ul></ul></ul><ul><ul><ul><li>” incl %eax”; </li></ul></ul></ul><ul><ul><ul><li>” popl %eax”); </li></ul></ul></ul>
  17. 17. Inline Assembly (cont’d) <ul><li>Extended Inline Statements </li></ul><ul><li>Format </li></ul><ul><ul><ul><li>asm(assembly code </li></ul></ul></ul><ul><ul><ul><li>:outputs </li></ul></ul></ul><ul><ul><ul><li>:inputs </li></ul></ul></ul><ul><ul><ul><li>:clobber list); </li></ul></ul></ul><ul><li>Assembly code </li></ul><ul><ul><li>Add keyword volatile after asm </li></ul></ul><ul><ul><ul><li>if no compiler optimizations are needed </li></ul></ul></ul>
  18. 18. Inline Assembly (cont’d) <ul><li>Outputs </li></ul><ul><ul><ul><li>Format </li></ul></ul></ul><ul><ul><ul><ul><li>” =op-constraint” (C-expression) </li></ul></ul></ul></ul><ul><ul><ul><li>Example </li></ul></ul></ul><ul><ul><ul><ul><li>” =r”(sum) </li></ul></ul></ul></ul><ul><ul><ul><li>Output constraints </li></ul></ul></ul><ul><ul><ul><ul><li>r = register </li></ul></ul></ul></ul><ul><ul><ul><ul><li>m = memory </li></ul></ul></ul></ul><ul><ul><ul><ul><li>i = immediate </li></ul></ul></ul></ul><ul><ul><ul><ul><li>rm = register or memory, </li></ul></ul></ul></ul><ul><ul><ul><ul><li>ri = register or immediate </li></ul></ul></ul></ul><ul><ul><ul><ul><li>g = general </li></ul></ul></ul></ul>
  19. 19. Inline Assembly (cont’d) <ul><li>Register letters </li></ul><ul><ul><li>Letter Register set </li></ul></ul><ul><ul><li>a EAX register </li></ul></ul><ul><ul><li>b EBX register </li></ul></ul><ul><ul><li>c ECX register </li></ul></ul><ul><ul><li>d EDX register </li></ul></ul><ul><ul><li>S ESI register </li></ul></ul><ul><ul><li>D EDI register </li></ul></ul><ul><ul><li>r Any of the 8 general registers </li></ul></ul><ul><ul><li>(EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP) </li></ul></ul>
  20. 20. Inline Assembly (cont’d) <ul><li>Register letters </li></ul><ul><ul><li>Letter Register set </li></ul></ul><ul><ul><li>q Any of four data registers </li></ul></ul><ul><ul><li>(EAX, EBX, ECX, EDX) </li></ul></ul><ul><ul><li>A A 64-bit value in EAX and EDX </li></ul></ul><ul><ul><li>f Floating-point registers </li></ul></ul><ul><ul><li>t Top floating-point register </li></ul></ul><ul><ul><li>u Second top floating-point register </li></ul></ul>
  21. 21. Inline Assembly (cont’d) <ul><li>Inputs </li></ul><ul><ul><li>Similar to how the outputs are specified (with no = sign) </li></ul></ul><ul><ul><li>Operands specified in the output and input parts are assigned sequence numbers 0, 1, 2, … </li></ul></ul><ul><ul><ul><li>Can be a total of 10 operands </li></ul></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>asm(“movl %1,%0” </li></ul></ul></ul><ul><ul><ul><li>:”=r” (sum) /* output */ </li></ul></ul></ul><ul><ul><ul><li>:”r” (number1) /* input */ </li></ul></ul></ul><ul><ul><ul><li>); </li></ul></ul></ul>%0 refers to sum %1 to number1
  22. 22. Inline Assembly (cont’d) <ul><li>Clobber list </li></ul><ul><ul><li>List of registers modified by the assembly code </li></ul></ul><ul><ul><li>Lets gcc know of this </li></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><ul><li>asm(“movl %0,%%eax” </li></ul></ul></ul><ul><ul><ul><li>: /*no output */ </li></ul></ul></ul><ul><ul><ul><li>:”r” (number1) /* inputs */ </li></ul></ul></ul><ul><ul><ul><li>:”%eax” /* clobber list */ </li></ul></ul></ul><ul><ul><ul><li>); </li></ul></ul></ul>
  23. 23. Inline Assembly (cont’d) <ul><li>Inline examples </li></ul><ul><ul><li>Example 1 </li></ul></ul><ul><ul><ul><li>hll_ex1_inline.c </li></ul></ul></ul><ul><ul><li>Example 2 </li></ul></ul><ul><ul><ul><li>Array sum example </li></ul></ul></ul><ul><ul><ul><li>hll_arraysum_inline.c </li></ul></ul></ul><ul><ul><li>Example 3 </li></ul></ul><ul><ul><ul><li>Second version of the last example </li></ul></ul></ul><ul><ul><ul><li>hll_arraysum_inline2.c </li></ul></ul></ul>Last slide

×