---
# System prepended metadata

title: Lab x

---

# Lab 5
Name:bhukya lachiram nayak
Roll No.: cs22b012

---
## Question 1

**Code** (if required)
```assembly=

 .data
x: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11  # Array x with 12 integers initialized to 0
s: .word 3      # 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  # Initialize loop counter (i = 10)
    
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 :Optimization

```



## Question 2
**Code** (if required)
```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)
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
# You can use t1 or the label of the first node of the reversed list to access it
OBSERVATION:Reversal

```
## Question 2
**Code** (if required)
```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,main2
    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    
    
main2:        # 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:
LinkedListManipulation


```
