---
# System prepended metadata

title: Untitled

---

# Lab 5
Name: Sai Kowshik
Roll No.: CS22B004

---
## Question 1

*
```assembly=
.data
array: .word 1 2 3 4 5 6 7 8 9 10 11 
.text

la x10 array  # first value address

addi x11 zero 11 # size of array

addi x10 x10 44  # last element

addi x5 zero 0
addi x6 zero 3 # s=3

loop:
 beq x6 x5 break 
 
 lw x9 0(x10)
 add x8 x9 x6
 sw x8 0(x10)
 addi x10 x10 -4
 
 lw x9 0(x10)
 add x8 x9 x6
 sw x8 0(x10)
 addi x10 x10 -4
 
 lw x9 0(x10)
 add x8 x9 x6
 sw x8 0(x10)
 addi x10 x10 -4

 lw x9 0(x10)
 add x8 x9 x6
 sw x8 0(x10)
 addi x10 x10 -4

 addi x4 x4 4
 addi x5 x5 1
j loop

break: 
li a7 10







```


IPC before = 0.643
IPC after =  0.739
## Question 2

*
```assembly=
.data
node1: .word 10
   .word 0
node2: .word 9
   .word 0
node3: .word 8
   .word 0
node4: .word 7
   .word 0
node5: .word 6
   .word 0
node6: .word 5
   .word 0
node7: .word 4
   .word 0
node8: .word 3
   .word 0
node9: .word 2
   .word 0
node10: .word 1
   .word 0
.text
la x4 node1
la x5 node2
la x6 node3
la x7 node4
la x8 node5
la x9 node6
la x10 node7
la x11 node8
la x12 node9
la x13 node10

sw x5 4(x4)
sw x6 4(x5)
sw x7 4(x6)
sw x8 4(x7)
sw x9 4(x8)
sw x10 4(x9)
sw x11 4(x10)
sw x12 4(x11)
sw x13 4(x12)
sw x0 4(x13)

sw x12 0(x13)
sw x11 0(x12)
sw x10 0(x11)
sw x9 0(x10)
sw x8 0(x9)
sw x7 0(x8)
sw x6 0(x7)
sw x5 0(x6)
sw x4 0(x5)
sw x0 0(x4)


```

![image](https://hackmd.io/_uploads/ByFsZkTnp.png)

## Question 3

*
```assembly=

.data
head: .word 1, 0
middle: .word 2, 0
middle2: .word 3, 0
tail: .word 4, 0
.text
addi x5 x5 4
j main

newnode:
la x8 head
addi x8 x8 4
loop:
lw x24 0(x8)
addi x8 x8 8
bne x24 x0 loop
addi x8 x8 -8
la x22 tail
sw x22 0(x8)
jalr x0 x7 0 

main:
    la x1 head
    la x2 middle 
    sw x2 4(x1)
    la x3 middle2
    sw x3 4(x2)
    sw x0 4(x3)
    jal x7 newnode
    li a7 10


```
![image](https://hackmd.io/_uploads/rkQYF1ahp.png)




