170 likes | 369 Views
TMS320C6713 Assembly Language (cont’d). Module 1 Exam (solution). 1. Functional Units a. How many can perform an ADD? Name them. b. Which support memory loads/stores? .M .S .D .L. six; .L1, .L2, .D1, .D2, .S1, .S2. 2. Conditional Code. a. Which registers can be used as cond’l registers?
E N D
Module 1 Exam (solution) 1. Functional Units a. How many can perform an ADD? Name them. b. Which support memory loads/stores? .M .S .D .L six; .L1, .L2, .D1, .D2, .S1, .S2
2. Conditional Code a. Which registers can be used as cond’l registers? b. Which instructions can be conditional? A1, A2, B0, B1, B2 All of them
3. Coding Problems a. Move contents of A0-->A1 MV .L1 A0, A1 or ADD .S1 A0, 0, A1 or MPY .M1 A0, 1, A1 (what’s the problem with this?)
3. Coding Problems a. Move contents of A0-->A1 b. Move contents of CSR-->A1 c. Clear register A5 MV .L1 A0, A1 or ADD .S1 A0, 0, A1 or MPY .M1 A0, 1, A1 (A0 can only be a 16-bit value) MVC CSR, A1 ZERO .S1 A5 or SUB .L1 A5, A5, A5 or MPY .M1 A5, 0, A5 or CLR .S1 A5, 0, 31, A5 or MVK .S1 0, A5 or XOR .L1 A5,A5,A5
3. Coding Problems (cont’d) d. A2 = A02 + A1 e. If (B1 0) then B2 = B5 * B6 f. A2 = A0 * A1 + 10 g. Load an unsigned constant (19ABCh) into register A6. MPY.M1 A0, A0, A2 ADD.L1 A2, A1, A2 [B1] MPY.M2 B5, B6, B2 MPY A0, A1, A2 ADD 10, A2, A2 value .equ 0x00019abc mvkl.s1 value,a6 mvkh.s1 value,a6 mvkl .s1 0x00019abc,a6mvkh .s1 0x00019abc,a6
3. Coding Problems (cont’d) x16 mem A7 10h mem1 h. Load A7 with contents of mem1 and post-increment the selected pointer. load_mem1:MVKL .S1 mem1, A6 MVKH .S1 mem1, A6 LDH .D1 *A6++, A7
Pipeline & NOP Pipeline stages Multiply: One NOP (NOP) Load: four NOPs (NOP 4) Branch: five NOPs (NOP 5)
Pipeline & NOP MVK .S1 40, A2 ; A2 = 40, loop count loop: LDH .D1 *A5++, A0 ; A0 = a(n) LDH .D1 *A6++, A1 ; A1 = x(n) NOP 4 MPY .M1 A0, A1, A3 ; A3 = a(n) * x(n) NOP ADD .L1 A3, A4, A4 ; Y = Y + A3 SUB .L1 A2, 1, A2 ; decrement loop count [A2] B .S1 loop ; if A2 0, branch NOP 5 STH .D1 A4, *A7 ; *A7 = Y
Interface C and Assembly • As a general rule the code written in C is used for initialization and for non-critical (in terms of speed or size) code. • Critical code (in terms of speed/size) can be written in 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 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 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 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 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 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 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
An example of ASM function .global _sum _sum: ZERO .L1 A9 MV .L1 B4,A2 loop: LDH .D1 *A4++, A7 NOP 4 ADD .L1 A7,A9,A9 [A2] SUB .L1 A2,1,A2 [A2] B .S1 loop NOP 5 MV .L1 A9,A4 B .S2 B3 NOP 5