High-Level Language Interface

Preview:

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

High-Level Language Interface

Chapter 17S. Dandamudi

2005To 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

2005To 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)

2005To 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

2005To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer, 2005.

S. Dandamudi Chapter 17: Page 5

Compiling Mixed-Mode Programs

2005To 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

2005To 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)

2005To 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 type Register used

8-, 16-, 32-bit value EAX

64-bit value EDX:EAX

• Floating-point values are discussed in the next chapter

2005To 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

2005To 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

2005To 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

2005To 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,4on line 31 in the example program on page 494

2005To 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 . . . );

2005To 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

» Reversedmov eax,ebx

is written asmovl %ebx,%eax

Operand size» Explicit using b, w, l for byte, word, and longword operands

2005To 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,%almovl $0xFFFFFFFF,%eax

Addressing» Use ( ) rather then [ ]

mov eax,[ebx] is written asmovl (%ebx),%eax

» Full protected-mode addressing formatimm32(base,index,scale)Computed as imm32 + base + index*scale

2005To 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 Statementsasm(”incl %eax”);

• Multiple statementsasm(”pushl %eax”);asm(”incl %eax”);asm(”popl %eax”);

Can be Written asasm(”pushl %eax; incl %eax; popl %eax”);

Or as (to add structure)asm(”pushl %eax”; ”incl %eax”; ”popl %eax”);

2005To 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

2005To 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 = registerm = memoryi = immediaterm = register or memory, ri = register or immediateg = general

2005To 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 lettersLetter Register seta EAX register b EBX registerc ECX registerd EDX registerS ESI register D EDI registerr Any of the 8 general registers

(EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP)

2005To 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 lettersLetter Register setq Any of four data registers

(EAX, EBX, ECX, EDX) A A 64-bit value in EAX and EDXf Floating-point registers

t Top floating-point registeru Second top floating-point register

2005To 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

Exampleasm(“movl %1,%0” :”=r” (sum) /* output */ :”r” (number1) /* input */ );

%0 refers to sum %1 to number1

2005To 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 */ );

2005To 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

Recommended