---
# System prepended metadata

title: '     ***QUESTION 1***     .da'

---

LAB 4:

NAME :BHUKYA LACHIRAM NAYAK
ROLL:CS22B012
            ***question 1**
      ```assembly=
      
      
       .data
x: .word 0:11   # Initialize 11 elements of array x to 0
s: .word 3       # Initialize variable s to 3

.text
main:
    # Initialize variables
    la x10, x      # Load base address of array x
    lw x5, s       # Load value of s
    li x6, 10      # Initialize i to 10

loop:
    bge x6, x0, exit  # Exit loop if i < 0
    lw x7, 0(x10)     # Load x[i]
    add x7, x7, x5    # x[i] = x[i] + s
    sw x7, 0(x10)     # Store updated value back to x[i]
    addi x10, x10, -4 # Move to the previous element of x
    addi x6, x6, -1   # Decrement i
    j loop            # Jump back to loop

exit:
    # Exit program
    li x7, 10         # Service to exit
    ecall
    ```

        observation:writing into assembly
         
         
         
            ***QUESTION 2***
```assembly=
 ..data
    arr: .word 1,0,5,6,8,9,0,0,0
    size: .word 9
.text
    la x5 arr    # loading the address of array 
    add x6 x0 x0    # i = 0
    add x11 x0 x0    #variable1 to check if previous num is odd
    add x13 x0 x0     # variable2 to check if previous two consecutive nums are odd
    lw x4 size 
    loop:
        slli x7 x6 2     #shifting left logical immediately
        add x7 x7 x5
        lw x8 0(x7)    #x8 = arr[i]
        li x9 1     
        and x8 x8 x9    #if arr[i] is odd then x8 will be 1
        and x12 x8 x11    #x12 will be 1 if current and previous nums are odd
        and x14 x13 x8    #x14 will be 1 if current and previous two nums are odd 
        beq x14 x9 exit    
        add x13 x0 x12    #x13 = x12
        add x11 x0 x8    #x11 = x8
        addi x6 x6 1
        blt x6 x4 loop
    
    
    exit:
        add a0 x0 x14    #prints 1 if true and 0 if false
        li a7 1
        ecall
    ```
    
OBSERVATION :termination



              **QUESTION 3**
              
              
```assembly=

.data
.word 0 1 2 3 4 5 6 7 8 9
base: .word 0x10000000
str1: .string "Initial Array is \n"
str2: .string "Modified Array is \n"
str3: .string " "
str4: .string "\n"

.text
lw x1,base
li s1, 0x10000028
main: li t2,0 # i=0
      li t1,5
Func: beq t1,t2,Exit
      lw a0,0(x1)
      sw a0,0(s1)
      lw a0,20(x1)
      sw a0,4(s1)
      addi x1,x1,4
      addi s1,s1,8
      addi t2,t2,1
      j Func
Exit:
    li a7 10

```
OBSERVATION:
Transformation


                 **QUESTION 4**
```assembly=
    .data
array:  .word 1,1,1,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,6,  # Array to be sorted
size:   .word 23        # Size of the array
str:    .string " "

.text
#loading the address in x4
la x4,array
#loading size of array in x5
lw x5,size
#incrementor i in x6
li x6,0
#counter in x11
li x11,1
#unique counter in x23
#address in x9
mv x12,x4
j solve

solve:
    lw x7,0(x4)
    sw x7,0(x12)
    addi x12,x12,4
    addi x23,x23,1
    mv x9,x4
    beq x5,x11,print_int
    jal x1,unique
    
unique:
    addi x4,x4,4
    addi x11,x11,1
    lw x8,0(x4)
    bne x8,x7,solve
    j unique
    
print_int:
    la x4,array
    li x1,0
    j print

print:
    li a7,1
    lw x2,0(x4)
    mv a0,x2
    ecall
    li a7,4
    la a0,str
    ecall
    addi x4,x4,4
    addi x1,x1,1
    beq x1,x23,exit
    j print

exit:
    li a7,10
    ecall


```
OBSERVATION:
LinkedListManipulation

              ***question 5**

```assembly=

#Define Node structure
.data 
node_size:  .word 8
head:   .word 0         #Head pointer initialized to NULL
On many RISC-V systems, the number 9 corresponds to the sbrk system call, which is used to adjust the program's data space (heap) and allocate additional memory.

.text
#Allocate memory for a new node
malloc_node:
    #Size of the node in bytes
    li a0, node_size   
    li a7, 9  
    #System call for sbrk 
    ecall
    #Result (address of allocated memory) is in a0
    ret
To insert a new node, first, read the value from the register to access the head node. Load the next pointer from the head node. If the next pointer is equal to x0 (indicating an empty or uninitialized node), add the address of the new node at this position. Save the new node's data and address in memory, and store the address in the designated location.

#Traversing the linked list 
traverse_print:
    lw a0, head         
    loop_start:
        lw a1, 0(a0)    #Load data from the current node
        lw a0, 4(a0)    # Load the next pointer
        bnez a0, loop_start  
    ret
Searching can be easily accomplished by traversing through consecutive memory locations pointed to by each node until the desired data is found or a NULL pointer is encountered.
After searching delete operation can also be done. By updating the previous node pointer value to current node's pointer value
```

         ***question 6**
         ```assembly=
         
         Function free(ptr):
    # Assuming each allocated memory block has a header containing its size and a pointer to the next block
    # Mark the memory block pointed to by ptr as free

    # Traverse the linked list of allocated memory blocks
    # Find the block containing ptr
    # Mark the block as free

    # Coalesce adjacent free blocks if possible

    # Update the linked list of allocated memory blocks
                     
                     
```
observation:free up space
                     
                     












              
              