---
# System prepended metadata

title: Lab5_CS22B057

---

# Lab 5
Name: SHIVADHARSHAN S
Roll No.: CS22B057

---
## Question 1
**Code**
Note: Sir said to take as multiple of 4
Normal:
```assembly=
.data 
arr: .word 1 2 3 4 5 6 7 8 9 10 11 12
s: .word 3
.text
la ra s
lw ra 0(ra)
la x3 arr
addi x5 zero 12
loop:
    beq x5 x0 exit1
    lw x4 0(x3)
    add x4 x4 ra
    sw x4 0(x3)
    addi x3 x3 4
    addi x5 x5 -1
    j loop
exit1:
    addi a7 zero 10
    ecall
    
```


Unrolled:
```assembly=
.data 
arr: .word 1 2 3 4 5 6 7 8 9 10 11 12
s: .word 3
.text
la ra s
lw ra 0(ra)
la x3 arr
addi x5 zero 3
loop1:
    beq x5 x0 exit
    lw x4 0(x3)
    lw x6 4(x3)
    lw x7 8(x3)
    lw x8 12(x3)
    add x4 x4 ra
    add x6 x6 ra
    add x7 x7 ra
    add x8 x8 ra
    sw x4 0(x3)
    sw x6 4(x3)
    sw x7 8(x3)
    sw x8 12(x3)
    addi x3 x3 16
    addi x5 x5 -1
    j loop1
exit:
    addi a7 zero 10
    ecall
    
```
**Observation**

*Normal:
IPC : 0.679*
![Screenshot from 2024-02-27 17-38-22](https://hackmd.io/_uploads/BJi-WUo3a.png)

*Unrolled:
IPC : .77*
![Screenshot from 2024-02-27 17-37-05](https://hackmd.io/_uploads/HJatxLjhT.png)


___

## Question 2

**Code**
```assembly=
.data
node1: .word 1 
.word 0 
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

beq x0 x0 main

# saving head node value to s0 and then using in function 
# t0 t1 t2 
reverse:
    addi t0 x0 0
    mv t1 s0
    mv t2 s0
    loop:
        beq t1 x0 return
        lw t2 4(t1)
        sw t0 4(t1)
        mv t0 t1
        mv t1 t2
        j loop
        
        
return:
    addi s0 t0 0 #return head node
    jalr x0 x1 0
    

# link the nodes

main:
la t0 , node1
la t1 , node2
sw t1 , 4(t0)
la t0 , node3
sw t0 , 4(t1)
la t1 , node4
sw t1 , 4(t0)
la t0 , node5
sw t0 , 4(t1)
la t1 , node6
sw t1 , 4(t0)
la t0 , node7
sw t0 , 4(t1)
la t1 , node8
sw t1 , 4(t0)
la t0 , node9
sw t0 , 4(t1)
la t1 , node10
sw t1 , 4(t0)
la s0 node1
jal x1 reverse

addi a7 zero 10
ecall
    
```
**Observation**

*Before Reverse:*

![Screenshot from 2024-02-27 16-44-34](https://hackmd.io/_uploads/HkM44Sshp.png)

*After Reverse:*

![Screenshot from 2024-02-27 16-43-52](https://hackmd.io/_uploads/HJqENBs2a.png)

___

## Question 3

**Code**
```assembly=
.data
node1: .word 1 
.word 0 
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

beq x0 x0 main # to skip function execution

# saving head node value to s0 and then using in function 
# saving the number to be added in s1
# t0 t1 t2 used as temp registers
allocate_node:
    mv t1 s0
    addi t0 zero 0
    loop:
        beq t1 x0 add
        lw t2 4(t1)
        mv t0 t1
        mv t1 t2
        j loop
    add:
        addi t1 t0 8
        sw t1 4(t0)
        sw s1 0(t1)
        sw zero 4(t1)
        jalr x0 x1 0

main:
la t0 , node1
la t1 , node2
sw t1 , 4(t0)
la t0 , node3
sw t0 , 4(t1)
la t1 , node4
sw t1 , 4(t0)
la t0 , node5
sw t0 , 4(t1)
la t1 , node6
sw t1 , 4(t0)
la t0 , node7
sw t0 , 4(t1)
la t1 , node8
sw t1 , 4(t0)
la t0 , node9
sw t0 , 4(t1)
la t1 , node10
sw t1 , 4(t0)

la s0 node1 
addi s1 zero 16 # data of new node 
jal x1 allocate_node

addi a7 zero 10
ecall

```
**Observation:**

*Before allocate:*

![Screenshot from 2024-02-27 17-27-05](https://hackmd.io/_uploads/HyWQRSj36.png)


*After allocate:*

![Screenshot from 2024-02-27 17-26-29](https://hackmd.io/_uploads/Sy8XRHjn6.png)

___
