# Assignment

## Assignment

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

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

2. Load a Constant 1 of 4 • We will use register A1 to hold variable x: • In assembly language we write: MVK 10, A1; • The instruction MVK moves (copies) the constant 10 into register A1 • Register A1 now contains 00000000Ah TMS320C6000 Assembly Language and its Rules

3. Load a Constant2 of 4 • The correct syntax is number then register. The number can also be in hexadecimal: MVK 10, A1;  MVK 0xA, A1;  MVK 0Ah, A1;  • Do not use # MVK #10, A1; X TMS320C6000 Assembly Language and its Rules

4. Load a Constant3 of 4 • To load full 32 bits of a register needs 2 instructions: MVK 0x5678, B2; MVKLH 0x1234, B2; • Register B2 now contains 12345678h TMS320C6000 Assembly Language and its Rules

5. Load a Constant4 of 4 • To load the full 32 bits of a register with 0 (zero) requires only a single instruction: ZERO B2; • Register B2 now contains 00000000h TMS320C6000 Assembly Language and its Rules

6. Incrementing a Register 1 of 2 • To increment a variable in C we can write: int x; x++; • This adds 1 to the value of the variable x TMS320C6000 Assembly Language and its Rules

7. Incrementing a Register 1 of 2 • In assembly language we use the instruction ADDK (add constant) ADDK 1, A2 ; • This adds the constant 1 to the contents of register A2 TMS320C6000 Assembly Language and its Rules

8. Decrementing a Register 1 of 2 • To decrement a variable in C we can write: int x; x--; • This subtracts 1 from the variable x. TMS320C6000 Assembly Language and its Rules

9. Decrementing a Register 2 of 2 • In assembly language we again use the instruction ADDK (add constant) ADDK -1, A2; • This adds the constant -1 to the contents of register A2 • There is no such instruction as SUBK TMS320C6000 Assembly Language and its Rules

10. No Operation • The TMS320C6000 provides an instruction that does nothing except take time. This is called NOP (No operation) NOP ; • If we want to execute 4 NOP instructions one after another we can write: NOP 4; • This instruction can be used to generate time delays. TMS320C6000 Assembly Language and its Rules

11. Topic Two • Controlling program flow TMS320C6000 Assembly Language and its Rules

12. Testing Conditions 1 of 5 • The if-else construct is widely used in C. • Consider the following simple piece of code: int x, y ; if ( x != 0 ) { y++; } • This means if x is not equal to zero, then increment variable y. TMS320C6000 Assembly Language and its Rules

13. Testing Conditions 2 of 5 • The assembler provides a neat way to do this. Assuming x is stored in register A1 and y is stored in register A2: [A1] ADDK 1, A2; • The term in [ ] is the condition to be tested. If the condition is A1 is not equal to zero is true, add 1 to the value in A2. Otherwise do nothing. TMS320C6000 Assembly Language and its Rules

14. Testing Conditions 3 of 5 • Consider another piece of C code: int x, y ; if ( x == 0 ) { y--; } • This means if x is equal to zero, decrement y. TMS320C6000 Assembly Language and its Rules

15. Testing Conditions 4 of 5 • Again the assembler provides a neat way to do this. Assuming x is stored in register A1 and y is stored in A2: [!A1] ADDK -1, A2; • The term in [ ] is the condition to be tested. If the condition is A1 is equal to zero is true, add -1 to the value in A2. Otherwise do nothing. TMS320C6000 Assembly Language and its Rules

16. Testing Conditions 5 of 5 • The test can use register A1, A2, B0, B1 or B2: [A1] MVK 10, A2;  [A0] MVK 10, A2; X [A3] MVK 10, A2; X [!B0] MVK 10, A2;  [B1] MVK 10, A2;  [B3] MVK 10, A2; X TMS320C6000 Assembly Language and its Rules

17. Branch Instructions 1 of 3 • Program execution can forced to a different place using the B (branch) instruction: label: B label; • When the B (branch) is reached, the next instruction to be executed will be at the address label. It is similar to the goto instruction in C. TMS320C6000 Assembly Language and its Rules

18. Branch Instructions 2 of 3 • Rather than using a label with the instruction B, a register can be used. MVKH label, B3; MVKL label, B3; B B3; This method is used by the C compiler, usually with B3, to return from a function. TMS320C6000 Assembly Language and its Rules

19. Branch Instructions 3 of 3 • The instruction B can also be combined with a test for a condition. label: ADDK 1, A3; [A1] B label; When the B (branch) is reached, the next instruction to be executed will be at the address label, but only if A1 is non-zero. TMS320C6000 Assembly Language and its Rules

20. Implementing a Delay Loop 1 of 2 • In C, a delay loop can be implemented using the do-while construct: int i = 10; do { i--; } while (i != 0); • We start with i = 10. Every time through the loop i is decremented. When i == 0 then the loop terminates. TMS320C6000 Assembly Language and its Rules

21. Implementing a Delay Loop 2 of 2 • In assembly language, we can use A1 to hold i. This can be decremented and tested: MVK 10, A1 ; A1 = 10 loop: ADDK –1, A1; Decrement A1 [A1] B loop; Branch to loop We start with A1 = 10. Every time through the loop A1 is decremented. When A1 == 0 then the loop terminates. TMS320C6000 Assembly Language and its Rules

22. Topic Three • Allocating storage for variables and constants. TMS320C6000 Assembly Language and its Rules

23. To Declare a Variable 1 of 2 • In C code, a 32-bit variable can be declared as follows: int x; • In assembly language use: x: .usect ".far",4, 4 TMS320C6000 Assembly Language and its Rules

24. To Declare a Variable 2 of 2 • This means: • x: A label. Where to find variable • .usect In un-initialised data • ".far", Large memory model • 4, How many bytes • 4; Align on 4-byte boundary TMS320C6000 Assembly Language and its Rules

25. To Declare a Buffer 1 of 2 • In C code, a 32-element buffer can be declared as an array: int buffer[32]; • In assembly language use: buffer: .usect ".far",128, 4 TMS320C6000 Assembly Language and its Rules

26. To Declare a Buffer 2 of 2 • This means: • buffer: A label. Where to find data • .usect In un-initialised data • ".far", Large memory model • 128, How many bytes • 4; Align on 4-byte boundary TMS320C6000 Assembly Language and its Rules

27. To Declare Constants 1 of 3 • In C code, an array of constants can be declared as: const int constants[5] = {1,2,3,4,5}; • This is an array of 5 read-only constants of value 1, 2, 3, 4 and 5. TMS320C6000 Assembly Language and its Rules

28. To Declare Constants 2 of 3 • In assembly language use: .sect ".const" .align 4 coefficients: .field 1, 32 ; .field 2, 32 ; .filed 3, 32 ; .field 4, 32 ; .field 5, 32 ; TMS320C6000 Assembly Language and its Rules

29. To Declare Constants 3 of 3 • Here .sect “.const” tells the linker where in memory to store the values. • .align 4 means align on a 4-byte boundary. • The constants are found at the address coefficients. • Each constant is declared as a field of a given value, and size 32 bits. • .field 3, 32 TMS320C6000 Assembly Language and its Rules

30. Topic Four • Using pointers. TMS320C6000 Assembly Language and its Rules

31. Pointing to a Buffer 1 of 3 • To set up a pointer to a buffer in C we write: int buffer[32]; int *ptr; ptr = &buffer[0]; • The pointer ptr is given the address of the start of the buffer. TMS320C6000 Assembly Language and its Rules

32. Pointing to a Buffer 2 of 3 • When using TMS320C6000 assembly language, it is usual practice to use the following registers as pointers: • A4, A5, A6, A7 • B4, B5, B6, B7 • These registers also support circular addressing. TMS320C6000 Assembly Language and its Rules

33. Pointing to a Buffer 2 of 3 • To use register A4 as the pointer to the buffer: • buffer: .usect ".far",128, 4 • MVKL buffer, A4 • MVKH buffer, A4 • First instruction MVKL writes to the low half of register A4 • The second instruction MVKH writes to the high half of register A4 TMS320C6000 Assembly Language and its Rules

34. Moving Data to a Register 1 of 2 • We can load a register with the 32-bit contents of a data memory address. Assume that register A4 points to buffer[0] LDW *A4, A5; • The instruction LDW (load word) copies a word of data from buffer[0] to register A5 • Here W = word = 32 bits TMS320C6000 Assembly Language and its Rules

35. Moving Data to a Register 2 of 2 • The instruction LDW takes 4 cycles to get the data, which makes it slow. Care is needed to wait the required time, for example using 4 NOPs. LDW *A4, A5; NOP 4 ; A5 not ready ADDK 2, A5 ; A5 now ready TMS320C6000 Assembly Language and its Rules

36. Moving Data from a Register • We can store the 32-bit contents of a register at an address in data memory. Assume that register A5 points to buffer[0]: STW A4, *A5; • The instruction STW (store word) copies a word of data from register A4 to buffer[0]. The data are available immediately. • Here W = word = 32 bits TMS320C6000 Assembly Language and its Rules

37. Operations on Pointers 1 of 4 • Several pointer operations are possible in C: • *ptr++; Post-increment • *ptr--; Post-decrement • ++*ptr; Pre-increment • --*ptr; Pre-decrement TMS320C6000 Assembly Language and its Rules

38. Operations on Pointers 2 of 4 • The same pointer operations are available in assembly language: • *A4++; Post-increment • *A5--; Post-decrement • ++*B6; Pre-increment • --*B7; Pre-decrement TMS320C6000 Assembly Language and its Rules

39. Operations on Pointers 3 of 4 • The pointer increment and decrement operators can be used with load and store instructions. • Suppose we want to copy data from one place to another. In C we might write: • for ( i = 0 ; i < 10 ; i++) { *ptr2++ = *ptr1++; } TMS320C6000 Assembly Language and its Rules

40. Operations on Pointers 4 of 4 • In assembly language, the part: *ptr2++ = *ptr1++; • Could be written as: LDW *A4++, A0; NOP 4; STW A0, *A5++; TMS320C6000 Assembly Language and its Rules

41. Topic Five • Multiplications and Division TMS320C6000 Assembly Language and its Rules

42. Multiplications 1 of 5 • Multiplication is widely used in DSP for Finite Impulse Response (FIR) filters and correlation. TMS320C6000 Assembly Language and its Rules

43. Multiplications 2 of 5 • Multiply instructions use registers: • MPY A1, A2, A3; • Multiply the 16-bit value in register A1 by the 16-bit value in register A2 and put the 32-bit product in register A3. • In other words, A3 = A1 x A2 TMS320C6000 Assembly Language and its Rules

44. Multiplications 3 of 5 • Multiplication instructions can only use registers. They cannot use pointer operations: • MPY A3, A4, A5  • MPY *A3, A4, A5 X • MPY A3, *A4++, A5 X TMS320C6000 Assembly Language and its Rules

45. Multiplications 4 of 5 • The MPY instruction has one delay slot. This means that the product is not available until 2 cycles after the MPY instruction. MPY A3, A4, A5; NOP ; Wait 1 cycle STW A5, *A4 ; Store product • It may be necessary to follow the MPY instruction with a NOP. TMS320C6000 Assembly Language and its Rules

46. Multiplications 5 of 5 • For multiplications by powers of 2, for example 2, 4, 8, 16, 32 etc, use the instruction SHL (Shift Left). SHL A3, 1, A3; Multiply by 2 SHL A4, 2, A4; Multiply by 4 SHL B5, 3, B5; Multiply by 8 SHL B7, 8, B7; Multiply by 256 • This is a single-cycle instruction. TMS320C6000 Assembly Language and its Rules

47. Division • To divide by powers of 2, for example 2, 4, 8, 16, 32 etc, use the instruction SHR (Shift Right). SHR B3, 1, B3; Divide by 2 SHR A4, 2, A5; Divide by 4 SHR B5, 3, A3; Divide by 8 SHR B7, 8, B7; Divide by 256 • This is a single-cycle instruction. TMS320C6000 Assembly Language and its Rules

48. Topic Six • Introducing Delay Slots TMS320C6000 Assembly Language and its Rules

49. Delay Slots 1 of 3 • So far we have ignored the time it takes the processor to implement an instruction. • In fact, the instruction B takes 6 cycles before the branch actually occurs. • Rather than just waiting 6 cycles, the TMS320C6000 allows another 5 other instructions to be executed. These are called delay slots. TMS320C6000 Assembly Language and its Rules

50. Delay Slots 2 of 3 • For correct operation of the processor we need to put 5 NOPs (or other instructions) after the B instruction. • loop: B loop; 1 cycle NOP; 1st delay slot NOP; 2nd delay slot NOP; 3rd delay slot NOP; 4th delay slot NOP; 5th delay slot NOP; B taken here. TMS320C6000 Assembly Language and its Rules