---
# System prepended metadata

title: aman

---

  # LAB 4
  ## AMAN ANAND
  ## CS22B054
  
  
  
## QUESTION  1
### C++ CODE
`code`
int s = 3;

 for ( int i = 10; i >= 0; i = i - 1) {
 x [ i ] = x[ i ] + s ;
 }
 ### ASSEMBLY CODE
`code`
       .data
        s:  .word 3
        x:  .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
      //total 11 elements
    .text
    .global main

    main:
  
    li t0, 10        # t0 = i
    la x3,x
    li x22,0
    li x24,11
    init_loop:
   
    slli t1, t0, 2   
    add t1, t1, x3    # t1 = &x[i]

    lw t2, 0(t1)     # t2 = x[i]


    lw t3, s         # t3 = s

   
    add t2, t2, t3   # t2 = x[i] + s

    
    sw t2, 0(t1)     # x[i] = t2


    addi t0, t0, -1  

 
    bgez t0, init_loop 
    print:
       lw x23,0(x3)
       add a0,x23,x0
       li a7,1
       ecall
       addi x22,x22,1
       addi x3,x3,4
       blt x22,x24,print
       li a7,10 
       ecall 
       
       
## QUESTION 2
 ### C++ CODE
 `code`
 
 
     
    std::vector<int> arr = {1, 2, 3, 5, 7, 6, 9};
    int n = arr.size();

    for (int i = 0; i < n - 2; i++) {
        if (arr[i] % 2 && arr[i + 1] % 2 && arr[i + 2] % 2) {
            std::cout << "true" << std::endl;
            return 0;
        }
    }

    std::cout << "false" << std::endl;
    return 0;
}
                                     
                                     
     

### ASSEMBLY CODE




`code`

    .data
     arr:  .word 1,2, 3, 5, 7, 6, 9
     size: .word 7 
      tru: .string "true"
        fal: .string "false"
       .text
        .global main
            main:
         li x1,0 #i
           la x2,arr
           li x3,5
      loop:
       slli x22,x1,2
       add x4,x1,x22
       addi x5,x4,4
       addi x6,x5,4
       lw x7,0(x4)
       lw x8,0(x5)
       lw x9,0(x6)
       andi x11,x7,1
       andi x12,x8,1
       andi x13,x9,1
       and x14,x11,x12
       and x15,x14,x13
       beq x15,x0, nodd
       la a0,tru
       li a7,4
       ecall
       li a7,10
       ecall
       nodd:
        addi x1,x1,1
        blt x1,x3,loop  
        la a0,fal
        li a7,4
        ecall
        li a7,10
        ecall



## QUESTION 3
### C++ CODE

`code`

       vector<int> shuffle(vector<int>& nums, int n)          {
       int j=0;
       int p= nums.size();
        vector <int > v(p);
        for(int i=0;i<n;i++){
            v[j]=nums[i];
           j=j+2;
        }
        int l=1;
        for(int k=n;k<p;k++){
            v[l]=nums[k];
            l=l+2;
         }
        return v;
       }
             
             
             

### ASSEMBLY CODE

`code` 

        .data
         arr: .word 2,5,1,3,4,7
           vec: .word 0,0,0,0,0,0
        s: .string "\n"
          .text
         .global main
          main: 
    la x1,arr
    la x9,vec
    li x2,0 #j
    li x3,0 #i
    li x12,1 # l
    li x11,3
    li x15,6
        li x25,0
    loop:
        slli x4,x3,2
        add x5,x4,x1
        lw x6,0(x5)
         slli x7,x2,2
         add x8,x7,x9
         sw x6,0(x8)
        
         addi x3,x3,1
         addi x2,x2,2
         blt x3,x11,loop
     
         loop1:
             slli x14,x11,2
             add x16,x14,x1
             lw x17,0(x16)
             slli x18,x12,2
             add x19,x18,x9
             sw x17,0(x19)
         
             addi x11,x11,1
             addi x12,x12,2
             blt x11,x15,loop1
         
             print:
                 lw a0,0(x9)
                 li a7,1
                 ecall
                 addi x25,x25,1
                 addi x9,x9,4
                 blt x25,x15,print 
                 li a7,10
                 ecall


## QUESTION 4
### C++ CODE

`code`

        #include <iostream>
         #include<bits/stdc++.h>
           bool isPalindrome(int number) {
    int reversedNumber = 0;
    int originalNumber = number;

    while (number > 0) {
        int digit = number % 10;
        reversedNumber = reversedNumber * 10 + digit;
        number /= 10;
    }

    return originalNumber == reversedNumber;
       }

         int main() {
    int number = 121;

    if (isPalindrome(number)) {
        std::cout << "Number " << number << " is a palindrome." << std::endl;
    } else {
        std::cout << "Number " << number << " is not a palindrome." << std::endl;
    }

    return 0;
             }

### ASSEMBLY CODE
`code`

      .data
       number: .word 1221          
     
      palin: .string "palindrome"
       notpalin: .string "not palindrome"
        .text
        .global main
                main:
        lw x1, number
         li x2, 0       
         li x3, 0       
    li x6, 10  
    mv x5, x1   
        reverse_loop:
    rem x3, x5, x6   
    mul x2, x2, x6   
    add x2, x2, x3    
    div x5, x5, x6   
    bnez x5, reverse_loop  
    beq x1, x2, is_palindrome  
    la a0, notpalin         
    li a7, 4       
    ecall         
    j exit
       is_palindrome:                 
    la a0, palin     
    li a7, 4      
    ecall          
         exit:
           li a7, 10     
          ecall          


## QUESTION 5
 ### PSEUDO CODE 
 
 Define the Node structure:

Each node should contain two fields: a value field to store the data and a next field to store the address of the next node.
Example:
struct Node {
    int value;
    Node* next;
};
1. Initialize: Set the head pointer to NULL to indicate an empty list.
2. Insert: Create a new node with the given value and insert it at the beginning of the list.
3. Delete: Find the node with the given value and remove it from the list.
4. Search: Find the node with the given value and return its address.
5. Display: Traverse the list and print the values of all nodes.

 Handle edge cases:
- Ensure proper handling of empty lists.
- Check for memory allocation errors.
- Handle duplicate values in insert and delete operations.


`psedo code`

      .data
      node_size:  .word 8        # Size of a node (2 words: value and next pointer)
     list_head:  .word 0        # Pointer to the head of the list

      .text
       .global _start
         allocate_node:
     lw a0, node_size        # Load node size
    li a7, 9               
    ecall                   # Allocate memory
 
    insert_beginning:
     
    sw t0, 0(a0)
     list_head
    lw t1, list_head
    sw t1, 4(a0)
  
    mv t1, a0
    sw t1, list_head
    
    mv t0, a0   

    display_list:
    lw t0, list_head        # Load list_head into t0
    li a0, 0                # Set loop counter to 0

       display_loop:
    beqz t0, end_display    # If t0 is 0, end loop
    lw a1, 0(t0)            # Load value of current node
    mv a0, a1               
    li a7, 1             
    ecall                  
    lw t0, 4(t0)            
    addi a0, a0, 32         # Print space between values
    li a7, 4                
    ecall                  
    addi a0, a0, 1        
    j display_loop         

    end_display:
    li a7, 10               
    ecall


   

    # Display the list
    call display_list

    # End of program
    li a7, 10             
    ecall


## QUESTION 6
### APPROACH
The free() function in a memory management system is responsible for deallocating a block of memory that was previously allocated by the malloc() or calloc() functions. Here's a pseudo-code algorithm for the free() function:
`psedo code`

    free(ptr):
    1. Check if ptr is NULL. If so, return.
    2. Find the memory block associated with ptr in the memory management system's data structures.
    3. Mark the memory block as free.
    4. Merge the freed memory block with adjacent free blocks, if possible, to reduce fragmentation.
    5. Update the memory management system's data structures to reflect the freed memory block.
    
 
 
 
Implementation of the free() function in C language. This implementation assumes a simple memory allocator that maintains a linked list of free memory blocks.


 ### PSEDO CODE
`code`

    void free(void *ptr) {
    if (ptr == NULL) {
        return; // Do nothing if ptr is NULL
    }

    // Get the block_meta structure corresponding to the given pointer
    block_meta *block_ptr = (block_meta *)ptr - 1;
    block_ptr->free = 1;

    // Merge adjacent free blocks
    merge_free_blocks();
   }


This pseudo-code assumes the presence of a block_meta structure for managing memory blocks. The free() function marks the block corresponding to the given pointer as free and then merges adjacent free blocks to reduce fragmentation. The merge_free_blocks() function is assumed to be implemented elsewhere in the codebase and is responsible for merging adjacent free blocks in the memory allocator's data structures.



==In practice, the free() function typically involves releasing the memory block pointed to by ptr back to the system's memory management system. This may involve updating internal data structures to indicate that the memory block is now free and available for reuse. The exact implementation details depend on the memory allocator being used (e.g., malloc() implementation) and the underlying operating system.==
