180909 ajay patil 1 tms320c6000 assembly language and its rules assignment one of the simplest...

101
180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment Assignment One of the simplest operations in C One of the simplest operations in C is to assign a constant to a is to assign a constant to a variable: variable: int x; int x; x = 10; x = 10; The variable The variable x x will contain the will contain the value 10 decimal. value 10 decimal.

Upload: brice-little

Post on 13-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

1TMS320C6000 Assembly Language and its Rules

AssignmentAssignment

One of the simplest operations in C is to One of the simplest operations in C is to assign a constant to a variable:assign a constant to a variable:

int x;int x;

x = 10;x = 10;

The variable The variable xx will contain the value 10 will contain the value 10 decimal.decimal.

Page 2: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

2TMS320C6000 Assembly Language and its Rules

Load a Constant 1 of 4Load a Constant 1 of 4

We will use register A1 to hold variable We will use register A1 to hold variable xx:: In assembly language we write:In assembly language we write:

MVK 10, A1;MVK 10, A1; The instruction The instruction MVKMVK moves (copies) the moves (copies) the

constant 10 into register A1constant 10 into register A1 Register A1 now contains 00000000AhRegister A1 now contains 00000000Ah

Page 3: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

3TMS320C6000 Assembly Language and its Rules

LoadLoad a Constant a Constant 2 of 42 of 4

The correct syntax is number then The correct syntax is number then register. The number can also be in register. The number can also be in hexadecimal: hexadecimal:

MVK 10, A1; MVK 10, A1; MVK 0xA, A1; MVK 0xA, A1; MVK 0Ah, A1; MVK 0Ah, A1; Do not use # Do not use #

MVK #10, A1; XMVK #10, A1; X

Page 4: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

4TMS320C6000 Assembly Language and its Rules

LoadLoad a Constant a Constant 3 of 43 of 4

To load full 32 bits of a register needs 2 To load full 32 bits of a register needs 2 instructions: instructions:

MVK 0x5678, B2; MVK 0x5678, B2;

MVKLH 0x1234, B2;MVKLH 0x1234, B2; Register B2 now contains 12345678hRegister B2 now contains 12345678h

Page 5: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

5TMS320C6000 Assembly Language and its Rules

LoadLoad a Constant a Constant 4 of 44 of 4

To load the full 32 bits of a register with 0 To load the full 32 bits of a register with 0 (zero) requires only a single instruction: (zero) requires only a single instruction:

ZERO B2; ZERO B2;

Register B2 now contains 00000000hRegister B2 now contains 00000000h

Page 6: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

6TMS320C6000 Assembly Language and its Rules

Incrementing a Register 1 of 2Incrementing a Register 1 of 2

To increment a variable in C we can write: To increment a variable in C we can write:

int x;int x;

x++;x++;

This adds 1 to the value of the variable This adds 1 to the value of the variable x x

Page 7: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

7TMS320C6000 Assembly Language and its Rules

Incrementing a Register 1 of 2Incrementing a Register 1 of 2

In assembly language we use the In assembly language we use the instruction instruction ADDKADDK (add constant) (add constant)

ADDK 1, A2ADDK 1, A2 ; ;

This adds the constant 1 to the contents of This adds the constant 1 to the contents of register A2register A2

Page 8: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

8TMS320C6000 Assembly Language and its Rules

Decrementing a Register 1 of 2Decrementing a Register 1 of 2

To decrement a variable in C we can To decrement a variable in C we can write: write:

int x;int x;

x--; x--;

This subtracts 1 from the variableThis subtracts 1 from the variable x. x.

Page 9: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

9TMS320C6000 Assembly Language and its Rules

Decrementing a Register 2 of 2Decrementing a Register 2 of 2

In assembly language we again use the In assembly language we again use the instruction instruction ADDKADDK (add constant) (add constant)

ADDK -1, A2;ADDK -1, A2;

This adds the constant -1 to the contents This adds the constant -1 to the contents of register A2of register A2

There is no such instruction as There is no such instruction as SUBKSUBK

Page 10: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

10TMS320C6000 Assembly Language and its Rules

No OperationNo Operation

The TMS320C6000 provides an instruction The TMS320C6000 provides an instruction that does nothing except take time. This is that does nothing except take time. This is called called NOPNOP (No operation) (No operation)

NOP NOP ; ;

If we want to execute 4 If we want to execute 4 NOPNOP instructions instructions one after another we can write:one after another we can write:NOP 4;NOP 4;

This instruction can be used to generate This instruction can be used to generate time delays. time delays.

Page 11: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

11TMS320C6000 Assembly Language and its Rules

Topic TwoTopic Two

Controlling program flowControlling program flow

Page 12: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

12TMS320C6000 Assembly Language and its Rules

Testing ConditionsTesting Conditions 1 of 5 1 of 5

The if-else construct is widely used in C.The if-else construct is widely used in C. Consider the following simple piece ofConsider the following simple piece of code:code:

int x, y ;int x, y ; if ( x != 0 )if ( x != 0 ) { y++; }{ y++; }

This means if This means if xx is not equal to zero, then is not equal to zero, then increment variable increment variable yy. .

Page 13: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

13TMS320C6000 Assembly Language and its Rules

Testing Conditions 2 of 5Testing Conditions 2 of 5

The assembler provides a neat way to do The assembler provides a neat way to do this. Assuming this. Assuming xx is stored in register A1 is stored in register A1 and and yy is stored in register A2: is stored in register A2:

[A1] ADDK 1, A2;[A1] ADDK 1, A2;

The term in [ ] is the condition to be The term in [ ] is the condition to be tested. If the condition is A1 is not equal tested. If the condition is A1 is not equal to zero is true, add 1 to the value in A2. to zero is true, add 1 to the value in A2. Otherwise do nothing.Otherwise do nothing.

Page 14: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

14TMS320C6000 Assembly Language and its Rules

Testing Conditions 3 of 5Testing Conditions 3 of 5

Consider another piece of C code:Consider another piece of C code:

int x, y ;int x, y ;

if ( x == 0 )if ( x == 0 )

{ y--; }{ y--; }

This means if This means if xx is equal to zero, is equal to zero, decrement decrement yy..

Page 15: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

15TMS320C6000 Assembly Language and its Rules

Testing Conditions 4 of 5Testing Conditions 4 of 5

Again the assembler provides a neat way Again the assembler provides a neat way to do this. Assuming to do this. Assuming xx is stored in register is stored in register A1 and A1 and yy is stored in A2: is stored in A2:

[!A1] ADDK -1, A2;[!A1] ADDK -1, A2;

The term in [ ] is the condition to be The term in [ ] is the condition to be tested. If the condition is A1 is equal to tested. If the condition is A1 is equal to zero is true, add -1 to the value in A2. zero is true, add -1 to the value in A2. Otherwise do nothing.Otherwise do nothing.

Page 16: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

16TMS320C6000 Assembly Language and its Rules

Testing Conditions 5 of 5Testing Conditions 5 of 5

The test can use register A1, A2, B0, B1 The test can use register A1, A2, B0, B1 or B2:or B2:

[A1] MVK 10, A2; [A1] MVK 10, A2; [A0] MVK 10, A2; X[A0] MVK 10, A2; X

[A3] MVK 10, A2; X[A3] MVK 10, A2; X

[!B0] MVK 10, A2; [!B0] MVK 10, A2; [B1] MVK 10, A2; [B1] MVK 10, A2;

[B3] MVK 10, A2; X[B3] MVK 10, A2; X

Page 17: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

17TMS320C6000 Assembly Language and its Rules

Branch InstructionsBranch Instructions 1 of 3 1 of 3

Program execution can forced to a Program execution can forced to a different place using the different place using the BB (branch) (branch) instruction:instruction:

label: B label;label: B label;

When the When the BB (branch) is reached, the next (branch) is reached, the next instruction to be executed will be at the instruction to be executed will be at the address address labellabel. It is similar to the . It is similar to the gotogoto instruction in C.instruction in C.

Page 18: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

18TMS320C6000 Assembly Language and its Rules

Branch InstructionsBranch Instructions 2 of 3 2 of 3

Rather than using a label with the Rather than using a label with the instruction instruction BB, a register can be used. , a register can be used.

MVKH label, B3;MVKH label, B3;

MVKL label, B3;MVKL label, B3;

B B3;B B3;

This method is used by the C compiler, This method is used by the C compiler, usually with B3, to return from a function.usually with B3, to return from a function.

Page 19: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

19TMS320C6000 Assembly Language and its Rules

Branch InstructionsBranch Instructions 3 of 3 3 of 3

The instruction The instruction BB can also be combined can also be combined with a test for a condition.with a test for a condition.

label: ADDK 1, A3;label: ADDK 1, A3;

[A1] B label;[A1] B label;

When the When the BB (branch) is reached, the next (branch) is reached, the next instruction to be executed will be at the instruction to be executed will be at the address address label, label, but only if A1 is non-zero.but only if A1 is non-zero.

Page 20: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

20TMS320C6000 Assembly Language and its Rules

Implementing a Delay Loop 1 of 2Implementing a Delay Loop 1 of 2

In C, a delay loop can be implemented using In C, a delay loop can be implemented using the do-while construct:the do-while construct:

int i = 10;int i = 10; do { do { i--;i--; } while (i != 0);} while (i != 0);

We start with We start with i = 10i = 10. Every time through the . Every time through the loop i is decremented. When loop i is decremented. When i == 0i == 0 then the then the loop terminates. loop terminates.

Page 21: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

21TMS320C6000 Assembly Language and its Rules

Implementing a Delay Loop 2 of 2Implementing a Delay Loop 2 of 2

In assembly language, we can use A1 to In assembly language, we can use A1 to hold hold ii. This can be decremented and . This can be decremented and tested:tested:

MVK 10, A1 ; A1 = 10MVK 10, A1 ; A1 = 10loop: ADDK –1, A1; Decrement A1loop: ADDK –1, A1; Decrement A1 [A1] B loop; Branch to loop [A1] B loop; Branch to loop We start with A1 = 10. Every time through We start with A1 = 10. Every time through

the loop A1 is decremented. When A1 == 0 the loop A1 is decremented. When A1 == 0 then the loop terminates. then the loop terminates.

Page 22: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

22TMS320C6000 Assembly Language and its Rules

Topic ThreeTopic Three

Allocating storage for variables and Allocating storage for variables and constants.constants.

Page 23: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

23TMS320C6000 Assembly Language and its Rules

To Declare a Variable 1 of 2 To Declare a Variable 1 of 2

In C code, a 32-bit variable can be In C code, a 32-bit variable can be declared as follows: declared as follows:

int x;int x;

In assembly language use:In assembly language use:

x: .usect ".far",4, 4x: .usect ".far",4, 4

Page 24: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

24TMS320C6000 Assembly Language and its Rules

To Declare a Variable 2 of 2To Declare a Variable 2 of 2

This means: This means:

x: x: A label. Where to find variableA label. Where to find variable .usect .usect In un-initialised dataIn un-initialised data ".far", ".far", Large memory modelLarge memory model 4,4, How many bytesHow many bytes

4; 4; Align on 4-byte boundaryAlign on 4-byte boundary

Page 25: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

25TMS320C6000 Assembly Language and its Rules

To Declare a Buffer 1 of 2To Declare a Buffer 1 of 2

In C code, a 32-element buffer can be In C code, a 32-element buffer can be declared as an array: declared as an array:

int buffer[32];int buffer[32];

In assembly language use:In assembly language use:

buffer: .usect ".far",128, 4buffer: .usect ".far",128, 4

Page 26: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

26TMS320C6000 Assembly Language and its Rules

To Declare a Buffer 2 of 2To Declare a Buffer 2 of 2

This means: This means:

buffer: buffer: A label. Where to find dataA label. Where to find data .usect .usect In un-initialised dataIn un-initialised data ".far", ".far", Large memory modelLarge memory model 128,128,How many bytesHow many bytes 4; 4; Align on 4-byte boundaryAlign on 4-byte boundary

Page 27: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

27TMS320C6000 Assembly Language and its Rules

To Declare Constants 1 of 3To Declare Constants 1 of 3

In C code, an array of constants can be In C code, an array of constants can be declared as: declared as:

const int constants[5] = const int constants[5] = {1,2,3,4,5};{1,2,3,4,5};

This is an array of 5 read-only constants This is an array of 5 read-only constants of value 1, 2, 3, 4 and 5.of value 1, 2, 3, 4 and 5.

Page 28: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

28TMS320C6000 Assembly Language and its Rules

To Declare Constants 2 of 3To Declare Constants 2 of 3

In assembly language use:In assembly language use:

.sect.sect ".const" ".const".align 4.align 4

coefficients:coefficients:.field .field 1, 32 ;1, 32 ;.field .field 2, 32 ;2, 32 ;.filed .filed 3, 32 ;3, 32 ;.field .field 4, 32 ;4, 32 ;.field .field 5, 32 ;5, 32 ;

Page 29: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

29TMS320C6000 Assembly Language and its Rules

To Declare Constants 3 of 3To Declare Constants 3 of 3

Here Here .sect “.const”.sect “.const” tells the linker tells the linker where in memory to store the values.where in memory to store the values.

.align 4 .align 4 means align on a 4-byte means align on a 4-byte boundary.boundary.

The constants are found at the address The constants are found at the address coefficients.coefficients.

Each constant is declared as a field of a Each constant is declared as a field of a given value, and size 32 bits.given value, and size 32 bits.

.field .field 3, 32 3, 32

Page 30: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

30TMS320C6000 Assembly Language and its Rules

Topic FourTopic Four

Using pointers.Using pointers.

Page 31: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

31TMS320C6000 Assembly Language and its Rules

Pointing to a Buffer 1 of 3Pointing to a Buffer 1 of 3

To set up a pointer to a buffer in C we To set up a pointer to a buffer in C we write:write:

int buffer[32];int buffer[32];

int *ptr;int *ptr;

ptr = &buffer[0];ptr = &buffer[0];

The pointer The pointer ptrptr is given the address of is given the address of the start of the buffer.the start of the buffer.

Page 32: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

32TMS320C6000 Assembly Language and its Rules

Pointing to a Buffer 2 of 3Pointing to a Buffer 2 of 3

When using TMS320C6000 When using TMS320C6000 assembly language, it is usual assembly language, it is usual practice to use the following practice to use the following registers as pointers:registers as pointers:

A4, A5, A6, A7A4, A5, A6, A7 B4, B5, B6, B7B4, B5, B6, B7

These registers also support These registers also support circular addressing.circular addressing.

Page 33: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

33TMS320C6000 Assembly Language and its Rules

Pointing to a Buffer 2 of 3Pointing to a Buffer 2 of 3 To use register A4 as the pointer To use register A4 as the pointer

to the buffer:to the buffer:

buffer: .usect ".far",128, 4 buffer: .usect ".far",128, 4 MVKL buffer, A4MVKL buffer, A4 MVKH buffer, A4MVKH buffer, A4

First instruction First instruction MVKLMVKL writes to writes to the low half of register A4the low half of register A4

The second instruction The second instruction MVKHMVKH writes to the high half of register writes to the high half of register A4A4

Page 34: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

34TMS320C6000 Assembly Language and its Rules

Moving Data to a Register 1 of 2Moving Data to a Register 1 of 2

We can load a register with the 32-bit We can load a register with the 32-bit contents of a data memory address. contents of a data memory address. Assume that register A4 points to Assume that register A4 points to buffer[0]buffer[0]

LDW *A4, A5; LDW *A4, A5;

The instruction The instruction LDWLDW (load word) copies a (load word) copies a word of data from word of data from buffer[0]buffer[0] to register A5 to register A5

Here Here WW = word = 32 bits = word = 32 bits

Page 35: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

35TMS320C6000 Assembly Language and its Rules

Moving Data to a Register 2 of 2Moving Data to a Register 2 of 2

The instruction The instruction LDWLDW takes 4 cycles to get takes 4 cycles to get the data, which makes it slow. Care is the data, which makes it slow. Care is needed to wait the required time, for needed to wait the required time, for example using 4 example using 4 NOPNOPs. s.

LDW *A4, A5;LDW *A4, A5;

NOP 4 ; A5 not ready NOP 4 ; A5 not ready ADDK 2, A5 ; A5 now readyADDK 2, A5 ; A5 now ready

Page 36: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

36TMS320C6000 Assembly Language and its Rules

Moving Data from a RegisterMoving Data from a Register

We can store the 32-bit contents of a We can store the 32-bit contents of a register at an address in data memory. register at an address in data memory. Assume that register A5 points to Assume that register A5 points to buffer[0]:buffer[0]:

STW A4, *A5; STW A4, *A5;

The instruction The instruction STWSTW (store word) copies a (store word) copies a word of data from register A4 to buffer[0]. word of data from register A4 to buffer[0]. The data are available immediately.The data are available immediately.

Here Here WW = word = 32 bits = word = 32 bits

Page 37: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

37TMS320C6000 Assembly Language and its Rules

Operations on Pointers 1 of 4Operations on Pointers 1 of 4

Several pointer operations are possible in Several pointer operations are possible in C:C:

*ptr++; *ptr++; Post-incrementPost-increment *ptr--; *ptr--; Post-decrementPost-decrement ++*ptr; ++*ptr; Pre-increment Pre-increment --*ptr;--*ptr; Pre-decrement Pre-decrement

Page 38: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

38TMS320C6000 Assembly Language and its Rules

Operations on Pointers 2 of 4Operations on Pointers 2 of 4

The same pointer operations are available The same pointer operations are available in assembly language:in assembly language:

*A4++; *A4++; Post-incrementPost-increment *A5--; *A5--; Post-decrementPost-decrement ++*B6; ++*B6; Pre-increment Pre-increment --*B7;--*B7; Pre-decrement Pre-decrement

Page 39: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

39TMS320C6000 Assembly Language and its Rules

Operations on Pointers 3 of 4Operations on Pointers 3 of 4

The pointer increment and decrement The pointer increment and decrement operators can be used with load and store operators can be used with load and store instructionsinstructions..

Suppose we want to copy data from one Suppose we want to copy data from one place to another. In C we might write:place to another. In C we might write:

for ( i = 0 ; i < 10 ; i++)for ( i = 0 ; i < 10 ; i++) { *ptr2++ = *ptr1++; }{ *ptr2++ = *ptr1++; }

Page 40: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

40TMS320C6000 Assembly Language and its Rules

Operations on Pointers 4 of 4Operations on Pointers 4 of 4

In assembly language, the part:In assembly language, the part: *ptr2++ = *ptr1++;*ptr2++ = *ptr1++;

Could be written as:Could be written as:

LDW *A4++, A0;LDW *A4++, A0;

NOP 4;NOP 4;

STW A0, *A5++;STW A0, *A5++;

Page 41: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

41TMS320C6000 Assembly Language and its Rules

Topic FiveTopic Five

Multiplications and DivisionMultiplications and Division

Page 42: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

42TMS320C6000 Assembly Language and its Rules

Multiplications 1 of 5Multiplications 1 of 5

Multiplication is widely used in DSP for Multiplication is widely used in DSP for Finite Impulse Response (FIR) filters and Finite Impulse Response (FIR) filters and correlation.correlation.

Page 43: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

43TMS320C6000 Assembly Language and its Rules

Multiplications 2 of 5Multiplications 2 of 5

Multiply instructions use registers:Multiply instructions use registers:

MPY A1, A2, A3;MPY A1, A2, A3;

Multiply the 16-bit value in register A1 by Multiply the 16-bit value in register A1 by the 16-bit value in register A2 and put the the 16-bit value in register A2 and put the 32-bit product in register A3. 32-bit product in register A3.

In other words, A3 = A1 x A2In other words, A3 = A1 x A2

Page 44: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

44TMS320C6000 Assembly Language and its Rules

Multiplications 3 of 5Multiplications 3 of 5

Multiplication instructions can only use Multiplication instructions can only use registers. They cannot use pointer registers. They cannot use pointer operations:operations:

MPY A3, A4, A5 MPY A3, A4, A5 MPY *A3, A4, A5 XMPY *A3, A4, A5 X MPY A3, *A4++, A5 XMPY A3, *A4++, A5 X

Page 45: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

45TMS320C6000 Assembly Language and its Rules

Multiplications 4 of 5Multiplications 4 of 5

The The MPYMPY instruction has one delay slot. This instruction has one delay slot. This means that the product is not available means that the product is not available until 2 cycles after the until 2 cycles after the MPYMPY instruction. instruction.

MPY A3, A4, A5;MPY A3, A4, A5; NOP ; Wait 1 cycleNOP ; Wait 1 cycle STW A5, *A4 ; Store productSTW A5, *A4 ; Store product

It may be necessary to follow the It may be necessary to follow the MPYMPY instruction with a instruction with a NOPNOP..

Page 46: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

46TMS320C6000 Assembly Language and its Rules

Multiplications 5 of 5Multiplications 5 of 5

For multiplications by powers of 2, for For multiplications by powers of 2, for example 2, 4, 8, 16, 32 etc, use the example 2, 4, 8, 16, 32 etc, use the instruction instruction SHLSHL (Shift Left). (Shift Left).

SHL A3, 1, A3; Multiply by 2SHL A3, 1, A3; Multiply by 2

SHL A4, 2, A4; Multiply by 4SHL A4, 2, A4; Multiply by 4

SHL B5, 3, B5; Multiply by 8SHL B5, 3, B5; Multiply by 8

SHL B7, 8, B7; Multiply by 256SHL B7, 8, B7; Multiply by 256 This is a single-cycle instruction.This is a single-cycle instruction.

Page 47: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

47TMS320C6000 Assembly Language and its Rules

DivisionDivision

To divide by powers of 2, for example 2, 4, To divide by powers of 2, for example 2, 4, 8, 16, 32 etc, use the instruction 8, 16, 32 etc, use the instruction SHRSHR (Shift Right).(Shift Right).

SHR B3, 1, B3; Divide by 2SHR B3, 1, B3; Divide by 2

SHR A4, 2, A5; Divide by 4SHR A4, 2, A5; Divide by 4

SHR B5, 3, A3; Divide by 8SHR B5, 3, A3; Divide by 8

SHR B7, 8, B7; Divide by 256SHR B7, 8, B7; Divide by 256 This is a single-cycle instruction.This is a single-cycle instruction.

Page 48: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

48TMS320C6000 Assembly Language and its Rules

Topic SixTopic Six

Introducing Delay SlotsIntroducing Delay Slots

Page 49: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

49TMS320C6000 Assembly Language and its Rules

Delay Slots 1 of 3Delay Slots 1 of 3

So far we have ignored the time it takes So far we have ignored the time it takes the processor to implement an the processor to implement an instruction.instruction.

In fact, the instruction In fact, the instruction BB takes 6 cycles takes 6 cycles before the branch actually occurs.before the branch actually occurs.

Rather than just waiting 6 cycles, the Rather than just waiting 6 cycles, the TMS320C6000 allows another 5 other TMS320C6000 allows another 5 other instructions to be executed. These are instructions to be executed. These are called called delay slotsdelay slots..

Page 50: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

50TMS320C6000 Assembly Language and its Rules

Delay Slots 2 of 3Delay Slots 2 of 3

For correct operation of the processor we For correct operation of the processor we need to put 5 need to put 5 NOPNOPs (or other instructions) s (or other instructions) after the after the BB instruction. instruction.

loop: B loop; 1 cycleloop: B loop; 1 cycle NOP; 1st delay slotNOP; 1st delay slot NOP; 2nd delay slotNOP; 2nd delay slot

NOP; 3rd delay slotNOP; 3rd delay slot NOP; 4th delay slotNOP; 4th delay slot NOP; 5th delay slotNOP; 5th delay slot NOP; B taken here. NOP; B taken here.

Page 51: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

51TMS320C6000 Assembly Language and its Rules

Delay Slots 3 of 3Delay Slots 3 of 3

For correct operation of the next For correct operation of the next instruction, the delay loop we saw earlier instruction, the delay loop we saw earlier should be written as:should be written as:

MVK 10, A1 ; A1 = 10MVK 10, A1 ; A1 = 10

loop: ADDK –1, A1; Decrement A1loop: ADDK –1, A1; Decrement A1

[A1] B loop; Branch to loop[A1] B loop; Branch to loop

NOP 5 ; 5 delay slotsNOP 5 ; 5 delay slots

Page 52: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

52TMS320C6000 Assembly Language and its Rules

Topic SevenTopic Seven

Writing an assembly language Writing an assembly language function callable from C code.function callable from C code.

Page 53: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

53TMS320C6000 Assembly Language and its Rules

C Callable Function 1 of 6C Callable Function 1 of 6

Suppose we want to write the Suppose we want to write the following assembly language function following assembly language function that adds together two numbers:that adds together two numbers:

int sum ( int x, int y)int sum ( int x, int y) {return (x + y);}{return (x + y);}

To use the function in C we might To use the function in C we might write:write:

int result;int result;result = function (100, 200);result = function (100, 200);

Page 54: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

54TMS320C6000 Assembly Language and its Rules

C Callable Function 2 of 6C Callable Function 2 of 6

The C compiler implements the function The C compiler implements the function as follows:as follows:

Parameter Parameter xx is passed in A4 is passed in A4 Parameter Parameter yy is passed in B4 is passed in B4 The return value is in A4:The return value is in A4:

The C function can be thought of as:The C function can be thought of as:

A4 sum ( A4, B4);A4 sum ( A4, B4);

Page 55: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

55TMS320C6000 Assembly Language and its Rules

C Callable Function 3 of 6C Callable Function 3 of 6

In assembly language we write:In assembly language we write:

.global _sum;.global _sum;

.sect “.text”;.sect “.text”;

.align 4;.align 4;

_sum:_sum: ADD A4, B4, A4;ADD A4, B4, A4;

B B3 ;B B3 ;

NOP 5 ;NOP 5 ;

Page 56: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

56TMS320C6000 Assembly Language and its Rules

C Callable Function 4 of 6C Callable Function 4 of 6

The The .global.global assembler directive makes assembler directive makes the label the label _sum _sum available outside this available outside this module.module.

.global _sum;.global _sum;

Notice the underscore at the beginning of Notice the underscore at the beginning of _sum_sum. This is a C compiler convention.. This is a C compiler convention.

Page 57: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

57TMS320C6000 Assembly Language and its Rules

C Callable Function 5 of 6C Callable Function 5 of 6

The line The line .sect “.text” .sect “.text” puts the code in puts the code in the code segment.the code segment.

The assembler directive .The assembler directive .align 4align 4 aligns aligns the code on a 32-bit boundary.the code on a 32-bit boundary.

The instructionThe instruction ADD A4, B4, A4 ADD A4, B4, A4 adds adds the value inthe value in A4 A4 to the value into the value in B4 B4 and and puts the result inputs the result in A4. A4.

Page 58: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

58TMS320C6000 Assembly Language and its Rules

C Callable Function 6 of 6C Callable Function 6 of 6

Finally, return from the function:Finally, return from the function: B B3 ;B B3 ;

NOP 5 ;NOP 5 ;

Just before the function Just before the function sum()sum() is called, the is called, the compiler puts the return address in compiler puts the return address in register B3. register B3.

Important: Do not change the register B3 Important: Do not change the register B3 inside the function. The return address will inside the function. The return address will be lost and the program may well crash!be lost and the program may well crash!

Page 59: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

59TMS320C6000 Assembly Language and its Rules

Allocating Local Variables 1 of 5Allocating Local Variables 1 of 5

In C, variables are sometimes used only in a In C, variables are sometimes used only in a particular function. These are local particular function. These are local variables.variables.

int my_function (void)int my_function (void) {{ int x; // A local variable.int x; // A local variable. };};

The variable The variable xx is only available within is only available within my_function()my_function()

Page 60: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

60TMS320C6000 Assembly Language and its Rules

Allocating Local Variables 2 of 5Allocating Local Variables 2 of 5

To allocate temporary storage for a 32-bit To allocate temporary storage for a 32-bit variable (4 bytes), subtract 4+4 from the variable (4 bytes), subtract 4+4 from the Stack Pointer (SP) at the beginning of the Stack Pointer (SP) at the beginning of the function.function.

.asg SP, B15 ; Make B15 the SP.asg SP, B15 ; Make B15 the SP

function:function:

SUB SP -8, SPSUB SP -8, SP

Page 61: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

61TMS320C6000 Assembly Language and its Rules

Allocating Local Variables 3 of 5Allocating Local Variables 3 of 5

To write to a local variable use:To write to a local variable use:

STW A4,*+SP(4)STW A4,*+SP(4)

This stores the contents of register A4 at This stores the contents of register A4 at the data memory location with a positive the data memory location with a positive offset of 4 bytes from SP.offset of 4 bytes from SP.

This can be used as a “This can be used as a “pushpush” instruction.” instruction.

Page 62: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

62TMS320C6000 Assembly Language and its Rules

Allocating Local Variables 4 of 5Allocating Local Variables 4 of 5

To read a local variable use:To read a local variable use:

LDW *+SP(4), A2LDW *+SP(4), A2

Here Here *+SP(4)*+SP(4) means the contents of the means the contents of the memory location at a positive offset of 4 memory location at a positive offset of 4 bytes from SP.bytes from SP.

This can be used as a “This can be used as a “poppop” instruction.” instruction.

Page 63: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

63TMS320C6000 Assembly Language and its Rules

Allocating Local Variables 5 of 5Allocating Local Variables 5 of 5

At the end of the function, add the same At the end of the function, add the same number of bytes to the SP to restore it to number of bytes to the SP to restore it to its original value.its original value.

ADDK 8, SPADDK 8, SP

B B3 ; ReturnB B3 ; Return

Page 64: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

64TMS320C6000 Assembly Language and its Rules

Topic EightTopic Eight

Parallel Operations.Parallel Operations.

Page 65: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

65TMS320C6000 Assembly Language and its Rules

Parallel Operations 1 of 4Parallel Operations 1 of 4

We have already seen how to write We have already seen how to write constants to a registers.constants to a registers.

MVK 1234h, A4; 1 cycleMVK 1234h, A4; 1 cycle MVK 5678h, B4; 1 cycleMVK 5678h, B4; 1 cycle

In this case we write the value 1234h to In this case we write the value 1234h to register A4 then write 5678h to register B4. register A4 then write 5678h to register B4.

This takes 2 cycles.This takes 2 cycles.

Page 66: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

66TMS320C6000 Assembly Language and its Rules

Parallel Operations 2 of 4Parallel Operations 2 of 4

We can write the same instructions using We can write the same instructions using the || operator:the || operator:

MVK 1234h, A4; MVK 1234h, A4; || MVK 5678h, B4; || MVK 5678h, B4;

In this case we write the value 1234h to In this case we write the value 1234h to register A4, at the same time as we write register A4, at the same time as we write 5678h to register B5.5678h to register B5.

This takes 1 cycle.This takes 1 cycle.

Page 67: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

67TMS320C6000 Assembly Language and its Rules

Parallel Operations 3 of 4Parallel Operations 3 of 4

We need one register from A0 to A15 and We need one register from A0 to A15 and the other from one of B0 to B15.the other from one of B0 to B15.

MVK 1234h, A4; MVK 1234h, A4;

|| MVK 5678h, A5; X|| MVK 5678h, A5; X

We cannot perform parallel operations on We cannot perform parallel operations on two registers from the same register two registers from the same register bank. bank.

Page 68: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

68TMS320C6000 Assembly Language and its Rules

Parallel Operations 4 of 4Parallel Operations 4 of 4

Parallel operations are very useful for Parallel operations are very useful for stereo audio processing.stereo audio processing.

We can process both the left channel and We can process both the left channel and the right channel at exactly the same the right channel at exactly the same time.time.

This means it can be as fast to process This means it can be as fast to process two channels as it is to process one.two channels as it is to process one.

This cannot be done in C. This cannot be done in C.

Page 69: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

69TMS320C6000 Assembly Language and its Rules

Topic NineTopic Nine

Using Circular Addressing for Using Circular Addressing for Circular Buffers.Circular Buffers.

Page 70: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

70TMS320C6000 Assembly Language and its Rules

Circular Buffers 1 of 7Circular Buffers 1 of 7

We can implement a circular buffer in C code We can implement a circular buffer in C code as follows:as follows:

int buffer[16]; int buffer[16]; static int * ptr; // Pointerstatic int * ptr; // Pointer

ptr = &buffer[0]; // Initialise ptr = &buffer[0]; // Initialise

if ( ptr < &buffer[15] )if ( ptr < &buffer[15] ) ptr++; // Incrementptr++; // Increment elseelse ptr = &buffer[0]; // Back to startptr = &buffer[0]; // Back to start

Page 71: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

71TMS320C6000 Assembly Language and its Rules

Circular Buffers 2 of 7Circular Buffers 2 of 7

The TMS320C6000 can support circular The TMS320C6000 can support circular buffers of size 8, 16, 32, 64, 128 etc bytes.buffers of size 8, 16, 32, 64, 128 etc bytes.

To set up a particular register for use as To set up a particular register for use as circular buffer, we must configure the circular buffer, we must configure the AMRAMR (Address Mode Register).(Address Mode Register).

At power up, the At power up, the AMRAMR contains 00000000h. contains 00000000h.

Page 72: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

72TMS320C6000 Assembly Language and its Rules

Circular Buffers 3 of 7Circular Buffers 3 of 7

To read the To read the AMRAMR register we must use the register we must use the special instruction special instruction MVCMVC (move control (move control register).register).

MVC AMR, B1; MVC AMR, B1; Copy AMR to B1Copy AMR to B1

MVC AMR, A1; X Must be B-sideMVC AMR, A1; X Must be B-side

To write to the To write to the AMRAMR register we again use register we again use the special instruction the special instruction MVCMVC (move control (move control register).register).

MVC A1, AMR; Copy A1 to AMRMVC A1, AMR; Copy A1 to AMR

Page 73: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

73TMS320C6000 Assembly Language and its Rules

Circular Buffers 4 of 7Circular Buffers 4 of 7

For example, to set up A7 for use in For example, to set up A7 for use in circular addressing, we write the value circular addressing, we write the value 00050040h to the 00050040h to the AMRAMR (Address Mode (Address Mode Register).Register).

MVKL 00050040h, A2MVKL 00050040h, A2

MVKH 00050040h, A2MVKH 00050040h, A2

MVC A2, AMR ; Update AMRMVC A2, AMR ; Update AMR

Page 74: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

74TMS320C6000 Assembly Language and its Rules

Circular Buffers 5 of 7Circular Buffers 5 of 7

The buffer has a size 16 * 4 = 64 bytes.The buffer has a size 16 * 4 = 64 bytes.

int buffer[16];int buffer[16];

For circular addressing, the buffer must For circular addressing, the buffer must be aligned on a boundary equal to the size be aligned on a boundary equal to the size of the buffer.of the buffer.

buffer: .usect ".far", 64, 64buffer: .usect ".far", 64, 64

Page 75: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

75TMS320C6000 Assembly Language and its Rules

Circular Buffers 6 of 7Circular Buffers 6 of 7

To store the value of the To store the value of the ptrptr we can we can write:write:

ptr: .usect “.far”, 4, 4ptr: .usect “.far”, 4, 4

At the beginning of program, set At the beginning of program, set ptrptr to to contain the starting address of the buffer.contain the starting address of the buffer.

Page 76: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

76TMS320C6000 Assembly Language and its Rules

Circular Buffers 7 of 7Circular Buffers 7 of 7

To read a value from the buffer with To read a value from the buffer with increment of A7:increment of A7:

LDW *A7++,A3; Read from bufferLDW *A7++,A3; Read from buffer

To write a value from a register back to To write a value from a register back to the buffer :the buffer :

STW A3, *A7; Write to bufferSTW A3, *A7; Write to buffer

Page 77: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

77TMS320C6000 Assembly Language and its Rules

Topic TenTopic Ten

40-bit Operations.40-bit Operations.

Page 78: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

78TMS320C6000 Assembly Language and its Rules

40-bit Operations 1 of 840-bit Operations 1 of 8

So far we have used registers A0 to A15 So far we have used registers A0 to A15 and B0 to B15 for 32-bit operations. and B0 to B15 for 32-bit operations.

The TMS320C6000 also supports 40-bit The TMS320C6000 also supports 40-bit maths.maths.

Let us look at a simple addition.Let us look at a simple addition.

ADD A2, A1:A0, A3:A2ADD A2, A1:A0, A3:A2

Page 79: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

79TMS320C6000 Assembly Language and its Rules

40-bit Operations 2 of 840-bit Operations 2 of 8

Here Here A1:A0A1:A0 is a 40-bit register. is a 40-bit register. 8 bits are provided by A18 bits are provided by A1 32 bits are provided by A032 bits are provided by A0

ADD A2, A1:A0, A3:A2ADD A2, A1:A0, A3:A2

This means: add the 40-bit value register This means: add the 40-bit value register pair A1:A0 to 32-bit register A2, then put pair A1:A0 to 32-bit register A2, then put the 40-bit result in register pair A3:A2.the 40-bit result in register pair A3:A2.

Page 80: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

80TMS320C6000 Assembly Language and its Rules

40-bit Operations 3 of 840-bit Operations 3 of 8

40-bit operations are particularly useful 40-bit operations are particularly useful for FIR filters, which use a large number for FIR filters, which use a large number of multiplies and additions.of multiplies and additions.

Suppose we are implementing a 64-Suppose we are implementing a 64-element FIR filter using 32-bit maths. To element FIR filter using 32-bit maths. To prevent overflow, we have to divide each prevent overflow, we have to divide each multiplication by 32 before adding. This multiplication by 32 before adding. This can mean a loss of accuracy. can mean a loss of accuracy.

Page 81: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

81TMS320C6000 Assembly Language and its Rules

40-bit Operations 4 of 840-bit Operations 4 of 8

In C code, the divide by 32 is In C code, the divide by 32 is implemented as a shift right 5 places:implemented as a shift right 5 places:

int temp = 0; // 32-bit variableint temp = 0; // 32-bit variablefor ( i = 0 ; i < 64 ; i++)for ( i = 0 ; i < 64 ; i++) {{ temp = input[i]*coeff[i];temp = input[i]*coeff[i]; result += (temp >> 5);result += (temp >> 5); }}result >>= 10;result >>= 10;

Page 82: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

82TMS320C6000 Assembly Language and its Rules

40-bit Operations 5 of 840-bit Operations 5 of 8

Using 40-bit maths, there is no need to Using 40-bit maths, there is no need to perform a division before each addition:perform a division before each addition:

long temp = 0; // 40 bitslong temp = 0; // 40 bits for ( i = 0 ; i < 64 ; i++)for ( i = 0 ; i < 64 ; i++) {{ temp = input[i]*coeff[i];temp = input[i]*coeff[i]; }} temp >>= 15; temp >>= 15;

Page 83: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

83TMS320C6000 Assembly Language and its Rules

40-bit Operations 6 of 840-bit Operations 6 of 8

The FIR implementation becomes:The FIR implementation becomes:

MVK 64, B1MVK 64, B1

loop: LDW *B4++, A0 ; B4 -> coeffs loop: LDW *B4++, A0 ; B4 -> coeffs LDW *B5++, A1 ; B5 -> inputs LDW *B5++, A1 ; B5 -> inputs

NOP 4NOP 4MPY A0, A1, A2 ; MultiplyMPY A0, A1, A2 ; Multiply

ADDK –1, B1ADDK –1, B1 ADD A2, A3:A2,A3:A2 ; AccumulateADD A2, A3:A2,A3:A2 ; Accumulate [B1] B loop[B1] B loop NOP 5NOP 5 SHR A3:A2,15,A3:A2 ; Divide onceSHR A3:A2,15,A3:A2 ; Divide once

Page 84: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

84TMS320C6000 Assembly Language and its Rules

40-bit Operations 7 of 840-bit Operations 7 of 8

When performing 40-bit operations, When performing 40-bit operations, the instruction the instruction ADDKADDK cannot be used. cannot be used. The instruction The instruction ADDADD must be used must be used instead:instead:

ADDK 1, A1:A0 ADDK 1, A1:A0 XX ADD 1, A1:A0, A1:A0 ADD 1, A1:A0, A1:A0

Similarly, the instruction Similarly, the instruction SUBSUB must be must be used to subtract from a 40-bit used to subtract from a 40-bit register:register:

ADDK -1, A5:A4ADDK -1, A5:A4 XX SUB 1, A5:A4, A5:A4 SUB 1, A5:A4, A5:A4

Page 85: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

85TMS320C6000 Assembly Language and its Rules

40-bit Operations 7 of 840-bit Operations 7 of 8

When converting a 40-bit value to 32-When converting a 40-bit value to 32-bits, it is wise to use the bits, it is wise to use the SATSAT (saturate) (saturate) instruction to prevent the sign instruction to prevent the sign changing: changing:

SAT A1:A0, A4SAT A1:A0, A4

Suppose Suppose A1:A0A1:A0 contains contains 00 FFFF FFFFh. This is a positive 00 FFFF FFFFh. This is a positive number. number.

However, the 32-bit value in However, the 32-bit value in A0A0 is FFFF is FFFF FFFFh. This is a FFFFh. This is a negativenegative number. number.

Page 86: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

86TMS320C6000 Assembly Language and its Rules

Topic ElevenTopic Eleven

Optimising assembly code for speed.Optimising assembly code for speed.

Page 87: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

87TMS320C6000 Assembly Language and its Rules

Optimising Code 1 of 5Optimising Code 1 of 5

Let us start with a very simple C function Let us start with a very simple C function that copies one block of data to another:that copies one block of data to another:

void copy(int* p1, int* p2, int size)void copy(int* p1, int* p2, int size) { { while (size--)while (size--) { { *p2++ = *p1++;*p2++ = *p1++; }} } }

Page 88: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

88TMS320C6000 Assembly Language and its Rules

Optimising Code 2 of 5Optimising Code 2 of 5

In assembly language we could write:In assembly language we could write: loop: LDW *A4++, A0loop: LDW *A4++, A0 NOP 4NOP 4 STW A0, *B4++STW A0, *B4++ ADDK –1, A1ADDK –1, A1 [B1] B loop[B1] B loop NOP 5NOP 5 B B3B B3 NOP 5NOP 5

This takes 144 cycles to execute.This takes 144 cycles to execute.

Page 89: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

89TMS320C6000 Assembly Language and its Rules

Optimising Code 3 of 5Optimising Code 3 of 5

We can move the We can move the ADDKADDK and and BB loop instructions loop instructions upwards:upwards:

loop: LDW *A4++, A0loop: LDW *A4++, A0 ADDK –1, A1ADDK –1, A1 [B1] B loop[B1] B loop NOP 2 ; Lose 2 NOPs hereNOP 2 ; Lose 2 NOPs here STW A0, *B4++STW A0, *B4++ NOP 2 ; Lose 2 NOPs hereNOP 2 ; Lose 2 NOPs here B B3B B3 NOP 5NOP 5

Page 90: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

90TMS320C6000 Assembly Language and its Rules

Optimising Code 4 of 5Optimising Code 4 of 5

We can also move the We can also move the B B3B B3 instruction upwards:instruction upwards:

loop: LDW *A4++, A0loop: LDW *A4++, A0 ADDK –1, A1ADDK –1, A1 [A1] B copy[A1] B copy || [!A1]B B3 ; Add test|| [!A1]B B3 ; Add test NOP 1NOP 1 STW A0, *B4++STW A0, *B4++ NOP 3NOP 3

This takes 93 cycles to execute.This takes 93 cycles to execute.

Page 91: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

91TMS320C6000 Assembly Language and its Rules

Optimising Code 5 of 5Optimising Code 5 of 5

The optimised version runs 35% faster The optimised version runs 35% faster than the un-optimised version.than the un-optimised version.

The only downside is that the code is The only downside is that the code is harder to read and debug.harder to read and debug.

It is therefore recommended that the code It is therefore recommended that the code is written and tested, then optimised and is written and tested, then optimised and re-tested again.re-tested again.

Page 92: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

92TMS320C6000 Assembly Language and its Rules

Topic TwelveTopic Twelve

A typical application of assembly A typical application of assembly language.language.

Page 93: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

93TMS320C6000 Assembly Language and its Rules

Implementing a Stereo FIR Implementing a Stereo FIR Filter 1 of 6Filter 1 of 6

We will design a stereo Finite Impulse We will design a stereo Finite Impulse Response (FIR) filter that processes both Response (FIR) filter that processes both the left and right hand audio channels at the left and right hand audio channels at exactly the same time. It will use 64 exactly the same time. It will use 64 coefficients.coefficients.

We will bring together several techniques We will bring together several techniques explained earlier. explained earlier.

Compare performance with C code Compare performance with C code version.version.

Page 94: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

94TMS320C6000 Assembly Language and its Rules

Implementing a Stereo FIR Implementing a Stereo FIR Filter 2 of 6Filter 2 of 6

We will start with a C callable We will start with a C callable function.function.int stereo_FIR (const int *, int stereo_FIR (const int *,

int x, int x, int y )int y ) { };{ };

const int *const int * points to the filter points to the filter coefficients.coefficients.

Here Here xx and and yy are the inputs. are the inputs.

Page 95: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

95TMS320C6000 Assembly Language and its Rules

Implementing a Stereo FIR Implementing a Stereo FIR Filter 3 of 6Filter 3 of 6

The C function has register usage as The C function has register usage as follows:follows:

A4 stereo_FIR ( A4, B4, A6 )A4 stereo_FIR ( A4, B4, A6 )

A4 points to the coefficients.A4 points to the coefficients. B4 contains B4 contains xx. A6 contains . A6 contains yy.. Note that there can only be one return Note that there can only be one return

value, so A4 will contain both outputs. value, so A4 will contain both outputs.

Page 96: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

96TMS320C6000 Assembly Language and its Rules

Implementing a Stereo FIR Implementing a Stereo FIR Filter 4 of 6Filter 4 of 6

We can start with the same code we used for We can start with the same code we used for 40-bit operations, but modified for buffer1.40-bit operations, but modified for buffer1.

MVK 64, B1MVK 64, B1

loop: LDW *A4++, A0 ; B4 -> coeffs loop: LDW *A4++, A0 ; B4 -> coeffs

LDW *A5++, A1 ; A5 -> buffer1 LDW *A5++, A1 ; A5 -> buffer1

NOP 4NOP 4

MPY A0, A1, A2 ; MultiplyMPY A0, A1, A2 ; Multiply

ADDK –1, B1ADDK –1, B1

ADD A2, A7:A6, A7:A6; AccumulateADD A2, A7:A6, A7:A6; Accumulate

[B1] B loop[B1] B loop

Page 97: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

97TMS320C6000 Assembly Language and its Rules

Implementing a Stereo FIR Implementing a Stereo FIR Filter 5 of 6Filter 5 of 6

Now we add parallel operations for the Now we add parallel operations for the second channel using B instead of A:second channel using B instead of A:

loop: LDW *A4++, A0 ; B4 -> coeffs loop: LDW *A4++, A0 ; B4 -> coeffs LDW *A5++, A1 ; A5 -> buffer1LDW *A5++, A1 ; A5 -> buffer1 || LDW *B5++, B1 ; B5 -> buffer2 || LDW *B5++, B1 ; B5 -> buffer2

NOP 4NOP 4MPY A0, A1, A2 ; MultiplyMPY A0, A1, A2 ; Multiply

|| MPY B0, B1, B2 ; || MPY B0, B1, B2 ; ADDK –1, B1ADDK –1, B1 ADD A2,A7:A6,A7:A6 ; AccumulateADD A2,A7:A6,A7:A6 ; Accumulate || ADD B2,B7:B6,B7:B6 ; || ADD B2,B7:B6,B7:B6 ; [B1] B loop[B1] B loop

Page 98: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

98TMS320C6000 Assembly Language and its Rules

Implementing a Stereo FIR Implementing a Stereo FIR Filter 6 of 6Filter 6 of 6

The operations on the B registers is done The operations on the B registers is done exactly at the same time as those on the A exactly at the same time as those on the A registers.registers.

The full assembly code for the stereo FIR The full assembly code for the stereo FIR filter is given in the files filter is given in the files FIR_filters_asm.asmFIR_filters_asm.asm and and FIR_filters_asm.hFIR_filters_asm.h

Page 99: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

99TMS320C6000 Assembly Language and its Rules

Topic TwelveTopic Twelve

Some other information.Some other information.

Page 100: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

100TMS320C6000 Assembly Language and its Rules

Other Instructions Other Instructions

Besides the instructions given here, the Besides the instructions given here, the C67xx and C64xx have additional C67xx and C64xx have additional assembly language instructions.assembly language instructions.

The C67xx has floating point instructions.The C67xx has floating point instructions. The C64xx has more registers and The C64xx has more registers and

supports 32-bit maths.supports 32-bit maths. See the References section for details.See the References section for details.

Page 101: 180909 ajay patil 1 TMS320C6000 Assembly Language and its Rules Assignment One of the simplest operations in C is to assign a constant to a variable: One

180909 ajay patil

101TMS320C6000 Assembly Language and its Rules

References References

TMS320C6000 CPU and Instruction Set TMS320C6000 CPU and Instruction Set Reference Guide SPRU189.Reference Guide SPRU189.

TMS320C6000 Assembly Language Tools TMS320C6000 Assembly Language Tools User's Guide SPRU186.User's Guide SPRU186.