---
# System prepended metadata

title: lab5

---

# Lab 5
Name: Havish Jadav
Roll No.: cs22b026
___
## Question 1
**code**
```assembly=
  .data 
.word 1 3 2 4 5 9 7 11 8 10 14 89
base: .word 0x10000000
.text
lw x16 base
addi x1 x0 0
addi x5 x0 12
addi x3 x0 3
addi x20 x0 12
loop:
ble x5 x0 exit    
lw x4 0(x16)
add x4 x4 x3
sw x4 0(x16)

lw x15 4(x16)
add x15 x15 x3
sw x15 4(x16)

lw x6 8(x16)
add x6 x6 x3
sw x6 8(x16)

lw x7 12(x16)
add x7 x7 x3
sw x7 12(x16)

addi x16 x16 16
addi x5 x5 -4 
j loop

exit:
    addi x0 x0 0
    addi x25 x0 0
    lw x16, base
done5:

beq x25, x20, exit2
lw x10 0(x16)
add a0, x0, x10
li a7, 1
ecall
addi x16, x16, 4
addi x25, x25, 1
addi a0 x0 32
li a7 11
ecall
addi x14 x14 10
j done5
exit2:
    addi x0 x0 0
```
___
## Question 2
```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
    la x1, node1
    la x2, node2
    la x3, node3
    la x4, node4
    la x5, node5
    la x6, node6
    la x7, node7
    la x8, node8
    la x9, node9
    la x10, node10

    sw x2, 4(x1)
    sw x3, 4(x2)
    sw x4, 4(x3)
    sw x5, 4(x4)
    sw x6, 4(x5)
    sw x7, 4(x6)
    sw x8, 4(x7)
    sw x9, 4(x8)
    sw x10, 4(x9)
    
exit:
    addi x20, x0, 10
    addi x25, x0, 0
    la x16, node1
    
done5:
    beq x25, x20, exit2
    lw x11, 0(x16)
    add a0, x0, x11
    li a7, 1
    ecall
    lw x16 4(x16)
    addi x25, x25, 1
    addi a0, x0, 32
    li a7, 11
    ecall
    addi x14, x14, 10
    j done5

exit2:
    addi x0, x0, 0
        la x1, node1
    la x2, node2
    la x3, node3
    la x4, node4
    la x5, node5
    la x6, node6
    la x7, node7
    la x8, node8
    la x9, node9
    la x10, node10
    sw x1, 4(x2)
    sw x2, 4(x3)
    sw x3, 4(x4)
    sw x4, 4(x5)
    sw x5, 4(x6)
    sw x6, 4(x7)
    sw x7, 4(x8)
    sw x8, 4(x9)
    sw x9, 4(x10)

    addi x20, x0, 9
    addi x25, x0, 0
    la x16, node10
    lw x11, 0(x16)
    add a0, x0, x11
    li a7, 1
    ecall
    addi a0, x0, 32
    li a7, 11
    ecall
    lw x16 4(x16)
done6:
    beq x25, x20, exit4
    lw x11, 0(x16)
    add a0, x0, x11
    li a7, 1
    ecall
    lw x16 4(x16)
    addi x25, x25, 1
    addi a0, x0, 32
    li a7, 11
    ecall
    addi x14, x14, 10
    j done6

exit4:
    addi x0, x0, 0


```
___
## Question 3
**code**
```assembly=
allocate_node:
    li t1, 0x8
    li a7, 93
    ecall

    bnez a0, allocation_successful
    mv a0, zero
    ret

allocation_successful:
    li t2, 0
    sw t2, 0(a0)
    sw t2, 4(a0)

    ret
```    
___
