---
# System prepended metadata

title: '#LA'

---

# LAB 5
Name : Guru Rohith.G
Roll No: CS22B022

## Question 1:

**Code**
```assembly=
.data
array: .word 2,4,6,8,10,12,1,3,5,7,9,11
st: .string " "
.text
main:
    li x1, 3            
    la x2, array     
    li x3, 11        # Initialize i to 11
    li x25 12        
loop:
    lw x4, 44(x2)    
    add x4, x4, x1 
    sw x4, 44(x2)    
    lw x4, 40(x2)   
    add x4, x4, x1   
    sw x4, 40(x2)    
    lw x4, 36(x2)    
    add x4, x4, x1   
    sw x4, 36(x2)
    lw x4, 32(x2)
    add x4, x4, x1   
    sw x4, 32(x2)    
    addi x2, x2, -16 
    addi x3, x3, -4  # Decrease i by 4
    bge x3, x0, loop # Repeat until i >= 0
    la x2, array
    li x3, 0         # Initialize loop counter
    
print_loop:
    lw x4, 0(x2)    
    mv a0, x4        
    li a7, 1       
    ecall
    la a0, st      
    li a7, 4         
    ecall
    addi x2, x2, 4   # Move to the next element
    addi x3, x3, 1   # Increment loop counter
    blt x3, x25, print_loop 
    li a7, 10       
    ecall

```
Output

![Screenshot 2024-02-28 191928](https://hackmd.io/_uploads/HyJbq2hnp.png)


---
---
### before optimization 
![Screenshot 2024-02-28 212032](https://hackmd.io/_uploads/S1HPwA23p.png)
### after optimization
![Screenshot 2024-02-28 211754](https://hackmd.io/_uploads/HyHuw033p.png)


## Question 2:
**Code**
```assembly=
.data
node1: .word 1   
       .word 0   
node2: .word 3
       .word 0
node3: .word 5   
       .word 0   
node4: .word 7   
       .word 0   
node5: .word 9   
       .word 0   
node6: .word 2   
       .word 0   
node7: .word 4   
       .word 0   
node8: .word 6
       .word 0
node9: .word 8   
       .word 0   
node10: .word 10  
        .word 0  

.text
# Link the nodes
la x0, node1   
la x1, node2   
la x2, node3   
la x3, node4   
la x4, node5   
la x5, node6   
la x6, node7   
la x7, node8  
la x8, node9   
la x9, node10  

sw x1, 4(x0)   
sw x2, 4(x1)  
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) # Store the address of node10 in node9

# Reverse the linked list
la x0, node1   # x0 points to the first node
li x1, 0       

reverse:
lw x2, 4(x0)  # next to current
sw x1, 4(x0)  
mv x1, x0      
mv x0, x2      # Move to the next node
bnez x0, reverse   # Repeat until x0 is not NULL


```

![Screenshot 2024-02-28 221734](https://hackmd.io/_uploads/r1QC7J6hp.png)
![Screenshot 2024-02-28 221753](https://hackmd.io/_uploads/S1kk4ypnT.png)


## Question 3:
```assembly=
.data
head: .word 4
      .word 0
middle: .word 3
        .word 0      
middle2: .word 5
      .word 0
middle3: .word 1
      .word 0
tail: .word 2
      .word 0      
space: .string " "

.text

createnode:
la t0, head  #Load the address of head into t0
addi t0, t0, 4       
    loop:
        lw t1, 0(t0)      
        addi t0, t0, 8   
        bne t1, x0, loop  
    addi t0, t0, -8      
    la t2, tail           
    sw t2, 0(t0)         
    jalr x0, x7, 0       
# Subroutine to print the linked list
print_linked_list:
    la a0, head          
    li a1, 5             
print_loop:
    lw a0, 0(a0)          
    mv a1, a0
    li a7, 1         
    ecall
    la a0, space         
    li a7, 4           
    ecall
    lw a0, 4(a0)         
    addi a1, a1, -1      
    bnez a1, print_loop   
    

.text
main:
    la x1, head
    la x2, middle
    sw x2, 4(x1)
    la x3, middle2
    sw x3, 4(x2)
    la x4, middle3
    sw x4, 4(x3)
    la x5, tail
    sw x5, 4(x4)
    sw x0, 4(x5)         

    jal x7, createnode    # Allocate a new node

    # Print the updated linked list
    jal print_linked_list

    li a7, 10             # Exit syscall
    ecall


```

![Screenshot 2024-02-28 223907](https://hackmd.io/_uploads/rJIqFyTnT.png)



