# Lab-4
---
Name : Viramgama Jaimin
Roll. No. : CS22B051
---
## Question-1
```C++ =
int s = 3;
for ( int i = 10; i >= 0; i = i - 1) {
x [i] = x[i] + s ;
}
```
```assemly=
.data
.word -3 0 -3 0 -3 0 -3 0 -3 0 -3
arr: .word 0x10000000
new: .string "Final Array: "
.text
addi x10 x0 3 #s
addi x11 x0 11 # i = 11
addi x12 x0 0
lw x9 arr #x9 is base register
loop:
beq x12 x11 exit
lw x13 0(x9) # load arr[i] into x13
add x13 x13 x10 # arr[i]+ = s
sw x13 0(x9)
addi x9 x9 4 # x9 = x9 + 4
addi x12 x12 1 # i--
j loop
exit:
li a7 4
la a0 new
ecall
lw x16, arr
addi x1 x0 11
addi x2 x0 0
print_loop:
beq x2 x1 print_exit
lw x5 0(x16)
addi a0 x5 0 #print
li a7 1
ecall
addi a0 x0 32 #spaces
li a7 11
ecall
addi x16 x16 4
addi x2 x2 1
j print_loop
print_exit:
li a7 10
ecall
```
---
## Question-2
```C++=
#include <iostream>
#include <vector>
using namespace std;
bool hasConsecutiveOdds(const vector<int>& arr) {
int count = 0;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] % 2 != 0) {
count++;
if (count == 3) {
return true;
}
} else {
count = 0;
}
}
return false;
}
int main() {
vector<int> arr = {1, 3, 5, 2, 4, 6, 7, 9, 11};
if (hasConsecutiveOdds(arr)) {
cout << "There are three consecutive odd numbers in the array." << endl;
} else {
cout << "There are no three consecutive odd numbers in the array." << endl;
}
return 0;
}
```
```assembly=
.data
.word 1, 3, 13, 2, 4, 6, 7, 9, 10
arr: .word 0x10000000
yes: .string "True"
no: .string "False"
.text
addi s1 x0 0 # i
addi s2 x0 9 # n
addi s6 x0 3 # 3
lw a1 arr
addi s5 x0 0 # count
addi t1 x0 0 # flag or iteration count
addi x1 x0 1
loop:
beq s1 s2 exit # Exit loop if i == n
lw a2 0(a1) # Load a[i]
andi s3 a2 1 # Extract last bit
beq s3 x1 if1 # If last bit is set, jump to if1
beq s3 x0 if2 # If last bit is not set, jump to if2
if1:
addi s5 s5 1 # Increment count
beq s5 s6 exit1 # If count == 3, exit loop
addi s1 s1 1 # Increment i
addi a1 a1 4 # Update pointer to next element
j loop
if2:
li s5 0
addi s1 s1 1 # Increment i
addi a1 a1 4 # Update pointer to next element
addi t1 t1 1 # Increment iteration count
j loop # Jump back to loop
exit1:
li a7 4 # Print "True"
la a0 yes
ecall
li a7 10 # Exit
ecall
exit:
j exit2 # Exit if iteration count == n
exit2:
li a7 4 # Print "False"
la a0 no
ecall
li a7 10 # Exit
ecall
j loop # Jump back to loop
```
---
## Question-3
```C++=
#include <iostream>
#include <vector>
using namespace std;
vector<int> shuffleArray(const vector<int>& nums) {
int n = nums.size() / 2;
vector<int> result;
for (int i = 0; i < n; i++) {
result.push_back(nums[i]);
result.push_back(nums[i + n]);
}
return result;
}
int main() {
// Example usage
vector<int> nums = {1,2,3,4,5,6};
vector<int> shuffled = shuffleArray(nums);
// Print the shuffled array
for (int num : shuffled) {
cout << num << " ";
}
cout << endl;
return 0;
}
```
```assembly=
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
.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
```C++=
#include <iostream>
using namespace std;
int main(){
int a[]= { 1, 2, 3, 5, 6};
int sum=0;
for(int i=0;i<5;i++){
sum+=a[i];
}
sum1= (int)6*7/2;
int missing = sum1-sum;
return 0;
}
```
```assembly=
.data
.word 1 2 0 4 5 6
arr: .word 0x10000000
.text
addi s1 x0 0
addi s2 x0 6
addi s3 x0 0
lw a2 arr
loop:
beq s1 s2 exit
lw s4 0(a2) #a[i]
add s3 s3 s1
sub s3 s3 s4
addi s1 s1 1 #i++
addi a2 a2 4
j loop
exit:
add s3 s3 s2
addi a0 s3 0
li a7 1
ecall
li a7 10 # Exit
ecall
```
---
## Question-5
```
the below code is generated using ChatGPT, but the initial approach was:
We can define a struct node and then define nodes and then do the traversals
as per the linked list norms which can be well depicted by the code here
```
```assembly=
# Define a Node structure
.data
node_size: .word 8 # Size of each node (assuming each node contains 2 words: data and next)
.text
# Define Node structure
Node:
.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
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
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
```
---
## Question-6
Core logic for the free function:
```
if(ptr == NULL)return;
brk(ptr);
```