Chapter 11 Interfacing C and Assembly Code

11
Chapter 11 Chapter 11 Interfacing C and Assembly Interfacing C and Assembly Code Code

description

Chapter 11 Interfacing C and Assembly Code. Learning Objectives. Different methods exist for interfacing C and assembly code: Calling assembly from C. Interrupt calling assembly routine. Intrinsics. Programming requirements when interfacing code. Introduction. - PowerPoint PPT Presentation

Transcript of Chapter 11 Interfacing C and Assembly Code

Page 1: Chapter 11 Interfacing C and Assembly Code

Chapter 11Chapter 11

Interfacing C and Assembly CodeInterfacing C and Assembly Code

Page 2: Chapter 11 Interfacing C and Assembly Code

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2002

Chapter 11, Slide 2

Learning ObjectivesLearning Objectives

Different methods exist for interfacing Different methods exist for interfacing C and assembly code:C and assembly code: Calling assembly from C.Calling assembly from C. Interrupt calling assembly routine.Interrupt calling assembly routine. Intrinsics.Intrinsics.

Programming requirements when Programming requirements when interfacing code.interfacing code.

Page 3: Chapter 11 Interfacing C and Assembly Code

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2002

Chapter 11, Slide 3

IntroductionIntroduction

This chapter shows how to interface C and assembly and how to use intrinsics.This chapter shows how to interface C and assembly and how to use intrinsics. As a general rule the code written in C is used for initialisation and for non-critical (in terms of speed or size) code.As a general rule the code written in C is used for initialisation and for non-critical (in terms of speed or size) code. Critical code (in terms of speed/size) can be written in assembly or linear assembly.Critical code (in terms of speed/size) can be written in assembly or linear assembly. There are three different ways to interface C and assembly code:There are three different ways to interface C and assembly code:

(1)(1) C code call to the assembly function.C code call to the assembly function.

(2)(2) An interrupt can call an assembly function.An interrupt can call an assembly function.

(3)(3) Call an assembly instruction using intrinsics.Call an assembly instruction using intrinsics.

Page 4: Chapter 11 Interfacing C and Assembly Code

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2002

Chapter 11, Slide 4

Calling Assembly from CCalling Assembly from C

The C and assembly functions share the same resources (e.g. registers).The C and assembly functions share the same resources (e.g. registers). The C and assembly functions may exchange data.The C and assembly functions may exchange data. Therefore code interfacing requires a means of handing-off data and Therefore code interfacing requires a means of handing-off data and

control info and some rules of handling shared registers.control info and some rules of handling shared registers.

main ()main ()

{{

y = asmFunction (a, b);y = asmFunction (a, b);

}}

_asmFunction_asmFunction

bb b3b3

Page 5: Chapter 11 Interfacing C and Assembly Code

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2002

Chapter 11, Slide 5

Calling Assembly from CCalling Assembly from C Use “_” underscore in Use “_” underscore in

assembly for all variables or assembly for all variables or functions declared in C.functions declared in C.

Labels also need to be global.Labels also need to be global.

main_c.cmain_c.c

int asm_Function (short, short);int asm_Function (short, short);

short x = 0x4000, y = 0x2000;short x = 0x4000, y = 0x2000;int z;int z;

void main (void)void main (void){{

z = asm_Function (x, y);z = asm_Function (x, y);}}

asm_Function.casm_Function.c

int asm_Function (short a, short int asm_Function (short a, short b)b){{

int y;int y;y = (a * b) << 1;y = (a * b) << 1;return y;return y;

}}

asm_Function.asmasm_Function.asm

.global _asm_Function.global _asm_Function

Page 6: Chapter 11 Interfacing C and Assembly Code

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2002

Chapter 11, Slide 6

Passing Arguments between C and AssemblyPassing Arguments between C and Assembly

The following registers are The following registers are used to pass and return used to pass and return variables when calling an variables when calling an assembly routine from C.assembly routine from C.

AA BB

arg1/arg1/r_valr_val

arg3arg3

arg5arg5

arg7arg7

arg9arg9

ret addrret addrarg2arg2

arg4arg4

arg6arg6

arg8arg8

arg10arg10

112233445566778899

101011111212131314141515

00

Page 7: Chapter 11 Interfacing C and Assembly Code

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2002

Chapter 11, Slide 7

Passing Arguments between C and AssemblyPassing Arguments between C and Assembly

Before assembly Before assembly call.call.

0x40000x4000 0x20000x20004455667788

AA BB

0x80000x8000 0x20000x20004455667788

After return from After return from assembly call.assembly call.

Page 8: Chapter 11 Interfacing C and Assembly Code

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2002

Chapter 11, Slide 8

Passing Arguments between C and AssemblyPassing Arguments between C and Assembly

Problem:Problem: The C code will use some or all of the registers.The C code will use some or all of the registers. The assembly code may also require the use of some or all registers.The assembly code may also require the use of some or all registers. If nothing is done then on return to the C code some of the values may have been If nothing is done then on return to the C code some of the values may have been

destroyed by the assembly code.destroyed by the assembly code.

Page 9: Chapter 11 Interfacing C and Assembly Code

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2002

Chapter 11, Slide 9

Passing Arguments between C and AssemblyPassing Arguments between C and Assembly

Solution:Solution: Both the C code and assembly Both the C code and assembly

code are responsible for code are responsible for saving some registers if they saving some registers if they need to use them.need to use them.

112233445566778899

101011111212131314141515

00AA BB

C code C code automatically automatically

saves these saves these registersregisters

Assembly Assembly code must code must save these save these registers - registers -

responsibility responsibility of the of the

programmerprogrammer

Page 10: Chapter 11 Interfacing C and Assembly Code

Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2002

Chapter 11, Slide 10

Interfacing C and Assembly ExamplesInterfacing C and Assembly Examples

Setup code written in C and interfaced with Setup code written in C and interfaced with critical code written in critical code written in assembly assembly can be can be found in the following chapters:found in the following chapters:

\Code\Chapter 14 - Finite Impulse Response Filters\Code\Chapter 14 - Finite Impulse Response Filters \Code\Chapter 16 - Adaptive Filters\Code\Chapter 16 - Adaptive Filters

Setup code written in C and interfaced with Setup code written in C and interfaced with critical code written in critical code written in linear assembly linear assembly can can be found in the following chapters:be found in the following chapters:

\Code\Chapter 15 - Infinite Impulse Response Filters\Code\Chapter 15 - Infinite Impulse Response Filters \Code\Chapter 17 - Goertzel Algorithm\Code\Chapter 17 - Goertzel Algorithm

Page 11: Chapter 11 Interfacing C and Assembly Code

Chapter 11Chapter 11

Interfacing C and Assembly CodeInterfacing C and Assembly Code

- End -- End -