# Lab 5 **Name: G.NISHCHITH** **Roll No.: CS22B021** --- ## Question 1 **CODE** **Before Optimization** ```assembly= #**before optimization** .data arr: .word 7,2,3,6,9,5,1,8,0,4,10,11 s: .string " " .text la x1 arr la x3 arr li x2 12 #length of array li x8 13 #for breaking the loop li x10 0 loop: lw x4 44(x1) #loading word in x4 addi x4 x4 3 #adding 3 to x4 sw x4 44(x1) #again storing in the same place after addition addi x1 x1 -4 addi x8 x8 -1 blt x8 x0 print bgt x8 x0 loop print: lw x9 0(x3) add a0 x9 x0 li a7 1 ecall la a0 s li a7 4 ecall addi x2 x2 -1 addi x3 x3 4 bne x2 x0 print ``` ![Screenshot 2024-02-28 223458](https://hackmd.io/_uploads/HJJAPkp36.png) **After optimization** ```assembly= #**After optimization** .data arr: .word 7,2,3,6,9,5,1,8,0,4,10,11 s: .string " " .text la x1 arr la x3 arr li x2 12 # length of array li x10 0 addi x8 x0 44 loop: lw x4 44(x1) #loading word in x4 coming from back to front lw x5 40(x1) #Considering 4 elements in the array at a time lw x6 36(x1) lw x7 32(x1) addi x4 x4 3 addi x5 x5 3 addi x6 x6 3 #incrmenting each element by 3 addi x7 x7 3 sw x4 44(x1) sw x5 40(x1) #After incrementation again storing in the same place sw x6 36(x1) sw x7 32(x1) addi x1 x1 -16 addi x8 x8 -16 blt x8 x10 print bgt x8 x10 loop print: #printing the values lw x9 0(x3) add a0 x9 x0 li a7 1 ecall la a0 s li a7 4 ecall addi x2 x2 -1 addi x3 x3 4 bne x2 x0 print ``` ![image](https://hackmd.io/_uploads/BJN_Ykpha.png) **Observation:** Before optimization the ipc is around 0.652 but after unrolling the ipc is 0.669. After unrolling by 4 it was incresed then before Reason may be the unrolling leades to increase in the number of instructions execute parallely which may leads to stalls in the pipeline --- ## Question 2 **Code** ```assembly= .data s: .string " " node1: .word 1 .word 0 node2: .word 2 .word 0 node3: .word 3 .word 0 node4: .word 4 .word 0 node5: .word 5 #storing value .word 0 #storing address node6: .word 6 .word 0 node7: .word 7 .word 0 node8: .word 8 .word 0 node9: .word 9 .word 0 node10: .word 10 .word 0 .text main: la x1 node1 #loading address la x2 node2 la x3 node3 la x4 node4 la x5 node5 la x6 node6 la x7 node7 la x8 node8 la x9 node9 la x10 node10 sw x2 4(x1) #storing next node address in preveious in second word sw x3 4(x2) sw x4 4(x3) sw x5 4(x4) sw x6 4(x5) sw x7 4(x6) sw x8 4(x7) sw x9 4(x8) sw x10 4(x9) li x20 10 #length of linked list li x11 0 mv x12 x1 #copying the address of x1 in x12(first node) li x13 0 loop: lw x13 4(x12) #loading word of next node address of x1's in x13 sw x11 4(x12) #updating the next node address in current node of the previous mv x11 x12 #Updating the previous node to current mv x12 x13 #moving to next node bne x13 x0 loop mv x16 x11 print: #printing the values lw x18 0(x16) add a0 x18 x0 li a7 1 ecall la a0 s li a7 4 ecall addi x20 x20 -1 lw x19 4(x16) mv x16 x19 bne x20 x0 print ``` **Output:** **10 9 8 7 6 5 4 3 2 1** ![image](https://hackmd.io/_uploads/B1KAoka3a.png) ![Screenshot 2024-02-28 225012](https://hackmd.io/_uploads/H1r13J6np.png) --- # Question: 3 **Code** ```assembly= .data # condering linked list of 7 nodes before adding node1: .word 1,0 node2: .word 2,0 node3: .word 3,0 node4: .word 4,0 #node in which first word stores value and second word stores address node5: .word 5,0 node6: .word 6,0 node7: .word 7,0 newnode: .word 8,0 #new node str: .string "Before insert: " str1: .string "After insert: " str2: .string " " str3: .string "\n" .text main: la x1 node1 la x2 node2 la x3 node3 la x4 node4 la x5 node5 la x6 node6 #loading address in registers la x7 node7 la x8 newnode sw x2 4(x1) sw x3 4(x2) sw x4 4(x3) #updtind address of next nodes address in current nodes second word sw x5 4(x4) sw x6 4(x5) sw x7 4(x6) addi x9 x0 7 #length of before nodes addi x20 x20 8 #length of after adding new node mv x11 x1 mv x14 x1 j print adding: sw x8 4(x4) #adding new node in 5th postion sw x5 4(x8) #adding of before 5th postion address in new node second word j print2 print: #printing the values before adding la a0 str li a7 4 ecall print1: lw a0 0(x11) li a7 1 ecall la a0 str2 li a7 4 ecall addi x9 x9 -1 lw x12 4(x11) mv x11 x12 beq x9 x0 adding bne x9 x0 print1 print2: #printing after adding la a0 str3 li a7 4 ecall la a0 str1 li a7 4 ecall print3: lw a0 0(x14) li a7 1 ecall la a0 str2 li a7 4 ecall addi x20 x20 -1 lw x15 4(x14) mv x14 x15 bne x20 x0 print3 ``` ![image](https://hackmd.io/_uploads/SJ3T3yanp.png)