---
# System prepended metadata

title: Lab 4

---

# Lab 5
Name: Anuj Sharma
Roll No.: CS22B007

---

## Question 1
Code without unrolling:
```assembly=
.data
.word 1 2 3 4 5 6 7 8 9 10 11 12
base: .word 0x10000000
.text
addi x4 x0 3 #x4 is s
addi x5 x0 11 #x5 is i
lw x16 base
addi x3 x16 0 #x3 is pointer to array

loop:
    lw x8 0(x3)
    add x8 x8 x4
    sw x8 0(x3)
    addi x3 x3 4
    addi x5 x5 -1
    bge x5 x0 loop
 
li a7 10
ecall

```
IPC = 0.658

Code by unrolling for loop:
```assembly=
.data
.word 1 2 3 4 5 6 7 8 9 10 11 12
base: .word 0x10000000
.text
addi x4 x0 3 #x4 is s
addi x5 x0 11 #x5 is i
lw x16 base
addi x3 x16 0 #x3 is pointer to array

loop:
    lw x8 0(x3)
    lw x9 4(x3)
    lw x10 8(x3)
    lw x11 12(x3)
    add x8 x8 x4
    add x9 x9 x4
    add x10 x10 x4
    add x11 x11 x4
    sw x8 0(x3)
    sw x9 4(x3)
    sw x10 8(x3)
    sw x11 12(x3)
    addi x3 x3 16
    addi x5 x5 -4
    bge  x5 x0 loop

li a7 10
ecall
```
IPC = 0.825
## Question 2
```assembly= 
.data
node1: .word 1 # value
       .word 0 # pointer to next node
node2: .word 2
       .word 0
node3: .word 3
       .word 0
node4: .word 4
       .word 0
node5: .word 5
       .word 0
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
# link the nodes
la x1 node1
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)
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)

# the NULL pointer is 0
la x20 node1
addi x2 x20 0
addi x6 x0 0
#x6 previous address initially null (zero)
#x7 next address
#x2 current address
loop2:
    lw x7 4(x2)
    sw x6 4(x2)
    addi x6 x2 0
    addi x2 x7 0
    bne x2 x0 loop2
    
#linked list is reversed
```
## Question 3
```assembly=
addi x1 x1 0 # free memory pointer in x1
lw x5 0(x1) #root node

AllocateNode:
    sw x1 0(a1) #a1 is argument for value
    sw x3 4(x2) #x2 contains address of previous node
    sw x0 4(x1) #making next of current node to null
    
    

```
