---
# System prepended metadata

title: Lab x

---

# Lab 5
Name: Aditya Raghuveer
Roll No.: CS22B019


## Question 1
```assembly=
.data
x: .word 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0x10, 0x11, 0x12 
###### Array x with 12 integers initialized above
s: .word 0x3      # Value of s

.text
    la t0, x      # Load address of array x into t0
    la t1, s      # Load address of s into t1
    lw t1, 0(t1)  # Load s from memory
  addi t2,x0,10   #initialise loop counter
    
    
loop:
    # Unrolled loop iteration for x[i]
    lw t3, 0(t0)     # Load x[i]
    add t3, t3, t1   # x[i] += s
    sw t3, 0(t0)     # Store updated x[i]
# Unrolled loop iteration for x[i - 1]
    lw t3, -4(t0)    # Load x[i - 1]
    add t3, t3, t1   # x[i - 1] += s
    sw t3, -4(t0)    # Store updated x[i - 1]

    # Unrolled loop iteration for x[i - 2]
    lw t3, -8(t0)    # Load x[i - 2]
    add t3, t3, t1   # x[i - 2] += s
    sw t3, -8(t0)    # Store updated x[i - 2]

    # Unrolled loop iteration for x[i - 3]
    lw t3, -12(t0)   # Load x[i - 3]
    add t3, t3, t1   # x[i - 3] += s
    sw t3, -12(t0)   # Store updated x[i - 3]

    # Update loop counter (i -= 4)
    addi t0, t0, -16  # Move to next set of elements
    addi t2, t2, -4   # Decrement loop counter

    # Check loop condition
    bgez t2, loop     # If t2 >= 0, continue loop

    # End of loop
    


**Observation:** 
IPC value before unrolling was 0.65 and after it was 0.66 which was an increment . higher ipc value indicates that processor can execute more instructions per cycle which improves performance.
```
---
## Question 2

```assembly=
.data
node1: .word 1 # value
.word 0 # pointer to next node

node2: .word 2
.word 0

node3: .word 0
.word 0

node4: .word 0
.word 0

node5: .word 0
.word 0

node6: .word 0
.word 0

node7: .word 0
.word 0

node8: .word 0
.word 0

node9: .word 0
.word 0

node10: .word 0
.word 0

.text
# link the nodes

la t0, node1

la t1, node2

la t2, node3

la t3, node4

la t4, node5

la t5, node6

la t6, node7

la x7, node8

la x8, node9

la x9, node10



sw t1, 4(t0) 

sw t2, 4(t1) #storing address of successive node in previous node

sw t3, 4(t2)

sw t4, 4(t3)

sw t5, 4(t4)

sw t6, 4(t5)

sw x7, 4(t6)

sw x8, 4(x7)

sw x9, 4(x8)

sw x0, 4(x9) # Set the last node's next pointer to NULL



# reverse the linked list

la t0, node1 # t0 points to the first node

li t1, 0 # Initialize t1 (previous) to NULL



reverse_loop:

lw t2, 4(t0) # Load the next pointer of the current node

sw t1, 4(t0) # Update the next pointer of the current node to the previous node

mv t1, t0 # Update the previous node to the current node

mv t0, t2 # Move to the next node

bnez t0, reverse_loop # Repeat until t0 is not NULL



# At this point, t1 points to the new head of the reversed list



```
---
## Question 3
```assembly=
.data
.word 1 2 3 4 5 6 7 8 9 10 11
base: .word 0x10000000
node1: .word 0 # value
       .word 0 # pointer to next node
node2: .word 0
       .word 0
node3: .word 0
       .word 0
node4: .word 0
       .word 0
node5: .word 0
       .word 0
node6: .word 0
       .word 0
node7: .word 0
       .word 0
node8: .word 0
       .word 0
node9: .word 0
       .word 0
node10: .word 0
        .word 0
str1: .string "Linked List is \n"
str2: .string "New Linked List is \n"
str3: .string " "
str4: .string "\n"

.text         # init the list
lw x1,base
addi t0,x0,0  #i=0
addi t1,x0,10 #i=10
la s0,node1
init:
    beq t0,t1,main1
    lw a0,0(x1)
    addi x1,x1,4
    sw a0,0(s0)
    addi s0,s0,8
    addi t0,t0,1
    j init
    
main1:        # link the nodes
addi t0,x0,1  # run only 9 times
la s0,node2
link:
    beq t0,t1,print1
    add a0,x0,s0
    sw a0,-4(s0)
    addi s0,s0,8
    addi t0,t0,1
    j link

print1:      # prints linked list
    la a0 str1
    li a7 4
    ecall
addi t0,x0,0  #i=0
addi t1,x0,10 #i=10
la s0,node1
loop1: 
    beq t0,t1,final
    lw a0,0(s0)
    li a7 1
    ecall
    la a0 str3
    li a7 4
    ecall
    lw s0,4(s0)
    addi t0,t0,1
    j loop1    
    
final:        # adds new node
lw x1,base
la s0,node10
addi a0,s0,8
sw a0,4(s0)
lw t0,40(x1)
sw t0,0(a0)
#sw t0,8(s0)
    
print2:        # prints new linked list
    la a0 str4
    li a7 4
    ecall
    la a0 str2
    li a7 4
    ecall
addi t0,x0,0  #i=0
addi t1,x0,11 #i=10
la s0,node1
loop2: 
    beq t0,t1,end
    lw a0,0(s0)
    li a7 1
    ecall
    la a0 str3
    li a7 4
    ecall
    lw s0,4(s0)
    addi t0,t0,1
    j loop2

end:
li a7,10

**Observation:** 
final function describes on how to create a node.remaining part describes linking to node , initialsing and printing 

```
---