# Lab 4
Name: Manan Chavda
Roll No.: CS22B017
---
## Question 1
**Code**
```c=
.data
.word 1 4 5 6 7 9 8 12 11 10
base:.word 0x10000000
.text
lw x16 base
addi x21 x0 3
addi x22 x0 10
loop:
lw x19 36(x16)
beq x0 x22 exit
addi x22 x22 -1
addi x16 x16 -4
add x19 x19 x21
j loop
exit:
addi x0 x0 0
```
---
## Question 2
**Code**
```c=
.data
.word 1 3 6 7 7 10 11 15 17 18
base:.word 0x10000000
.text
lw x16 base
addi x21 x0 3
addi x22 x0 10
addi x30 x0 1
loop:
lw x19 0(x16)
beq x23 x22 print
addi x23 x23 1
addi x16 x16 4
andi x24 x19 1
beq x24 x30 oddcounter
bne x24 x30 reset
j loop
reset:
addi x27 x0 0
j loop
oddcounter:
addi x28 x28 0
addi x27 x27 1
beq x27 x21 result
j loop
result:
addi x29 x0 1
print:
beq x29 x30 print2
bne x29 x30 print3
print2:
.data
str4:.string "Yes"
.text
la a0 str4
li a7 4
ecall
j exit
print3:
.data
str3:.string "No"
.text
la a0 str3
li a7 4
ecall
j exit
exit:
addi x0 x0 0
```
___
## Question 3
**Code**
```c=
.data
.word 1 2 3 4 5 6 7 8 9 10
base1: .word 0x10000000
.word 0 0 0 0 0 0 0 0 0 0
base2: .word 0x1000000
str1: .string "TRUE"
.text
addi x1 x0 5 #No of array element
addi x2 x0 0 #x2 is i
lw x16 base1
lw x6 base2
loop:
beq x2 x1 exit
lw x4 0(x16)
lw x5 20(x16)
sw x4 0(x6)
sw x5 4(x6)
addi x16 x16 4
addi x6 x6 8
addi x2 x2 1
j loop
exit:
lw x6 base2
addi x7 x0 0
add x9 x1 x1
print_loop:
bge x7 x9 end_print
lw x11 0(x6)
addi a0, x11, 0
li a7, 1
ecall
addi a0, x0, 32
li a7, 11
ecall
addi x6 x6 4
addi x7 x7 1
j print_loop
end_print:
li a7, 10
ecall
```
___
## Question 4
**Code**
```c=
.data
.word 1 3 5 6
base: .word 0x10000000
.text
lw x16 base
addi x4 x0 0
addi x7 x0 2 #x7 is the value of target
loop:
lw x8 0(x16)
bge x8 x7 print
addi x4 x4 1
addi x16 x16 4
j loop
#value of x4 will be the insert position we want
print:
li a7 11
add a0 x0 x4
li a7 1
ecall
```
**link of attempted question**
Search-insert position
https://leetcode.com/problems/search-insert-position/description/
___
## Question 5
**Pseudo Code/Algorithm**
```c=
.data
node_size: .word 8
#size of each node
.text
.word 0 # Data
.word 0 # Pointer to next node
# Define functions
# Function to initialize a linked list
init_linked_list:
# Arguments:
# a0 = Pointer to the head of the linked list
# t0 = Size of the linked list
li t1, 0 # Initialize loop counter to 0
init_loop:
# Initialize each node with 0
la t2, Node # Load address of Node structure
add t2, t2, t1 # Calculate address of current node
sw zero, 0(t2) # Initialize data to 0
sw zero, 4(t2) # Initialize next pointer to 0
addi t1, t1, 8 # Move to the next node
blt t1, a1, init_loop # Loop until all nodes are initialized
ret
# Function to append a node to the end of the linked list
append_to_linked_list:
# Arguments:
# a0 = Pointer to the head of the linked list
# a1 = Value to be appended
# Traverse the list to find the last node
la t1, Node # Load address of Node structure
traverse_loop:
lw t2, 4(a0) # Load address of next node
beqz t2, append_node # If next node is NULL, append the new node
add a0, t2, zero # Move to the next node
j traverse_loop # Continue traversing the list
append_node:
# Allocate memory for the new node
la t2, Node # Load address of Node structure
li t3, 8 # Size of Node structure
mv a2, a0 # Move pointer to head of list to a2
jal malloc # Call malloc function to allocate memory for new node
add a0, v0, zero # Move address of new node to a0
sw a1, 0(a0) # Store data in the new node
sw zero, 4(a0) # Set next pointer of new node to NULL
# Link the new node to the end of the list
sw a0, 4(a2) # Set next pointer of previous last node to point to the new node
ret
# Function to free the memory allocated for the linked list
free_linked_list:
# Arguments:
# a0 = Pointer to the head of the linked list
free_loop:
lw t0, 4(a0) # Load address of next node
beqz t0, exit_free # If next node is NULL, exit loop
mv a0, t0 # Move to the next node
jal free # Call free function to deallocate memory for current node
j free_loop # Continue freeing memory for remaining nodes
exit_free:
jal free # Deallocate memory for last node
ret
```
**Explanation**
here,I have constructed an implementation of a linked list,some of the basic functions I've added here. It might not run as a code,its just for a rough idea.
___
## Question 6
**Pseudo Code/Algorithm**
```c=
free(void* ptr){
if ptr == nullptr{
return;
}
brk(ptr);
return;
}