---
# System prepended metadata

title: Lab x

---

# Lab 5
Name: Harshit garg
Roll No.: CS22B024

---
## Question 1

**Code** 
```assembly=
.data
arr: .word 10 20 30 40 50 60 70 80 90 100 110 120
.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***

13 23 33 43 53 63 73 83 93 103 113 123







---
## Question 2
**Code** 
```assembly=
.data
node1: .word 10
.word 0

node2: .word 20
.word 0

node3: .word 30
.word 0

node4: .word 40
.word 0

node5: .word 50
.word 0

node6: .word 60
.word 0

node7: .word 70
.word 0

node8: .word 80
.word 0

node9: .word 90
.word 0

node10: .word 100
.word 0

.text 

la x3 , node1
la x4 , node2
la x5 , node3
la x6 , node4
la x7 , node5
la x8 , node6
la x9 , node7
la x10 , node8
la x11 , node9
la x12 ,node10

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

print1:
.data
str:.string "Before reverse:"
.text
la a0 str
li a7 4
ecall 
printloop1:
beqz x3 printnewline
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 printloop1
printnewline:
.data
str2:.string "\n"
.text
la a0 str2
li a7 4
ecall 
return:
la x3 node1 
la x27 node1
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 node1 
la x27 node10
addi x25 x0 0
print2:
.data
str1:.string "After reverse:"
.text
la a0 str1
li a7 4
ecall 

printloop2:
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 printloop2

exit:
addi x0 x0 0

```

***Output***

Before reverse : 10 20 30 40 50 60 70 80 90 100

After reverse : 100 90 80 70 60 50 40 30 20 10

***Explanation :*** 

In the context of RISC-V assembly language, a linked list typically refers to a data structure where each element, or node, contains a reference or pointer to the next element in the sequence. This structure allows for dynamic memory allocation and efficient insertion and deletion operations. In RISC-V assembly, manipulating linked lists involves managing memory addresses and pointers to navigate through the list, often using load and store instructions to access data and update pointers accordingly. Efficient traversal and manipulation of linked lists are crucial for many algorithms and data processing tasks in RISC-V assembly programming.

---

## Question 3
**Code** 
```assembly=

.equ NODE_SIZE, 8
.equ NODE_VALUE_OFFSET, 0
.equ NODE_NEXT_OFFSET, 4

allocate_node:
    li  a0, NODE_SIZE
    beqz    a0, allocation_failed
    sw  a1, NODE_VALUE_OFFSET(a0)
    li  t0, 0
    sw  t0, NODE_NEXT_OFFSET(a0)
    ret

allocation_failed:
    ret
```



---

