Problem: Consider the following two SRC code segments for implementing multiplication. Find which one is more efficient in terms of instruction count and execution time. Program 1: Multiplication by using repeated addition in a for loop Program 2: Multiplication using sub-routine call la r5, 5 ; load value of loop lar r6,mpy ;load address of mpy lar r7, next ;load address of next ld r2, b ; load contents of b in r2 ld r3, c ; load contents of c in r3 la r4, 0 ;load 0 in r4 mpy: brzr r7,r5 ; jump to next after 5 iteration add r4,r4,r3 ;r4 contains r4+c addi r5,r5,-1 ; decrement index br r6 ; loop again next: add r4,r4,r2 ; r4 contains sum of b st r4, a ;store at address label a lar r1,mpy ;load address of mpy in r1 la r3,5 ; load index in r3 ld r4,c ; load contents of c in r4 brl r5, r1 ; r5 contains PC add r2,r2,r7 ; r2 contains sum of b & 5c st r2, a lar r8,again ;r8 contain again address again: brzr r5,r3 ;exit loop when index is 0 add r7,r7,r4 ; r7 contains r7+c addi r3,r3,-1 ; decrement index br r8
Solution The instructions in both programs can be divided into 3 types and the respective count of each type is Number of.. Program 1 Program 2 Data transfer instructions 7 6 Control instructions 2 3 ALSU instructions IC for program 1 = 7 + 2 + 3= 12 IC for program 2 = 6 + 3 + 3= 12
Solution contd.. For execution time, consider the following SRC specifications. ET = IC x CPI x T ET1= (7x4)+(2x2)+(3x3) = 41 ET2= (6x4)+(3x2)+(3x3) = 39 Conclusion: Although the instruction count for both programs is same, program 2 runs much faster than program 1 due to lesser number of clock cycles required. Instruction Type CPI Control 2 ALSU 3 Data Transfer 4