---
# System prepended metadata

title: Lab 4

---

# Lab 4
Name: G Jaswanth
Roll No.: CS22B020

---
## Question 1

**Code** 
```assembly=
.data
array: .word 1 2 3 4 5 6 7 8 9 10 11
str: .string " " 

.text
la x1 array
addi x2 x0 3  #storing s=3
addi x3 x0 11 # iterator i=10
addi x1 x1 40    # moving the starting addr to array + 40 times
addi x7 x7 11

loop:
    lw x4 0(x1)     #loading the address into x4
    add x4 x4 x2    #adding x[i] = x[i] +3
    sw x4 0(x1)    #storing the incremented value
    
    addi x3 x3 -1    # i-- since started with i=10
    addi x1 x1 -4    # since we are at the last addr of array we need to decrement the addr
    beq x3 x0 print    #if i==0 start printing
    j loop

print:

    addi x1 x1 4
    lw x4 0(x1)
    addi a0 x4 0    #printing the incremented values
    li a7 1
    ecall
    
    la a0 str    # printing space between integers
    li a7 4
    ecall

    addi x3 x3 1    # increasing the iterator i++
    beq x3 x7 Exit    # if i==11 it gets exit
    j print

Exit:  # exit from the code
    li a7 10
    ecall 
```


_You can use either one of these according to the question_

**1 a**
output:
4 5 6 7 8 9 10 11 12 13 14


---
## Question 2
**Code** 
```assembly=
.data 
array: .word 1 4 3 5 7 8
str1: .string " True, there exists 3 consecutive odd nums"
str2: .string " False, there is no 3 consecutive odd nums"

.text
la x1 array    #loading array addr into x1
addi x2 x0 0    # int i=0
addi x3 x0 6    # array size n=6
addi x20 x3 -2

loop:
    lw x4 0(x1)     #loading all arr[i]
    lw x5 4(x1)    #loading arr[i+1]
    lw x6 8(x1)    #loading arr[i+2]
    addi x7 x0 1   # storing 1 in x7
    
    and x8 x7 x4    # checking condn whther arr[i] is odd 
    and x9 x7 x5    # checking condn whther arr[i+1] is odd 
    and x10 x7 x6    # checking condn whther arr[i+2] is odd 

   #below code is for whther there are 3 consec or not 
    and x8 x8 x9        
    and x8 x8 x10
    beq x8 x7 print1    # if condn satisfy it prints true
    
    addi x2 x2 1    # i++
    addi x1 x1 4    # increasing arr[i] to arr[i+1]
    beq x2 x20 print2    # if no 3 consecutive it prints false
    j loop

print1:            
    la a0 str1    # to print true
    li a7 4
    ecall
    
    li a7 10
    ecall

print2:
    la a0 str2    # to print false
    li a7 4
    ecall
```

```cpp=
class Solution {
public:
    bool threeConsecutiveOdds(vector<int>& arr) {
        int n=arr.size();
        int flag=0;
        for(int i=0;i<n-2;i++){
            if(arr[i]%2==1 && arr[i+1]%2==1 && arr[i+2]%2==1){
                flag=1;
                break;
            }
        }
        if(flag==1)
            return true;
        return false;
    }
};
```
_You can use either one of these according to the question_

**2 a**
Output:
True, there exists 3 consecutive odd nums




---
## Question 3

**Code** 
```assembly=
.data
array: .word 1,2,3,4,5,11,12,13,14,15
str: .string " "
newarray: .word 0x10000000
.text
addi x20 zero 10 # size of the array
la x1 array
la x5 array
la x2 newarray
la x3 newarray
addi x4 zero 0
addi x29 x29 0
loop:
    beq x20 x4 print     # 10!=0 
    lw x7 0(x1)    #loading arr[i] in x7
    lw x8 20(x5)    #loading the arr[n+i] in x8
    sw x7 0(x2)    #storing the loaded words
    sw x8 4(x2)    
    addi x1 x1 4    
    addi x5 x5 4    
    addi x2 x2 8    
    addi x4 x4 2    
    j loop    
print:
        lw x15 0(x3)    #load the address
       
        beq x20 x29 Exit    #exiting if they are e
        addi x29 x29 1        #increment the x29
        addi x3 x3 4
        
        addi a0 x15 0    #for printing changed values
        li a7 1
        ecall
        
        la a0 str    # for printing space
        li a7 4
        ecall
        j print
Exit:
    li a7 10        # to Exit 
    ecall

```

```cpp=
class Solution {
public:
    vector<int> shuffle(vector<int>& nums, int n) {
        vector <int> v;
        for(int i=0;i<n;i++){
            v.push_back(nums[i]);
            v.push_back(nums[i+1]);
        }
        return v;
      }
};
```
_You can use either one of these according to the question_

**3 a**
1 11 2 12 3 13 4 14 5 15




**Observation:**
* We gave input as 
* 1 2 3 4 5 11 12 13 14 15
* and output as 1 11 2 12 3 13 4 14 5 15 by taking two pointers at i and i+n position.
---
## Question 4
**Code** 
```assembly=
.data
array:.word 5,2,7,5,8,3,9,6
base:.word 0x10000000
str:.string " "
.text
addi x12 x0 8    #size of array int 
lw x14 base        
addi x15 x0 5    # we need to remove this element
Loop:
    beq x18 x12 exit
    lw x20 0(x14)
    addi x14 x14 4
    addi x18 x18 1    #i++
    bne x20 x15 print    # if it is not equal to selected elemt it goes to print
    j Loop
print:
    li a7 1    # for printing except the selected element
    addi a0 x20 0
    ecall
    
    li a7 4    #For printing spaces
    la a0 str
    ecall
    j Loop
exit:
    li a7 10    #Exit 
    ecall 
```

```cpp=
class Solution {
public:
    void Print_Emnts(vector<int>& nums, int n) {
        for(int i=0;i<n;i++){
            if(nums[i]!=n){
                cout<<nums[i]<<" ";
            }
        }
      }
};
```
_You can use either one of these according to the question_

**4 a**
Output:
2 7 8 3 9 6



**Observation:** 
This code is to print the array elemnts except the selected value..

---
## Question 5
**Code** (if required)
```assembly=
.data
    newline: .string "\n"

.text
main:
    # Initialize the linked list
    la x5, elem1     # Load the address of the first element into x5
    la x6, elem2     # Load the address of the second element into x6
    la x7, elem3     # Load the address of the third element into x7
    la x8, elem4     # Load the address of the fourth element into x8

    # Link the elements together
    sw x6, 4(x5)     # Store the address of elem2 in the next pointer of elem1
    sw x7, 4(x6)     # Store the address of elem3 in the next pointer of elem2
    sw x8, 4(x7)     # Store the address of elem4 in the next pointer of elem3

    # Print all elements of the linked list
    la a0, elem1     # Load the address of the head of the list into a0
    jal ra, print_list

    # Insert a new element into the linked list
    la x9, new_elem  # Load the address of the new element into x9
    lw x10, 4(x5)     # Load the next pointer of elem1 into x10
    sw x9, 4(x5)     # Store the address of the new element in the next pointer of elem1
    sw x10, 4(x9)     # Store the address of elem2 in the next pointer of the new element

    # Print all elements of the updated linked list
    la a0, elem1     # Load the address of the head of the list into a0
    jal ra, print_list

    # Exit program
    li a7, 10        # Exit syscall
    ecall

print_list:
    # Load the address of the head of the list into a1
    mv a1, a0

loop:
    # Load the data of the current node into a1
    lw a1, 0(a1)
    
    # Print the value of the current node
    mv a0, a1        # Set a0 to the data value for printing
    li a7, 1         # Print integer syscall
    ecall

    # Print a newline character
    la a0, newline   # Load the newline character into a0
    li a7, 4         # Print string syscall
    ecall

    # Get the next pointer
    lw a0, 4(a0)

    # Check if we reached the end of the list
    bnez a0, loop   # If the next pointer is not zero, continue to the next node

    ret

.data
    elem1: .word 1, 0     # First element of the linked list
    elem2: .word 2, 0     # Second element of the linked list
    elem3: .word 3, 0     # Third element of the linked list
    elem4: .word 4, 0     # Fourth element of the linked list
    new_elem: .word 99, 0 # New element to be inserted into the linked list

```


**Observation:** 

* We can Link the two node by storing the element address in the next i+4th address where i is the addr of elemnt etc 
---
## Question 6
**Code** (if required)
```assembly=
.data
    .align 3
    FREE_LIST: .word 0            # Pointer to the head of the free list

.text
malloc:
    # Function prologue
    addi sp, sp, -4         # Adjust stack pointer
    sw x1, 0(sp)            # Save return address (using x1 instead of ra)
    
    # Load argument (size to allocate)
    mv x11, x10             # Move size to x11 (x10 will be used as return value)
    
    # Allocate memory using sbrk
    li a7, 214              # sbrk syscall number
    ecall                   # Call sbrk
    
    # Move return value (new memory address) to x10
    mv x10, a0              # x10 already holds the return value
    
    # Function epilogue
    lw x1, 0(sp)            # Restore return address
    addi sp, sp, 4          # Restore stack pointer
    ret                     # Return from function

free:
    # Function prologue
    addi sp, sp, -4         # Adjust stack pointer
    sw x1, 0(sp)            # Save return address (using x1 instead of ra)
    
    # Load arguments
    mv a0, a0               # Assuming pointer to be freed is passed in a0
    
    # For simplicity, we do nothing with the freed memory
    
    # Function epilogue
    lw x1, 0(sp)            # Restore return address
    addi sp, sp, 4          # Restore stack pointer
    ret                     # Return from function

```
