# Lab 5
Name: Tilak Reddy
Roll No.: CS22B047
---
## Question 1
_without loop unrolling (IPC=0.701)_
```assembly=
.data
array: .word 0,0,0,0,0,0,0,0,0,0,0,0
s: .word 3
.text
la x4 array #array address
lw x5 s #x5 = s
li x6 11 #x6 = i
loop:
slli x7 x6 2
add x7 x4 x7
lw x8 0(x7) #x8 = s[i]
add x8 x8 x5 #x8 = x8 + 3
sw x8 0(x7) #s[i] = s[i] + 3
addi x6 x6 -1 #i = i-1
bge x6 x0 loop
#IPC = 0.701
```
_with loop unrolling (IPC=0.726)_
```assembly=
.data
array: .word 0,0,0,0,0,0,0,0,0,0,0,0
s: .word 3
.text
la x4 array #array address
lw x5 s #x5 = s
li x6 11 #x6 = i
loop:
slli x7 x6 2
add x7 x4 x7
lw x8 0(x7) #x8 = s[i]
add x8 x8 x5 #x8 = x8 + 3
sw x8 0(x7) #s[i] = s[i] + 3
lw x8 -4(x7) #x8 = s[i]
add x8 x8 x5 #x8 = x8 + 3
sw x8 -4(x7) #s[i] = s[i] + 3
lw x8 -8(x7) #x8 = s[i]
add x8 x8 x5 #x8 = x8 + 3
sw x8 -8(x7) #s[i] = s[i] + 3
lw x8 -12(x7) #x8 = s[i]
add x8 x8 x5 #x8 = x8 + 3
sw x8 -12(x7) #s[i] = s[i] + 3
addi x6 x6 -4 #i = i-1
bge x6 x0 loop
#ipc = 0.726
```
---
## Question 2
_In the context of assembly language, a linked list refers to a data structure where each element in the list contains a value and address to the next element in the sequence. In RISC-V assembly, this can be implemented by using la and sw instructions link nodes together._
```assembly=
.data
node1: .word 1,0
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
#initializing linked list
la t0 node1
la t1 node2
sw t1 4(t0)
la t0 node2
la t1 node3
sw t1 4(t0)
la t0 node3
la t1 node4
sw t1 4(t0)
la t0 node4
la t1 node5
sw t1 4(t0)
la t0 node5
la t1 node6
sw t1 4(t0)
la t0 node6
la t1 node7
sw t1 4(t0)
la t0 node7
la t1 node8
sw t1 4(t0)
la t0 node8
la t1 node9
sw t1 4(t0)
la t0 node9
la t1 node10
sw t1 4(t0)
#reversing linked llist
la t0 node1 #current
lw t3 4(t0) # next
la t2 node1 #prev
li t1 9
rev_loop:
lw t3 4(t0) #next = current->next
sw t2 4(t0) #current->next = prev
mv t2 t0 #prev = current
mv t0 t3 #current = next
addi t1 t1 -1
bge t1 x0 rev_loop
```
---
## Question 3
_Linked list initialization using new_node function_
```assembly=
.data
node: .word 0,0
.text
la t0 node
li t1 0 #iterator
li t2 1 #new node will have t2 as data
jal x1 new_node
li t2 2
jal x1 new_node
li t2 3
jal x1 new_node
li t2 4
jal x1 new_node
li t2 5
jal x1 new_node
li t2 6
jal x1 new_node
li t2 7
jal x1 new_node
li t2 8
jal x1 new_node
li t2 9
jal x1 new_node
li t2 10
jal x1 new_node
#exit
li a7 10
ecall
new_node:
slli t3 t1 2
add t3 t3 t0
sw t2 0(t3) #new_node->data = t2
addi t4 t3 8 #t4 = address of next node
sw t4 4(t3) #new_node->next = t4
addi t1 t1 2 #incrementing iterator
jr ra
```
---