# Lab 04
Name: CHATSE SIDDHANT MADHUKAR
Roll No.: CS22B016
---
## Question 1
**Code**
```assembly=
.data
.word 1 2 3 4 5 6 7 8 9 10 11
base: .word 0x10000000
.text
addi x1 x0 3
li x2 10
addi x3 x0 -1
lw x16 base
loop:
beq x2 x3 exit
lw x4 40(x16)
add x4 x4 x1
sw x4 40(x16)
addi x16 x16 -4
addi x2 x2 -1
j loop
exit:
lw x16 base
addi x2 x0 0
addi x7 x0 11
p_loop:
beq x2 x7 p_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 p_loop
p_exit:
li a7, 10
ecall
```
**Output:** 4 5 6 7 8 9 10 11 12 13 14
---
## Question 2
**Code**
```assembly=
.data
.word 1 2 4 5 7 9
base: .word 0x10000000
str1: .string "True"
str2: .string "False"
.text
addi x1 x0 10
addi x2 x0 0
addi x15 x0 1
addi x13 x0 3
lw x16 base
loop:
lw x4 0(x16)
beq x2 x1 exit
addi x2 x2 1
addi x16 x16 4
andi x3 x4 1
beq x3 x15 odd
beq x3 x0 even
j loop
even:
addi x5 x0 0
j loop
odd:
addi x5 x5 1
beq x5 x13 set
j loop
set:
addi x20 x0 1
exit:
beq x20 x15 e1
bne x20 x15 e2
e1:
la a0 str1
li a7,4
ecall
li a7 10
ecall
e2:
la a0 str2
li a7,4
ecall
li a7 10
ecall
```
**Output:** True
---
## Question 3
**Code**
```assembly=
.data
.word 1 2 3 4 5 11 12 13 14 15
b1: .word 0x10000000
.word 0 0 0 0 0 0 0 0 0 0
b2: .word 0x1000000
.text
addi x1 x0 5
addi x2 x0 0
lw x16 b1
lw x6 b2
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 b2
addi x7 x0 0
add x9 x1 x1
p_loop:
bge x7 x9 e_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 p_loop
e_print:
li a7, 10
ecall
```
**Output:** 1 11 2 12 3 13 4 14 5 15
---
## Question 4
268. Missing Number-
Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.
**Assembly Code:**
```assembly=
.data
sum: .word 0
n: .word 5
nums: .word 5 4 2 1 0
.text
la x1, sum
lw x2, 0(x1)
la x1, n
lw x3, 0(x1)
la x4, nums
loop:
beq x3 x0 end
lw x5, 0(x4)
sub x5, x3, x5
add x2, x2, x5
addi x3, x3, -1
addi x4, x4, 4
j loop
end:
add x2, x2, x3
mv a0, x2
li a7, 1
ecall
```
**Output:** 3
**C++ Code:**
```cpp=
class Solution {
public:
int missingNumber(vector<int>& nums) {
int sum=0;
int n=nums.size();
for(int i=0;i<n;i++)
{
sum+=(i-nums[i]);
}
sum+=n;
return sum;
}
};
```
---
## Question 5
**Explaination:**
Initially we will define structure called Node which will contain all the data that node holds.then we will define another structure named LinkedList to manage the head and tail pointers of the LinkedList. with this we have created the basic framework of LinkedList allowing us to perform different kinds of operations on it.
Appending:
To append a new node, we create a new Node and initialize its data with the necessary values.If the headNode pointer is nullptr, we will assign the newNode as the headNode.Otherwise, we link the newNode to the lastNode of the LinkedList by assigning the next attribute of the lastNode to the address of newNode.
Deleting:
To delete a node, We will take the position as inputand then we have to traverse through the LinkedList until the (pos-1)th node.We temporarily store node->next in a tempNode.Then, we update the node.next to (node->next)->next.Since the link has been removed, we can release tempNode for future use of that memory space.
similarly we can perform Update and insert Operations also.
---
## Question 6
**Explaination:**
Initially we will check whether the pointer is 'nullptr' or not. If it is a nullptr then we will end the function with an error message. Otherwise we execute various operations to release the allocated memory. Upon confirming that the pointer is not nullptr we determine which memory block it belongs to.we will release that specific block of memory mark it as free and thereby make it available for future space utilization.
---