---
# System prepended metadata

title: LAB 5

---


# LAB 5
NAME - AMAN ANAND
ROLL NO - CS22B054

## QUESTION NO 1
### BEFORE OPTIMIZATION
 `code`
 
    .data
    arr:
    .word 1,2,3,4,5,6,7,8,9,10,11
      .text
       main:
     la s0 arr
    li s1 3
      li s2 10
    li s3 -1
    loop1:
    beq s2 s3 exit
    lw s4 40(s0)
    add s4 s4 s1
      sw s4 40(s0)
    addi s2 s2 -1
    addi s0 s0 -4
    exit:
    li a7 10
    #IPC: 0.714
    
   
   ### AFTER OPTIMIZATION
`code`

    .data
     arr:
     .word 1,2,3,4,5,6,7,8,9,10,11
    .text
    main:
    la s0 arr
     li s1 3
    li s2 10
    li s3 2
    loop1:
    beq s2 s3 temp
    lw s4 40(s0)
    add s4 s4 s1
    sw s4 40(s0)
     lw s4 36(s0)
    add s4 s4 s1
    sw s4 36(s0)
    lw s4 32(s0)
    add s4 s4 s1
    sw s4 32(s0)
    lw s4 28(s0)
    add s4 s4 s1
    sw s4 28(s0)
    addi s2 s2 -4
    addi s0 s0 -16
    temp:
    li s2 -1
    loop2:
    beq s3 s2 exit
     lw s4 40(s0)
     add s4 s4 s1
    sw s4 40(s0)
    addi s2 s2 -1
    addi s0 s0 -4
    exit:
     li a7 10 
     #IPC: 0.764
 
 IPC is increased after unrolling by 0.05
 
By unrolling a loop, the compiler generates code that executes multiple iterations of the loop body in a single iteration, reducing the overhead of loop control and increasing the amount of work done in each iteration. This can improve instruction-level parallelism (ILP) and help achieve higher instructions per cycle (IPC) at runtime.

## QUESTION NO 2
```javascript=
.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

.global main


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 
    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
```

## QUESTION N0 3

```javascript=
.data
node1: .word 1
.word 0
node2: .word 2
.word 0
node3: .word 3
.word 0
node4: .word 4
.word 0
.text
.global main
main:
    li x19,5
 la x1,node1
 la x2,node2
 sw x2,4(x1)
 la x3,node3
 sw x3,4(x2)
 la x4, node4
 sw x4,4(x3)

  


    la x2, node1
    fia:
        beq x2,x0,new_node 
        lw x3, 4(x2)
    lw a0,0(x2)
    li a7,1
    ecall
  mv x2,x3
    j fia
    new_node:
       la x2, node1
       mv x3,x2
       loop:
           beq x3,x0,add_node
           lw x4 ,4(x3)
           mv x5,x3
           mv x3,x4
           j loop
           add_node:
               addi x7,x5,8
               sw x7,4(x5)
               sw x19,0(x7)
               sw x0, 4(x7)
               j print_again
               print_again:
                    la x2, node1
    fa:
        beq x2,x0,exit
        lw x3, 4(x2)
    lw a0,0(x2)
    li a7,1
    ecall
  mv x2,x3
    j fa
                   
    exit:
        li a7,10
        ecall
     
```



==END==

            
   