chapter 11 interfacing c and assembly code n.
Download
Skip this Video
Download Presentation
Chapter 11 Interfacing C and Assembly Code

Loading in 2 Seconds...

play fullscreen
1 / 11

Chapter 11 Interfacing C and Assembly Code - PowerPoint PPT Presentation


  • 77 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 11 Interfacing C and Assembly Code' - oprah-cline


Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 11 interfacing c and assembly code
Chapter 11

Interfacing C and Assembly Code

learning objectives
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
Introduction
  • 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.
  • 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:

(1) C code call to the assembly function.

(2) An interrupt can call an assembly function.

(3) Call an assembly instruction using intrinsics.

calling assembly from c
Calling Assembly from C

main ()

{

y = asmFunction (a, b);

}

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

_asmFunction

b b3

calling assembly from c1
Calling Assembly from C

main_c.c

int asm_Function (short, short);

short x = 0x4000, y = 0x2000;

int z;

void main (void)

{

z = asm_Function (x, y);

}

  • Use “_” underscore in assembly for all variables or functions declared in C.
  • Labels also need to be global.

asm_Function.c

int asm_Function (short a, short b)

{

int y;

y = (a * b) << 1;

return y;

}

asm_Function.asm

.global _asm_Function

passing arguments between c and assembly
Passing Arguments between C and Assembly

A

B

0

1

2

3

ret addr

4

arg1/r_val

arg2

5

6

arg3

arg4

7

8

arg5

arg6

9

10

arg7

arg8

11

12

arg9

arg10

13

14

15

  • The following registers are used to pass and return variables when calling an assembly routine from C.
passing arguments between c and assembly1
Passing Arguments between C and Assembly

A

B

4

4

0x8000

0x4000

0x2000

0x2000

5

5

6

6

7

7

8

8

  • Before assembly call.
  • After return from assembly call.
passing arguments between c and assembly2
Passing Arguments between C and Assembly

Problem:

  • The C code will use some or all of the 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 destroyed by the assembly code.
passing arguments between c and assembly3
Passing Arguments between C and Assembly

A

B

Solution:

  • Both the C code and assembly code are responsible for saving some registers if they need to use them.

0

1

2

3

C code automatically saves these registers

4

5

6

7

8

9

10

Assembly code must save these registers - responsibility of the programmer

11

12

13

14

15

interfacing c and assembly examples
Interfacing C and Assembly Examples
  • Setup code written in C and interfaced with critical code written in assembly can be found in the following chapters:
    • \Code\Chapter 14 - Finite Impulse Response Filters
    • \Code\Chapter 16 - Adaptive Filters
  • Setup code written in C and interfaced with critical code written in linear assembly can be found in the following chapters:
    • \Code\Chapter 15 - Infinite Impulse Response Filters
    • \Code\Chapter 17 - Goertzel Algorithm
chapter 11 interfacing c and assembly code end
Chapter 11

Interfacing C and Assembly Code

- End -