---
# System prepended metadata

title: Lab-5

---

# Computer Organisation Lab
# LAB-5

## Question-1
---
Without unrolling:
```assembly= 
.data
.word -3 0 -3 0 -3 0 -3 0 -3 0 -3 0
arr: .word 0x10000000

.text
addi x10 x0 3 #s
addi x11 x0 12 # i = 12
addi x12 x0 0 
lw x9 arr #x9 is base register

loop:
    beq x12 x11 exit
    lw x13 0(x9)         # load arr[i] into x13
    add x13 x13 x10     # arr[i]+ = s
    sw x13 0(x9)
    addi x9 x9 4       # x9 = x9 + 4
    addi x11 x11 -1     # i--
    j loop

exit:
    lw x16, arr
    addi x1 x0 11
    addi x2 x0 0
```

```
IPC=0.788
```

With unrolling:
```assembly=
.data
.word -3 0 -3 0 -3 0 -3 0 -3 0 -3 0
arr: .word 0x10000000

.text
addi x10 x0 3 #s
addi x11 x0 12 # i = 12
addi x12 x0 0 
lw x9 arr #x9 is base register

loop:
    beq x12 x11 exit
    lw x13 0(x9)         # load arr[i] into x13
    add x13 x13 x10     # arr[i]+ = s
    sw x13 0(x9)
    addi x9 x9 4       # x9 = x9 + 4
    addi x12 x12 1     # i--
    lw x13 0(x9)         # load arr[i] into x13
    add x13 x13 x10     # arr[i]+ = s
    sw x13 0(x9)
    addi x9 x9 4       # x9 = x9 + 4
    addi x12 x12 1     # i--
    lw x13 0(x9)         # load arr[i] into x13
    add x13 x13 x10     # arr[i]+ = s
    sw x13 0(x9)
    addi x9 x9 4       # x9 = x9 + 4
    addi x12 x12 1     # i--
    lw x13 0(x9)         # load arr[i] into x13
    add x13 x13 x10     # arr[i]+ = s
    sw x13 0(x9)
    addi x9 x9 4       # x9 = x9 + 4
    addi x12 x12 1     # i--
    j loop

exit: 
    lw x16, arr
    addi x1 x0 11
    addi x2 x0 0
```

```
IPC=0.817
```

## Question-2

--- 

```assembly=
. data
node1 : . word 0 # value
. word 0 # pointer to next node
node2 : . word 0
. word 0
node3 : . word 0
. word 0

. text
# link the nodes
la t0 , node1
la t1 , node2
la t2 , node3
sw t1 , 4( t0 )
sw t2 , 4( t1 )
# the NULL pointer is 0
```

assembly code for initializing linked-list with 10 elements and then reverse it
```assembly=
.data
node1:  .word -1
        .word 0  
node2:  .word 0
        .word 0
node3:  .word 4
        .word 0
node4:  .word 34
        .word 0
node5:  .word 51
        .word 0
node6:  .word 1
        .word 0
node7:  .word 2
        .word 0
node8:  .word 3
        .word 0
node9:  .word 6
        .word 0
node10: .word 143
        .word 0
    
.text
    la t0 node1
    la t1 node2
    la t2 node3
    la t3 node4
    la t4 node5
    la t5 node6
    la t6 node7
    la s0 node8
    la s1 node9
    la s2 node10    
    
    sw t1 4(t0)
    sw t2 4(t1)
    sw t3 4(t2)
    sw t4 4(t3)
    sw t5 4(t4)
    sw t6 4(t5)
    sw s0 4(t6)
    sw s1 4(s0)
    sw s2 4(s1)
    
    #Reversing the LL:    
    la s9,node1

    addi s10,x0,0 #previous node address

    loop:
        lw x2,4(s9) 
        sw s10,4(s9) # updating the previous node address
        addi s10,s9,0
        addi s9,x2,0
        bne s9,x0,loop 
```

## Question-3
---

```
Write assembly code for allocate_node function which allocates a new node in the linked list.
```

```assembly=
.data 

node1: .word 0
       .word 0
node2: .word 0
       .word 0
node3: .word 0
       .word 0
node4: .word 0
       .word 0
.text
    la t0 node1
    la t1 node2
    la t2 node3
    
    sw t1 4(t0)
    sw t2 4(t1)
    sw t3 4(t2)
allocate_node:    #assuming the first 3 nodes are already linked this function links the fourth one
    la t3 node4
    sw t4 4(t3)
```