---
# System prepended metadata

title: '#Lab 5'

---

# Lab 5
Name: Bilwani K
Roll no.: CS22B013

---
## Question 1

**Code after enrolling the Loop by factor four**


```assembly=
.data
.word 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0x10 0x11 0x12
base: .word 0x10000000

.text

addi x10,x0  3
addi x11,x0  10 
lw x12, base 

loop:
  
  lw x17, 0(x12) 
  add x17, x17, x10 
  sw x17, 0(x12) 

  
  lw x17, 4(x12) 
  add x17, x17, x10 
  sw x17, 4(x12) 

  
  lw x17, 8(x12) 
  add x17, x17, x10 
  sw x17, 8(x12) 

  lw x17, 12(x12) 
  add x17, x17, x10 
  sw x17, 12(x12)

  
  addi x12, x12, 16


  addi x11, x11, -16 
  bge x11, x16, loop 
  
 
  
  ```
  IPC before the optimisation is - 0.661
  IPC after the optimisation is  - 0.731
  
  It increased because for each iteration of original lopp or before unrolling it has one instructions of lw, add, sw, bge. 
  but for the unrolled loop that is after it has 4 lw, add, sw, bge. This reduces the number of loop control instructions and loop overhead, allowing the CPU to execute more instructions in parallel and leading to higher IPC.
  
  
  
  ## Question 2
  

In the context of RISC-V assembly language, the term "linked list" refers to a data structure implemented using memory pointers in the RISC-V architecture. A linked list consists of a sequence of nodes, where each node contains both data and a reference (or pointer) to the next node in the sequence.
In RISC-V assembly language, implementing a linked list typically involves manipulating memory addresses and using load and store instructions to access data elements within the nodes. This includes allocating memory for new nodes, updating pointers to maintain the list structure, traversing the list, and performing various operations such as insertion, deletion, and searching.


**Initialize a linked list with 10 elements, and reverse the Linked list**
```assembly=
.data
node1: .word 1, 0 # value, pointer to next node
node2: .word 2, 0
node3: .word 3, 0
node4: .word 4, 0
node5: .word 5, 0
node6: .word 6, 0
node7: .word 7, 0
node8: .word 8, 0
node9: .word 9, 0
node10: .word 10, 0

.text

    # Link the nodes
    la x10, node1
    la x11, node2
    la x12, node3
    la x13, node4
    la x14, node5
    la x15, node6
    la x16, node7
    la x17, node8
    la x18, node9
    la x19, node10

    sw x11, 4(t0) 
    sw x12, 4(t1) 
    sw x13, 4(t2) 
    sw x14, 4(t3) 
    sw x15, 4(t4) 
    sw x16, 4(t5) 
    sw x17, 4(t6) 
    sw x18, 4(t7) 
    sw x19, 4(t8) 
    sw x0, 4(t9) # Link node10 to NULL (0)
    
    # Reverse the linked list
    la x10, node1 
    li x11, 0     
    la x12, node1 

reverse_loop:
    lw x13, 4(x12)  
    sw x11, 4(x12)  
    mv x11, x12     
    mv x12, x13    
    bnez x12, reverse_loop  


    la x10, node1
    sw x11, 4(x10)

      
 ```
 
 ## Question 3
 ```assembly=
 .data
node1: .word 1, 0
node2: .word 2, 0
node3: .word 3, 0
new_node: .word 0, 0

.text

allocate_node:
    la x10, new_node
    lw x11, 0(x10)
    lw x12, 4(x10)
    li x13, 8
    li a0, 9
    li a1, 0
    li a2, 0
    ecall
    li x14, -1
    bne a0, x14, allocate_successful

allocate_successful:
    sw a0, 0(x10)
    sw zero, 4(x10)
    jr ra

exit_program:
    li a0, 0
    li a7, 93
    ecall

.text
main:
    la x10, node1
    la x11, node2
    la x12, node3

    sw x11, 4(x10) 
    sw x12, 4(x11) 
    sw zero, 4(x12) 
    
    jal allocate_node

    j exit_program
```
