---
# System prepended metadata

title: 'Lab 05Name: CHATSE SIDDHANT MA'

---

# Lab 05
Name: CHATSE SIDDHANT MADHUKAR
Roll No.: CS22B016

---
## Question 1

**Code:** 
```assembly=
.data
arr: .word 11 12 13 14 15 16 17 18 19 20 21 22
.text
addi x1 x0 3
addi x2 x0 12
la x16 arr
loop:
    lw x5 44(x16)
    add x5 x5 x1
    sw x5 44(x16)
    addi x2 x2 -1
    lw x5 40(x16)
    add x5 x5 x1
    sw x5 40(x16)
    addi x2 x2 -1
    lw x5 36(x16)
    add x5 x5 x1
    sw x5 36(x16)
    addi x2 x2 -1
    lw x5 32(x16)
    add x5 x5 x1
    sw x5 32(x16)
    addi x2 x2 -1
    addi x16 x16 -16
    bne x2 x0 loop
exit:
    la x16 arr
    addi x2 x0 0
    addi x7 x0 12
print_loop:
    beq x2 x7 print_exit
    lw x5 0(x16)
    addi a0 x5 0
    li a7 1
    ecall
    addi a0 x0 32
    li a7 11
    ecall
    addi x16 x16 4
    addi x2 x2 1
    j print_loop
print_exit:
    li a7, 10
    ecall
```


**Output:** 14 15 16 17 18 19 20 21 22 23 24 25

---
## Question 2
**Code:** 
```assembly=
.data
Node_1: .word 11
.word 0
Node_2: .word 12
.word 0
Node_3: .word 13
.word 0
Node_4: .word 14
.word 0
Node_5: .word 15
.word 0
Node_6: .word 16
.word 0
Node_7: .word 17
.word 0
Node_8: .word 18
.word 0
Node_9: .word 19
.word 0
Node_10: .word 20
.word 0

.text 
la x3 , Node_1
la x4 , Node_2
la x5 , Node_3
la x6 , Node_4
la x7 , Node_5
la x8 , Node_6
la x9 , Node_7
la x10 , Node_8
la x11 , Node_9
la x12 ,Node_10

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)
sw x11 ,4(x10)
sw x12 ,4(x11)
addi x13 x13 1
addi x14 x14 20
addi x18 x0 5

p_1:
.data
str:.string "Before reverse:"
.text
la a0 str
li a7 4
ecall 
p_loop_1:
beqz x3 print_new_line
lw x26, 0(x3)
add a0, x0, x26
li a7, 1
ecall

lw x3 4(x3)
addi x25, x25, 1
addi a0 x0 32
li a7 11
ecall

j p_loop_1
print_new_line:
.data
string_2:.string "\n"
.text
la a0 string_2
li a7 4
ecall 
return:
la x3 Node_1 
la x27 Node_1
reverse:
beqz x27 return2
    lw t5 4(x27)    
    sw t4 4(x27)    
    mv t4 x27       
    mv x27 t5  
    addi x23 x23 1     
    j reverse 
return2:
la x3 Node_1 
la x27 Node_10
addi x25 x0 0
p_2:
.data
string_1:.string "After reverse:"
.text
la a0 string_1
li a7 4
ecall 

p_loop_2:
beqz x27 exit
lw x26, 0(x27)
add a0, x0, x26
li a7, 1
ecall

lw x27 4(x27)
addi x25, x25, 1
addi a0 x0 32
li a7 11
ecall

j p_loop_2

exit:
addi x0 x0 0
```

**Output:** 
Before reverse : 11 12 13 14 15 16 17 18 19 20
After reverse : 20 19 18 17 16 15 14 13 12 11

**Explaination:**
In the context of RISC-V assembly language, a linked list typically refers to a data structure where each element contains a pointer to the next element in memory. This structure facilitates dynamic memory allocation and efficient traversal, commonly used in implementing various algorithms and data structures in low-level programming.

---
## Question 3
**Code:**
```assembly=
.equ N_size, 8
.equ N_value_offset, 0
.equ N_next_offset, 4

Allocate_Node:
    li  a0, N_size
    beqz    a0, Allocation_Failed
    sw  a1, N_value_offset(a0)
    li  t0, 0
    sw  t0, N_next_offset(a0)
    ret

Allocation_Failed:
    ret
```


